-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
334 lines (287 loc) · 8.97 KB
/
handler_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package rip
import (
"bytes"
gjson "encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/dolanor/rip/encoding"
"github.com/dolanor/rip/encoding/json"
"github.com/dolanor/rip/encoding/msgpack"
"github.com/dolanor/rip/encoding/xml"
"github.com/dolanor/rip/encoding/yaml"
)
func TestHandleResourceWithPath(t *testing.T) {
up := newUserProvider()
ro := NewRouteOptions().
WithCodecs(
json.Codec,
yaml.Codec,
msgpack.Codec,
xml.Codec,
)
mux := http.NewServeMux()
mux.HandleFunc(HandleEntities("/users/", up, ro))
s := httptest.NewServer(mux)
u := user{Name: "Jane", BirthDate: time.Date(2009, time.November, 1, 23, 0, 0, 0, time.UTC)}
c := s.Client()
availableCodecs := ro.codecs.Codecs
t.Log(ro.codecs)
for name, codec := range availableCodecs {
var b bytes.Buffer
err := codec.NewEncoder(&b).Encode(u)
panicErr(t, err)
t.Run(name, func(t *testing.T) {
if name == encoding.DefaultCodecKey {
// it's an internal logic, it's not a known mime type
t.Skip("skipping default codec key")
}
t.Run("create", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, s.URL+"/users/", &b)
panicErr(t, err)
req.Header["Content-Type"] = []string{name}
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
b, err := io.ReadAll(resp.Body)
t.Fatalf("post status code is not 201: body: %v: %s", err, string(b))
}
var buf bytes.Buffer
_, err = buf.ReadFrom(resp.Body)
panicErr(t, err)
var uCreated user
err = codec.NewDecoder(&buf).Decode(&uCreated)
panicErr(t, err)
// somehow msgpack package changes the time reference (UTC to CET)
// so we're more graceful with the checks
if uCreated != u && !uCreated.BirthDate.Equal(u.BirthDate) {
t.Fatal("user created != from original:", uCreated, u)
}
})
t.Run("get", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL+"/users/"+u.IDString(), nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("get status code is not 200: %d. body: %v: %s", resp.StatusCode, err, string(b.String()))
}
var uGet user
err = codec.NewDecoder(resp.Body).Decode(&uGet)
panicErr(t, err)
if uGet != u && !uGet.BirthDate.Equal(u.BirthDate) {
t.Fatal("user get != from original:", uGet, u)
}
})
uUpdated := u
t.Run("update", func(t *testing.T) {
uUpdated.BirthDate = uUpdated.BirthDate.Add(2 * time.Hour)
err := codec.NewEncoder(&b).Encode(uUpdated)
panicErr(t, err)
req, err := http.NewRequest(http.MethodPut, s.URL+"/users/"+u.IDString(), &b)
panicErr(t, err)
req.Header["Content-Type"] = []string{name}
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal("updated status code is not 200:", resp.StatusCode)
}
err = codec.NewDecoder(resp.Body).Decode(&uUpdated)
panicErr(t, err)
if uUpdated.BirthDate.Equal(u.BirthDate) {
t.Fatal("updated birthdate not different")
}
})
// Get after update
t.Run("get after update", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL+"/users/"+u.IDString(), nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal("get status code is not 200:", resp.StatusCode)
}
var uGet user
err = codec.NewDecoder(resp.Body).Decode(&uGet)
panicErr(t, err)
if uGet != uUpdated && !uGet.BirthDate.Equal(uUpdated.BirthDate) {
t.Fatal("user get != from original:", uGet, uUpdated)
}
})
t.Run("create other user", func(t *testing.T) {
u := user{Name: "Joe", BirthDate: time.Date(2008, time.November, 1, 23, 0, 0, 0, time.UTC)}
err := codec.NewEncoder(&b).Encode(u)
panicErr(t, err)
req, err := http.NewRequest(http.MethodPost, s.URL+"/users/", &b)
panicErr(t, err)
req.Header["Content-Type"] = []string{name}
req.Header["Accept"] = []string{name}
respCreate, err := c.Do(req)
panicErr(t, err)
defer respCreate.Body.Close()
if respCreate.StatusCode != http.StatusCreated {
t.Fatal("post status code is not 201:", respCreate.StatusCode)
}
var uCreated user
err = codec.NewDecoder(respCreate.Body).Decode(&uCreated)
panicErr(t, err)
if uCreated != u && !uCreated.BirthDate.Equal(u.BirthDate) {
t.Fatal("user created != from original:", uCreated, u)
}
})
t.Run("list", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL+"/users/", nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal("get status code is not 200:", resp.StatusCode)
}
var users []user
dec := codec.NewDecoder(resp.Body)
switch name {
case "application/xml", "text/xml":
// the curren XML impl doesn't create an array of users
// it just streams more values.
// FIXME make XML create a top array value
for {
var user user
err = dec.Decode(&user)
if err == io.EOF {
break
}
panicErr(t, err)
users = append(users, user)
}
default:
err = dec.Decode(&users)
panicErr(t, err)
}
if len(users) != 2 {
t.Fatal("list does not contain 2 elements, contains:", len(users))
}
})
t.Run("delete", func(t *testing.T) {
req, err := http.NewRequest(http.MethodDelete, s.URL+"/users/"+u.IDString(), nil)
panicErr(t, err)
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatal("delete status is not 204:", resp.StatusCode)
}
})
t.Run("get after delete", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL+"/users/"+u.IDString(), nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Fatal("get status code after delete is not 404:", resp.StatusCode)
}
})
t.Run("list after delete", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, s.URL+"/users/", nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal("get status code is not 200:", resp.StatusCode)
}
var users []user
dec := codec.NewDecoder(resp.Body)
switch name {
case "text/xml":
// the current XML impl doesn't create an array of users
// it just streams more values.
// FIXME make XML create a top array value
for {
var user user
err = dec.Decode(&user)
if err == io.EOF {
break
}
panicErr(t, err)
users = append(users, user)
}
default:
err = dec.Decode(&users)
panicErr(t, err)
}
if len(users) != 1 {
t.Fatal("list does not contain 1 element contains:", len(users))
}
})
t.Run("delete again (check idempotency)", func(t *testing.T) {
req, err := http.NewRequest(http.MethodDelete, s.URL+"/users/"+u.IDString(), nil)
panicErr(t, err)
req.Header["Accept"] = []string{name}
resp, err := c.Do(req)
panicErr(t, err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatal("delete status is not 204:", resp.StatusCode)
}
})
})
}
}
func TestMiddleware(t *testing.T) {
up := newUserProvider()
var callNum int
middleware := func(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
callNum++
f(w, r)
}
}
ro := NewRouteOptions().
WithMiddlewares(middleware).
WithCodecs(json.Codec)
mux := http.NewServeMux()
mux.HandleFunc(HandleEntities[*user]("/users/", up, ro))
s := httptest.NewServer(mux)
u := user{Name: "Jane", BirthDate: time.Date(2009, time.November, 1, 23, 0, 0, 0, time.UTC)}
b, err := gjson.Marshal(u)
panicErr(t, err)
c := s.Client()
t.Run("create", func(t *testing.T) {
respCreate, err := c.Post(s.URL+"/users/", "application/json", bytes.NewReader(b))
panicErr(t, err)
defer respCreate.Body.Close()
if respCreate.StatusCode != http.StatusCreated {
t.Fatal("post status code is not 201:", respCreate.StatusCode)
}
var uCreated user
err = gjson.NewDecoder(respCreate.Body).Decode(&uCreated)
panicErr(t, err)
if uCreated != u {
t.Fatal("user created != from original")
}
})
if callNum != 1 {
t.Fatalf("middleware registered %d calls", callNum)
}
}
func panicErr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}