-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_test.go
284 lines (231 loc) · 7.59 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gorilla/mux"
)
func setup() *App {
// Initialize an in-memory database for full integration testing.
app := &App{}
app.Initialize("sqlite3", ":memory:")
return app
}
func teardown(app *App) {
// Closing the connection discards the in-memory database.
app.DB.Close()
}
func StarFormValues(star Star) *strings.Reader {
// Transforms Star record into *strings.Reader suitable for use in HTTP POST forms.
data := url.Values{
"name": {star.Name},
"description": {star.Description},
"url": {star.URL},
}
return strings.NewReader(data.Encode())
}
func TestCreateHandler(t *testing.T) {
app := setup()
testStar := &Star{
ID: 1,
Name: "test/name",
Description: "test desc",
URL: "test url",
}
// Set up a new request.
req, err := http.NewRequest("POST", "/stars", StarFormValues(*testStar))
if err != nil {
t.Fatal(err)
}
// Our API expects a form body, so set the content-type header to make sure it's treated as one.
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
http.HandlerFunc(app.CreateHandler).ServeHTTP(rr, req)
// Test that the status code is correct.
if status := rr.Code; status != http.StatusCreated {
t.Errorf("Status code is invalid. Expected %d. Got %d instead", http.StatusCreated, status)
}
// Test that the Location header is correct.
expectedURL := fmt.Sprintf("/stars/%s", testStar.Name)
if location := rr.Header().Get("Location"); location != expectedURL {
t.Errorf("Location header is invalid. Expected %s. Got %s instead", expectedURL, location)
}
// Test that the created star is correct.
// Note: There is only one star in the database.
createdStar := Star{}
app.DB.First(&createdStar)
if createdStar != *testStar {
t.Errorf("Created star is invalid. Expected %+v. Got %+v instead", testStar, createdStar)
}
teardown(app)
}
func TestUpdateHandler(t *testing.T) {
app := setup()
// Create a star for us to update.
testStar := &Star{
ID: 1,
Name: "test/name",
Description: "test desc",
URL: "test url",
}
app.DB.Create(testStar)
// Set up a test table.
starTests := []struct {
original Star
update Star
}{
{original: *testStar,
update: Star{ID: 1, Name: "test/name", Description: "updated desc", URL: "test URL"},
},
{original: Star{ID: 1, Name: "test/name", Description: "updated desc", URL: "test URL"},
update: Star{ID: 1, Name: "updated name", Description: "updated desc", URL: "test URL"},
},
}
for _, tt := range starTests {
// Set up a new request.
req, err := http.NewRequest("PUT", fmt.Sprintf("/stars/%s", tt.original.Name), StarFormValues(tt.update))
if err != nil {
t.Fatal(err)
}
// Our API expects a form body, so set the content-type header appropriately.
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
// We need a mux router in order to pass in the `name` variable.
r := mux.NewRouter()
r.HandleFunc("/stars/{name:.*}", app.UpdateHandler).Methods("PUT")
r.ServeHTTP(rr, req)
// Test that the status code is correct.
if status := rr.Code; status != http.StatusNoContent {
t.Errorf("Status code is invalid. Expected %d. Got %d instead", http.StatusNoContent, status)
}
// Test that the updated star is correct.
// Note: There is only one star in the database.
updatedStar := Star{}
app.DB.First(&updatedStar)
if updatedStar != tt.update {
t.Errorf("Updated star is invalid. Expected %+v. Got %+v instead", tt.update, updatedStar)
}
}
teardown(app)
}
func TestViewHandler(t *testing.T) {
app := setup()
// Set up a test table.
starTests := []Star{
Star{ID: 1, Name: "test/name", Description: "test desc", URL: "test URL"},
Star{ID: 2, Name: "test/another_name", Description: "test desc 2", URL: "http://example.com/"},
}
for _, star := range starTests {
// Create a star for us to view.
app.DB.Create(star)
// Set up a new request.
req, err := http.NewRequest("GET", fmt.Sprintf("/stars/%s", star.Name), nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
// We need a mux router in order to pass in the `name` variable.
r := mux.NewRouter()
r.HandleFunc("/stars/{name:.*}", app.ViewHandler).Methods("GET")
r.ServeHTTP(rr, req)
// Test that the status code is correct.
if status := rr.Code; status != http.StatusOK {
t.Errorf("Status code is invalid. Expected %d. Got %d instead", http.StatusOK, status)
}
// Read the response body.
data, err := ioutil.ReadAll(rr.Result().Body)
if err != nil {
t.Fatal(err)
}
// Test that the updated star is correct.
returnedStar := Star{}
if err := json.Unmarshal(data, &returnedStar); err != nil {
t.Errorf("Returned star is invalid JSON. Got: %s", data)
}
if returnedStar != star {
t.Errorf("Returned star is invalid. Expected %+v. Got %+v instead", star, returnedStar)
}
}
teardown(app)
}
func TestListHandler(t *testing.T) {
app := setup()
// Create a couple stars to list.
stars := []Star{
Star{ID: 1, Name: "test/name", Description: "test desc", URL: "test URL"},
Star{ID: 2, Name: "test/another_name", Description: "test desc 2", URL: "http://example.com/"},
}
for _, star := range stars {
app.DB.Create(star)
}
// Set up a new request.
req, err := http.NewRequest("GET", "/stars", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
http.HandlerFunc(app.ListHandler).ServeHTTP(rr, req)
// Test that the status code is correct.
if status := rr.Code; status != http.StatusOK {
t.Errorf("Status code is invalid. Expected %d. Got %d instead", http.StatusOK, status)
}
// Read the response body.
data, err := ioutil.ReadAll(rr.Result().Body)
if err != nil {
t.Fatal(err)
}
// Test that our stars list is the same as what was returned.
returnedStars := []Star{}
if err := json.Unmarshal(data, &returnedStars); err != nil {
t.Errorf("Returned star list is invalid JSON. Got: %s", data)
}
if len(returnedStars) != len(stars) {
t.Errorf("Returned star list is an invalid length. Expected %d. Got %d instead", len(stars), len(returnedStars))
}
for index, returnedStar := range returnedStars {
if returnedStar != stars[index] {
t.Errorf("Returned star is invalid. Expected %+v. Got %+v instead", stars[index], returnedStar)
}
}
teardown(app)
}
func TestDeleteHandler(t *testing.T) {
app := setup()
// Set up a test table.
starTests := []struct {
star Star
}{
{star: Star{ID: 1, Name: "test/name", Description: "test desc", URL: "test URL"}},
{star: Star{ID: 2, Name: "test/another_name", Description: "test desc 2", URL: "http://example.com/"}},
}
for _, tt := range starTests {
// Create a star for us to delete.
app.DB.Create(tt.star)
// Set up a new request.
req, err := http.NewRequest("DELETE", fmt.Sprintf("/stars/%s", tt.star.Name), nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
// We need a mux router in order to pass in the `name` variable.
r := mux.NewRouter()
r.HandleFunc("/stars/{name:.*}", app.DeleteHandler).Methods("DELETE")
r.ServeHTTP(rr, req)
// Test that the status code is correct.
if status := rr.Code; status != http.StatusNoContent {
t.Errorf("Status code is invalid. Expected %d. Got %d instead", http.StatusNoContent, status)
}
// Test that the star is no longer in the db.
deletedStar := Star{}
app.DB.Where("name = ?", tt.star.Name).First(&deletedStar)
if deletedStar != (Star{}) {
t.Errorf("Star still exists in db: %+v", tt.star)
}
}
teardown(app)
}