This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
binding_test.go
400 lines (370 loc) · 14.5 KB
/
binding_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package binding
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type Model struct {
Foo string `json:"foo"`
Bar *string `json:"bar"`
Baz []int `json:"baz"`
Child ChildModel `json:"child"`
Quux *multipart.FileHeader `json:"quux"`
Corge []*multipart.FileHeader `json:"corge"`
}
func (m *Model) FieldMap(req *http.Request) FieldMap {
return FieldMap{
&m.Foo: Field{
Form: "foo",
Required: true,
},
&m.Bar: Field{
Form: "bar",
Required: true,
},
&m.Baz: "baz",
&m.Child: "child",
&m.Quux: "quux",
&m.Corge: "corge",
}
}
type ChildModel struct {
Wibble string `json:"wibble"`
}
func TestBind(t *testing.T) {
Convey("A request", t, func() {
Convey("Without a Content-Type", func() {
Convey("But with a query string", func() {
Convey("With method GET", func() {
Convey("Should invoke the URL binder", func() {
})
})
Convey("With method HEAD", func() {
Convey("Should invoke the URL binder", func() {
})
})
Convey("With unsafe method", func() {
Convey("Should invoke the Form deserializer", nil)
})
})
Convey("With safe method", func() {
Convey("And without a query string", func() {
Convey("Should yield an error", nil)
})
})
})
Convey("With a form-urlencoded Content-Type", func() {
data := url.Values{}
data.Add("foo", "foo-value")
data.Add("child.wibble", "wobble")
data.Add("baz", "1")
data.Add("baz", "2")
data.Add("baz", "3")
req := httptest.NewRequest("POST", "http://www.example.com", strings.NewReader(data.Encode()))
req.Header.Add("Content-type", "application/x-www-form-urlencoded")
Convey("Should invoke the Form deserializer", func() {
model := new(Model)
invoked := false
formBinder = func(req *http.Request, v FieldMapper) Errors {
invoked = true
return defaultFormBinder(req, v)
}
Bind(req, model)
So(invoked, ShouldBeTrue)
formBinder = defaultFormBinder
})
})
Convey("With a multipart/form-data Content-Type", func() {
body := new(bytes.Buffer)
w := multipart.NewWriter(body)
_ = w.WriteField("foo", "foo-value")
_ = w.WriteField("child.wibble", "wobble")
_ = w.WriteField("baz", "1")
_ = w.WriteField("baz", "2")
_ = w.WriteField("baz", "3")
file, _ := w.CreateFormFile("quux", "quux.txt")
_, _ = file.Write([]byte("quux contents"))
file, _ = w.CreateFormFile("corge", "corge1.txt")
_, _ = file.Write([]byte("corge1 contents"))
file, _ = w.CreateFormFile("corge", "corge2.txt")
_, _ = file.Write([]byte("corge2 contents"))
if err := w.Close(); err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "http://www.example.com", body)
So(err, ShouldBeNil)
req.Header.Set("Content-Type", w.FormDataContentType())
Convey("Should invoke the MultipartForm deserializer", func() {
model := new(Model)
invoked := false
multipartFormBinder = func(req *http.Request, v FieldMapper) Errors {
invoked = true
return defaultMultipartFormBinder(req, v)
}
Bind(req, model)
So(invoked, ShouldBeTrue)
multipartFormBinder = defaultMultipartFormBinder
})
Convey("With file data", func() {
model := new(Model)
Bind(req, model)
So(model.Quux.Filename, ShouldEqual, "quux.txt")
So(model.Corge[0].Filename, ShouldEqual, "corge1.txt")
So(model.Corge[1].Filename, ShouldEqual, "corge2.txt")
})
})
Convey("With a json Content-Type", func() {
data := `{ "foo": "foo-value", "child": { "wibble": "wobble" }, "baz": [1,2,3]}`
req, err := http.NewRequest("POST", "http://www.example.com", strings.NewReader(data))
So(err, ShouldBeNil)
req.Header.Add("Content-type", "application/json; charset=utf-8")
Convey("Should invoke Json deserializer", func() {
model := new(Model)
invoked := false
jsonBinder = func(req *http.Request, v FieldMapper) Errors {
invoked = true
return defaultJsonBinder(req, v)
}
Bind(req, model)
So(invoked, ShouldBeTrue)
jsonBinder = defaultJsonBinder
})
})
Convey("With an unsupported Content-Type", func() {
Convey("Should yield an error", nil)
})
})
}
func TestBindForm(t *testing.T) {
Convey("Given a struct all of whose fields are required", t, func() {
actual := AllTypes{}
Convey("Given form data with non-zero values for each of the fields", func() {
expected := NewCompleteModel()
formData := expected.FormValues()
Convey("When bindForm is called", func() {
req, err := http.NewRequest("POST", "http://www.example.com", nil)
So(err, ShouldBeNil)
var errs Errors
errs = bindForm(req, &actual, formData, nil)
Convey("Then all of the struct's fields should be populated", func() {
Convey("Then the Uint8 field should have the expected value", func() {
So(actual.Uint8, ShouldEqual, expected.Uint8)
})
Convey("Then the PointerToUint8 field should have the expected value", func() {
So(*actual.PointerToUint8, ShouldEqual, *expected.PointerToUint8)
})
Convey("Then the Uint8Slice field should have the expected values", func() {
So(len(actual.Uint8Slice), ShouldEqual, len(expected.Uint8Slice))
for i := range actual.Uint8Slice {
So(actual.Uint8Slice[i], ShouldEqual, expected.Uint8Slice[i])
}
})
Convey("Then the Uint16 field should have the expected value", func() {
So(actual.Uint16, ShouldEqual, expected.Uint16)
})
Convey("Then the PointerToUint16 field should have the expected value", func() {
So(*actual.PointerToUint16, ShouldEqual, *expected.PointerToUint16)
})
Convey("Then the Uint16Slice field should have the expected values", func() {
So(len(actual.Uint16Slice), ShouldEqual, len(expected.Uint16Slice))
for i := range actual.Uint16Slice {
So(actual.Uint16Slice[i], ShouldEqual, expected.Uint16Slice[i])
}
})
Convey("Then the Uint32 field should have the expected value", func() {
So(actual.Uint32, ShouldEqual, expected.Uint32)
})
Convey("Then the PointerToUint32 field should have the expected value", func() {
So(*actual.PointerToUint32, ShouldEqual, *expected.PointerToUint32)
})
Convey("Then the Uint32Slice field should have the expected values", func() {
So(len(actual.Uint32Slice), ShouldEqual, len(expected.Uint32Slice))
for i := range actual.Uint32Slice {
So(actual.Uint32Slice[i], ShouldEqual, expected.Uint32Slice[i])
}
})
Convey("Then the Uint64 field should have the expected value", func() {
So(actual.Uint64, ShouldEqual, expected.Uint64)
})
Convey("Then the PointerToUint64 field should have the expected value", func() {
So(*actual.PointerToUint64, ShouldEqual, *expected.PointerToUint64)
})
Convey("Then the Uint64Slice field should have the expected values", func() {
So(len(actual.Uint64Slice), ShouldEqual, len(expected.Uint64Slice))
for i := range actual.Uint64Slice {
So(actual.Uint64Slice[i], ShouldEqual, expected.Uint64Slice[i])
}
})
Convey("Then the Int8 field should have the expected value", func() {
So(actual.Int8, ShouldEqual, expected.Int8)
})
Convey("Then the PointerToInt8 field should have the expected value", func() {
So(*actual.PointerToInt8, ShouldEqual, *expected.PointerToInt8)
})
Convey("Then the Int8Slice field should have the expected values", func() {
So(len(actual.Int8Slice), ShouldEqual, len(expected.Int8Slice))
for i := range actual.Int8Slice {
So(actual.Int8Slice[i], ShouldEqual, expected.Int8Slice[i])
}
})
Convey("Then the Int16 field should have the expected value", func() {
So(actual.Int16, ShouldEqual, expected.Int16)
})
Convey("Then the PointerToInt16 field should have the expected value", func() {
So(*actual.PointerToInt16, ShouldEqual, *expected.PointerToInt16)
})
Convey("Then the Int16Slice field should have the expected values", func() {
So(len(actual.Int16Slice), ShouldEqual, len(expected.Int16Slice))
for i := range actual.Int16Slice {
So(actual.Int16Slice[i], ShouldEqual, expected.Int16Slice[i])
}
})
Convey("Then the Int32 field should have the expected value", func() {
So(actual.Int32, ShouldEqual, expected.Int32)
})
Convey("Then the PointerToInt32 field should have the expected value", func() {
So(*actual.PointerToInt32, ShouldEqual, *expected.PointerToInt32)
})
Convey("Then the Int32Slice field should have the expected values", func() {
So(len(actual.Int32Slice), ShouldEqual, len(expected.Int32Slice))
for i := range actual.Int32Slice {
So(actual.Int32Slice[i], ShouldEqual, expected.Int32Slice[i])
}
})
Convey("Then the Int64 field should have the expected value", func() {
So(actual.Int64, ShouldEqual, expected.Int64)
})
Convey("Then the PointerToInt64 field should have the expected value", func() {
So(*actual.PointerToInt64, ShouldEqual, *expected.PointerToInt64)
})
Convey("Then the Int64Slice field should have the expected values", func() {
So(len(actual.Int64Slice), ShouldEqual, len(expected.Int64Slice))
for i := range actual.Int64Slice {
So(actual.Int64Slice[i], ShouldEqual, expected.Int64Slice[i])
}
})
Convey("Then the Float32 field should have the expected value", func() {
So(actual.Float32, ShouldEqual, expected.Float32)
})
Convey("Then the PointerToFloat32 field should have the expected value", func() {
So(*actual.PointerToFloat32, ShouldEqual, *expected.PointerToFloat32)
})
Convey("Then the Float32Slice field should have the expected values", func() {
So(len(actual.Float32Slice), ShouldEqual, len(expected.Float32Slice))
for i := range actual.Float32Slice {
So(actual.Float32Slice[i], ShouldEqual, expected.Float32Slice[i])
}
})
Convey("Then the Float64 field should have the expected value", func() {
So(actual.Float64, ShouldEqual, expected.Float64)
})
Convey("Then the PointerToFloat64 field should have the expected value", func() {
So(*actual.PointerToFloat64, ShouldEqual, *expected.PointerToFloat64)
})
Convey("Then the Float64Slice field should have the expected values", func() {
So(len(actual.Float64Slice), ShouldEqual, len(expected.Float64Slice))
for i := range actual.Float64Slice {
So(actual.Float64Slice[i], ShouldEqual, expected.Float64Slice[i])
}
})
Convey("Then the Uint field should have the expected value", func() {
So(actual.Uint, ShouldEqual, expected.Uint)
})
Convey("Then the PointerToUint field should have the expected value", func() {
So(*actual.PointerToUint, ShouldEqual, *expected.PointerToUint)
})
Convey("Then the UintSlice field should have the expected values", func() {
So(len(actual.UintSlice), ShouldEqual, len(expected.UintSlice))
for i := range actual.UintSlice {
So(actual.UintSlice[i], ShouldEqual, expected.UintSlice[i])
}
})
Convey("Then the Int field should have the expected value", func() {
So(actual.Int, ShouldEqual, expected.Int)
})
Convey("Then the PointerToInt field should have the expected value", func() {
So(*actual.PointerToInt, ShouldEqual, *expected.PointerToInt)
})
Convey("Then the IntSlice field should have the expected values", func() {
So(len(actual.IntSlice), ShouldEqual, len(expected.IntSlice))
for i := range actual.IntSlice {
So(actual.IntSlice[i], ShouldEqual, expected.IntSlice[i])
}
})
Convey("Then the Bool field should have the expected value", func() {
So(actual.Bool, ShouldEqual, expected.Bool)
})
Convey("Then the PointerToBool field should have the expected value", func() {
So(*actual.PointerToBool, ShouldEqual, *expected.PointerToBool)
})
Convey("Then the BoolSlice field should have the expected values", func() {
So(len(actual.BoolSlice), ShouldEqual, len(expected.BoolSlice))
for i := range actual.BoolSlice {
So(actual.BoolSlice[i], ShouldEqual, expected.BoolSlice[i])
}
})
Convey("Then the String field should have the expected value", func() {
So(actual.String, ShouldEqual, expected.String)
})
Convey("Then the PointerToString field should have the expected value", func() {
So(*actual.PointerToString, ShouldEqual, *expected.PointerToString)
})
Convey("Then the StringSlice field should have the expected values", func() {
So(len(actual.StringSlice), ShouldEqual, len(expected.StringSlice))
for i := range actual.StringSlice {
So(actual.StringSlice[i], ShouldEqual, expected.StringSlice[i])
}
})
Convey("Then the Time field should have the expected value", func() {
So(actual.Time.Equal(expected.Time), ShouldBeTrue)
})
Convey("Then the PointerToTime field should have the expected value", func() {
So((*actual.PointerToTime).Equal(*expected.PointerToTime), ShouldBeTrue)
})
Convey("Then the TimeSlice field should have the expected values", func() {
So(len(actual.TimeSlice), ShouldEqual, len(expected.TimeSlice))
for i := range actual.TimeSlice {
So(actual.TimeSlice[i].Equal(expected.TimeSlice[i]), ShouldBeTrue)
}
})
})
Convey("Then no errors should be produced", FailureContinues, func() {
So(errs.Len(), ShouldEqual, 0)
if errs.Len() > 0 {
for _, e := range errs {
t.Logf("%v. %s", e.Fields(), e.Error())
}
}
})
})
})
Convey("Given form data with zero values for each of the fields", func() {
Convey("When bindForm is called", func() {
req, err := http.NewRequest("POST", "http://www.example.com", nil)
So(err, ShouldBeNil)
errs := bindForm(req, &actual, map[string][]string{}, nil)
Convey("Then none of the struct's fields should be populated", func() {
expected := AllTypes{}
So(reflect.DeepEqual(actual, expected), ShouldBeTrue)
})
Convey("Then an error for each field should be produced", FailureContinues, func() {
fields := make(map[string]struct{})
for _, f := range actual.FieldMap(nil) {
fields[f.(Field).Form] = struct{}{}
}
for _, err := range errs {
So(len(err.Fields()), ShouldEqual, 1)
_, ok := fields[err.Fields()[0]]
So(ok, ShouldBeTrue)
}
})
})
})
})
}