-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_test.go
More file actions
502 lines (422 loc) · 16.1 KB
/
handlers_test.go
File metadata and controls
502 lines (422 loc) · 16.1 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package rv_test
import (
"fmt"
"math"
"strings"
"time"
"github.com/OwnLocal/rv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type mockHandler struct {
called int
values []interface{}
errs []error
}
func (h *mockHandler) Run(req rv.Request, field *rv.Field) {
h.values = append(h.values, field.Value)
h.called++
for _, err := range h.errs {
field.Errors = append(field.Errors, err)
}
}
var _ = Describe("Validators", func() {
var req *rv.BasicRequest
var field *rv.Field
BeforeEach(func() {
req = &rv.BasicRequest{
Path: map[string]string{"foo": "bar"},
Query: "bar=baz&blah=flah"}
field = new(rv.Field)
})
Describe("SourceFieldHandler", func() {
Describe("NewSourceFieldHandler", func() {
It("rejects invalid sources", func() {
_, err := rv.NewSourceFieldHandler([]string{"foo.bar"})
Expect(err).To(HaveOccurred())
})
It("accepts valid sources", func() {
Expect(rv.NewSourceFieldHandler([]string{"path.foo"})).To(Equal(rv.SourceFieldHandler{Source: rv.PATH, Field: "foo"}))
Expect(rv.NewSourceFieldHandler([]string{"query.foo"})).To(Equal(rv.SourceFieldHandler{Source: rv.QUERY, Field: "foo"}))
Expect(rv.NewSourceFieldHandler([]string{"json.foo"})).To(Equal(rv.SourceFieldHandler{Source: rv.JSON, Field: "foo"}))
Expect(rv.NewSourceFieldHandler([]string{"form.foo"})).To(Equal(rv.SourceFieldHandler{Source: rv.FORM, Field: "foo"}))
})
})
Describe("Run", func() {
It("properly pulls fields from path arguments", func() {
rv.SourceFieldHandler{Source: rv.PATH, Field: "foo"}.Run(req, field)
Expect(field.Value).To(Equal("bar"))
})
It("properly pulls fields from query arguments", func() {
rv.SourceFieldHandler{Source: rv.QUERY, Field: "blah"}.Run(req, field)
Expect(field.Value).To(Equal("flah"))
})
It("properly pulls fields from JSON body arguments", func() {
req.Body = `{"one": 2}`
rv.SourceFieldHandler{Source: rv.JSON, Field: "one"}.Run(req, field)
Expect(field.Value).To(Equal(2.0))
})
It("properly pulls fields from form body arguments", func() {
req.Body = `one=two&three=four`
rv.SourceFieldHandler{Source: rv.FORM, Field: "three"}.Run(req, field)
Expect(field.Value).To(Equal("four"))
})
})
})
Describe("TypeHandler", func() {
Describe("NewTypeHandler", func() {
for _, typeName := range strings.Split("bool int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float32 float64 string", " ") {
It(fmt.Sprintf("accepts %s type", typeName), func() {
Expect(rv.NewTypeHandler([]string{typeName})).To(Equal(rv.TypeHandler{Type: typeName}))
})
}
for _, typeName := range strings.Split("uintptr complex64 complex128 array func interface map ptr slice struct unsafe.Pointer", " ") {
It(fmt.Sprintf("doesn't accept %s type yet", typeName), func() {
_, err := rv.NewTypeHandler([]string{typeName})
Expect(err).To(HaveOccurred())
})
}
})
Describe("Run", func() {
type tm struct {
ttype string
from interface{}
to interface{}
}
for _, tc := range []tm{
tm{"bool", true, true},
tm{"bool", "1", true},
tm{"bool", "t", true},
tm{"bool", "T", true},
tm{"bool", "TRUE", true},
tm{"bool", "true", true},
tm{"bool", "True", true},
tm{"bool", false, false},
tm{"bool", "0", false},
tm{"bool", "f", false},
tm{"bool", "F", false},
tm{"bool", "FALSE", false},
tm{"bool", "false", false},
tm{"bool", "False", false},
tm{"bool", 1, true},
tm{"bool", 0, false},
tm{"bool", uint(1), true},
tm{"bool", uint(0), false},
tm{"int", "42", 42},
tm{"int", 42, 42},
tm{"int8", int64(42), int8(42)},
tm{"int16", int8(42), int16(42)},
tm{"int32", int(42), int32(42)},
tm{"int64", int32(42), int64(42)},
tm{"int", "-42", -42},
tm{"int", -42, -42},
tm{"int8", int64(-42), int8(-42)},
tm{"int16", int8(-42), int16(-42)},
tm{"int32", int(-42), int32(-42)},
tm{"int64", int32(-42), int64(-42)},
tm{"uint", "42", uint(42)},
tm{"uint", 42, uint(42)},
tm{"uint8", int64(42), uint8(42)},
tm{"uint16", int8(42), uint16(42)},
tm{"uint32", int(42), uint32(42)},
tm{"uint64", int32(42), uint64(42)},
tm{"uint8", uint64(42), uint8(42)},
tm{"uint16", uint8(42), uint16(42)},
tm{"uint32", uint(42), uint32(42)},
tm{"uint64", uint32(42), uint64(42)},
tm{"float32", 42.0, float32(42.0)},
tm{"string", "yarp", "yarp"},
tm{"string", false, "false"},
tm{"string", true, "true"},
tm{"string", int64(-4), "-4"},
tm{"string", int32(-3), "-3"},
tm{"string", int16(-2), "-2"},
tm{"string", int8(-1), "-1"},
tm{"string", 0, "0"},
tm{"string", uint8(1), "1"},
tm{"string", uint16(2), "2"},
tm{"string", uint32(3), "3"},
tm{"string", uint64(4), "4"},
tm{"string", float32(5.0), "5"},
tm{"string", float32(6.1), "6.1"},
tm{"string", float64(7.0), "7"},
tm{"string", float64(8.1), "8.1"},
tm{"time", "2015-01-01", time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC)},
tm{"time", "2015-01-01T12:13", time.Date(2015, time.January, 1, 12, 13, 0, 0, time.UTC)},
tm{"time", "2015-01-01T12:13:14", time.Date(2015, time.January, 1, 12, 13, 14, 0, time.UTC)},
tm{"time", "2015-01-01T12:13:14.15", time.Date(2015, time.January, 1, 12, 13, 14, 150000000, time.UTC)},
tm{"time", "2015-01-01T12:13:14-07:00", time.Date(2015, time.January, 1, 12, 13, 14, 0, time.FixedZone("", -25200))},
} {
ttype, from, to := tc.ttype, tc.from, tc.to
It(fmt.Sprintf("coerces %T(%#v) to %s(%#v)", from, from, ttype, to), func() {
field.Value = from
rv.TypeHandler{Type: ttype}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
Expect(field.Value).To(Equal(to))
})
}
for _, tc := range []tm{
tm{"string", nil, nil},
tm{"bool", nil, nil},
tm{"int", nil, nil},
tm{"uint", nil, nil},
tm{"float32", nil, nil},
} {
ttype, from := tc.ttype, tc.from
It("does not set value if incoming value is nil", func() {
field.Value = from
rv.TypeHandler{Type: ttype}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
Expect(field.Value).To(BeNil())
})
}
for _, tc := range []tm{
tm{"bool", "foobly", nil},
tm{"bool", "42", nil},
tm{"bool", 42, nil},
tm{"bool", uint(42), nil},
tm{"int", "arrr", nil},
tm{"int8", uint64(42), nil},
tm{"int16", uint8(42), nil},
tm{"int32", uint(42), nil},
tm{"int64", uint32(42), nil},
tm{"int", uint64(math.MaxUint64), nil},
tm{"int8", int64(256), nil},
tm{"int32", uint64(math.MaxUint64), nil},
tm{"int64", uint64(math.MaxUint64), nil},
tm{"int8", int64(math.MinInt64), nil},
tm{"int16", int64(math.MinInt64), nil},
tm{"int32", int64(math.MinInt64), nil},
tm{"uint", "-42", nil},
tm{"uint", -42, nil},
tm{"uint8", int64(-42), nil},
tm{"uint16", int8(-42), nil},
tm{"uint32", int(-42), nil},
tm{"uint64", int32(-42), nil},
tm{"float32", "blar", nil},
tm{"float64", "blar", nil},
} {
ttype, from := tc.ttype, tc.from
It(fmt.Sprintf("cannot coerce %T(%v) to %s", from, from, ttype), func() {
field.Value = from
rv.TypeHandler{Type: ttype}.Run(req, field)
Expect(field.Value).To(Equal(from))
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
}
})
})
Describe("DefaultHandler", func() {
Describe("NewDefaultHandler", func() {
It("has a single value if it gets a single argument", func() {
h, err := rv.NewDefaultHandler([]string{"one"})
Expect(err).NotTo(HaveOccurred())
Expect(h.(rv.DefaultHandler).Default).To(Equal("one"))
})
It("has a slice of values if it gets a multiple arguments", func() {
h, err := rv.NewDefaultHandler([]string{"one", "two", "three"})
Expect(err).NotTo(HaveOccurred())
Expect(h.(rv.DefaultHandler).Default).To(Equal([]string{"one", "two", "three"}))
})
})
Describe("Run", func() {
It("does nothing if there is already a value set", func() {
field.Value = "already set"
rv.DefaultHandler{Default: "not set"}.Run(req, field)
Expect(field.Value).To(Equal("already set"))
})
It("sets the value to the default there is no value set", func() {
rv.DefaultHandler{Default: "not set"}.Run(req, field)
Expect(field.Value).To(Equal("not set"))
})
})
})
Describe("RangeHandler", func() {
Describe("Run", func() {
It("returns no error if value is in range", func() {
field.Value = 5
rv.RangeHandler{Start: "1", End: "10"}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
It("works with uint values", func() {
field.Value = uint32(5)
rv.RangeHandler{Start: "1", End: "10"}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
It("works with floating point values", func() {
field.Value = 5.5
rv.RangeHandler{Start: "1", End: "10"}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
It("works with string values", func() {
field.Value = "abc"
rv.RangeHandler{Start: "aaa", End: "ddd"}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
It("returns an error for out-of-range string values", func() {
field.Value = "zzz"
rv.RangeHandler{Start: "aaa", End: "ddd"}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
It("returns an error if there is no value", func() {
rv.RangeHandler{Start: "1", End: "10"}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
It("returns an error if the value is out of range", func() {
field.Value = -1
rv.RangeHandler{Start: "1", End: "10"}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
It("returns an error if the range is not valid", func() {
field.Value = 5
rv.RangeHandler{Start: "one", End: "10"}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
})
})
Describe("OptionsHandler", func() {
y := struct{}{}
Describe("Run", func() {
It("returns no error if value is in options", func() {
field.Value = "two"
rv.OptionsHandler{Options: map[string]struct{}{"one": y, "two": y, "three": y}}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
It("returns an error if value is not in options", func() {
field.Value = "five"
rv.OptionsHandler{Options: map[string]struct{}{"one": y, "two": y, "three": y}}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
It("works on ints", func() {
field.Value = 5
rv.OptionsHandler{Options: map[string]struct{}{"1": y, "2": y, "3": y}}.Run(req, field)
Expect(field.Errors).ToNot(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
It("works on floats", func() {
field.Value = 2.2
rv.OptionsHandler{Options: map[string]struct{}{"1.1": y, "2.2": y, "3.3": y}}.Run(req, field)
Expect(field.Errors).To(BeEmpty())
})
})
})
Describe("ListHandler", func() {
Describe("Run", func() {
var handler rv.ListHandler
Context("When the supplied field value is a comma-separated string", func() {
BeforeEach(func() {
handler = rv.ListHandler{SubHandlers: rv.FieldHandlers{
&mockHandler{errs: []error{fmt.Errorf("ow")}},
&mockHandler{},
}}
field.Value = "one,two,three"
handler.Run(req, field)
})
It("splits string arguments and calls each sub-handler with each one", func() {
Expect(handler.SubHandlers[0].(*mockHandler).called).To(Equal(3))
Expect(handler.SubHandlers[0].(*mockHandler).values).To(Equal([]interface{}{"one", "two", "three"}))
Expect(handler.SubHandlers[1].(*mockHandler).called).To(Equal(3))
Expect(handler.SubHandlers[1].(*mockHandler).values).To(Equal([]interface{}{"one", "two", "three"}))
})
It("sets the field value to a list of the types specified", func() {
Expect(field.Value).To(Equal([]string{"one", "two", "three"}))
})
It("gathers errors returned by handlers into the top-level field", func() {
ow := fmt.Errorf("ow")
Expect(field.Errors).To(Equal([]error{ow, ow, ow}))
})
})
Context("When the supplied field is already a slice of strings", func() {
BeforeEach(func() {
handler = rv.ListHandler{SubHandlers: rv.FieldHandlers{
&mockHandler{errs: []error{fmt.Errorf("ow")}},
}}
field.Value = []string{"one", "two", "three"}
handler.Run(req, field)
})
It("still works as expected", func() {
Expect(handler.SubHandlers[0].(*mockHandler).called).To(Equal(3))
Expect(handler.SubHandlers[0].(*mockHandler).values).To(Equal([]interface{}{"one", "two", "three"}))
Expect(field.Value).To(Equal([]string{"one", "two", "three"}))
ow := fmt.Errorf("ow")
Expect(field.Errors).To(Equal([]error{ow, ow, ow}))
})
})
Context("When the supplied field is a single int", func() {
BeforeEach(func() {
handler = rv.ListHandler{SubHandlers: rv.FieldHandlers{
&mockHandler{errs: []error{fmt.Errorf("ow")}},
}}
field.Value = 42
handler.Run(req, field)
})
It("calls the SubHandlers for that item as if it were in a slice", func() {
Expect(handler.SubHandlers[0].(*mockHandler).called).To(Equal(1))
Expect(handler.SubHandlers[0].(*mockHandler).values).To(Equal([]interface{}{42}))
Expect(field.Value).To(Equal([]int{42}))
Expect(field.Errors).To(Equal([]error{fmt.Errorf("ow")}))
})
})
Context("When there is a type sub-handler", func() {
BeforeEach(func() {
handler = rv.ListHandler{SubHandlers: rv.FieldHandlers{
rv.TypeHandler{Type: "int"},
}}
})
It("will transform a comma-separated string with ints into a slice of ints", func() {
field.Value = "1,2,3"
handler.Run(req, field)
Expect(field.Value).To(Equal([]int{1, 2, 3}))
})
It("will transform a slice of strings into a slice of ints", func() {
field.Value = []string{"1", "2", "3"}
handler.Run(req, field)
Expect(field.Value).To(Equal([]int{1, 2, 3}))
})
It("will transform a slice of ints into a slice of strings", func() {
field.Value = []int{1, 2, 3}
handler = rv.ListHandler{SubHandlers: rv.FieldHandlers{
rv.TypeHandler{Type: "string"},
}}
handler.Run(req, field)
Expect(field.Value).To(Equal([]string{"1", "2", "3"}))
})
})
})
})
Describe("RequiredHandler", func() {
Describe("NewRequiredHandler", func() {
It("accepts string forms of bools as in strconv.ParseBool", func() {
Expect(rv.NewRequiredHandler([]string{"1"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"t"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"T"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"TRUE"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"true"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"True"})).To(Equal(rv.RequiredHandler{Required: true}))
Expect(rv.NewRequiredHandler([]string{"0"})).To(Equal(rv.RequiredHandler{Required: false}))
Expect(rv.NewRequiredHandler([]string{"f"})).To(Equal(rv.RequiredHandler{Required: false}))
Expect(rv.NewRequiredHandler([]string{"F"})).To(Equal(rv.RequiredHandler{Required: false}))
Expect(rv.NewRequiredHandler([]string{"FALSE"})).To(Equal(rv.RequiredHandler{Required: false}))
Expect(rv.NewRequiredHandler([]string{"false"})).To(Equal(rv.RequiredHandler{Required: false}))
Expect(rv.NewRequiredHandler([]string{"False"})).To(Equal(rv.RequiredHandler{Required: false}))
})
It("rejects strings that don't represent bools", func() {
_, err := rv.NewRequiredHandler([]string{"dksjfsdds"})
Expect(err).To(HaveOccurred())
})
})
Describe("Run", func() {
It("adds an error if a required field is not present", func() {
rv.RequiredHandler{Required: true}.Run(req, field)
Expect(field.Errors).NotTo(BeEmpty())
Expect(field.Errors[0]).To(HaveOccurred())
})
})
})
})