-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
147 lines (120 loc) · 4 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
)
func TestGetUsers(t *testing.T) {
// Create a request to pass to our handler
req, _ := http.NewRequest("GET", "/users", nil)
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Create a router and pass in the request and response recorder
r := chi.NewRouter()
r.Get("/users", getUsers)
r.ServeHTTP(rr, req)
// Check the status code is what we expect
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body is what we expect
var users []User
json.Unmarshal(rr.Body.Bytes(), &users)
assert.Equal(t, 0, len(users))
}
func TestCreateUser(t *testing.T) {
user := User{
Name: "John Doe",
Email: "johndoe@example.com",
Password: "mysecretpassword",
Address: "123 Main St",
ZipCode: "12345",
State: "NY",
}
body, _ := json.Marshal(user)
// Create a request to pass to our handler
req, _ := http.NewRequest("POST", "/users", bytes.NewBuffer(body))
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Create a router and pass in the request and response recorder
r := chi.NewRouter()
r.Post("/users", createUser)
r.ServeHTTP(rr, req)
// Check the status code is what we expect
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body is what we expect
var createdUser User
json.Unmarshal(rr.Body.Bytes(), &createdUser)
assert.Equal(t, user.Name, createdUser.Name)
assert.Equal(t, user.Email, createdUser.Email)
assert.Equal(t, user.Password, createdUser.Password)
assert.Equal(t, user.Address, createdUser.Address)
assert.Equal(t, user.ZipCode, createdUser.ZipCode)
assert.Equal(t, user.State, createdUser.State)
}
func TestUpdateUser(t *testing.T) {
user := User{
ID: 1,
Name: "Jane Doe",
Email: "janedoe@example.com",
Password: "mysecretpassword",
Address: "456 Park Ave",
ZipCode: "67890",
State: "CA",
}
users = append(users, user)
body, _ := json.Marshal(user)
// Create a request to pass to our handler
req, _ := http.NewRequest("PUT", "/users/1", bytes.NewBuffer(body))
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Create a router and pass in the request and response recorder
r := chi.NewRouter()
r.Put("/users/{id}", updateUser)
r.ServeHTTP(rr, req)
// Check the status code is what we expect
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body is what we expect
var updatedUser User
json.Unmarshal(rr.Body.Bytes(), &updatedUser)
assert.Equal(t, user.Name, updatedUser.Name)
assert.Equal(t, user.Email, updatedUser.Email)
assert.Equal(t, user.Password, updatedUser.Password)
assert.Equal(t, user.Address, updatedUser.Address)
assert.Equal(t, user.ZipCode, updatedUser.ZipCode)
assert.Equal(t, user.State, updatedUser.State)
}
func TestDeleteUser(t *testing.T) {
user := User{
ID: 1,
Name: "John Doe",
Email: "johndoe@example.com",
Password: "mysecretpassword",
Address: "123 Main St",
ZipCode: "12345",
State: "NY",
}
users = append(users, user)
// Create a request to pass to our handler
req, _ := http.NewRequest("DELETE", "/users/1", nil)
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Create a router and pass in the request and response recorder
r := chi.NewRouter()
r.Delete("/users/{id}", deleteUser)
r.ServeHTTP(rr, req)
// Check the status code is what we expect
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body is what we expect
var deletedUser User
json.Unmarshal(rr.Body.Bytes(), &deletedUser)
assert.Equal(t, user.ID, deletedUser.ID)
assert.Equal(t, user.Name, deletedUser.Name)
assert.Equal(t, user.Email, deletedUser.Email)
assert.Equal(t, user.Password, deletedUser.Password)
assert.Equal(t, user.Address, deletedUser.Address)
assert.Equal(t, user.ZipCode, deletedUser.ZipCode)
assert.Equal(t, user.State, deletedUser.State)
}