-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_test.go
326 lines (255 loc) · 7.84 KB
/
decode_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
package formulate
import (
"fmt"
"net/url"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
func TestHTTPDecoder_Decode(t *testing.T) {
description := `This is a long description about the customer
It spans multiple lines`
vals := url.Values{
"Name": {"John Smith"},
"Age": {"25"},
"Email": {"[email protected]"},
"ConfirmedEmail": {"on"},
"Description": {description},
"Password": {"hunter2"},
"Time": {"2020-05-28T15:28"},
"Pet": {"moose"},
"ContactMethod": {"email"},
joinFields("Address", "HouseName"): {"1 Example Road"},
joinFields("Address", "AddressLine1"): {"Fake Town"},
joinFields("Address", "AddressLine2"): {"Fake City"},
joinFields("Address", "Postcode"): {"Postcode"},
joinFields("Address", "TelephoneNumber"): {"012345678910"},
joinFields("Address", "Country"): {"UK"},
joinFields("EmbeddedStruct", "Type"): {"4838374"},
joinFields("EmbeddedStruct", "SomeMultiselect"): {"[]"},
"TestMap": {`{"Foo": "Banana", "baz": "chocolate"}`},
"FavouriteNumber": {"1.222"},
"FavouriteFoods": {"burger", "pizza", "beans"},
"CountryCode": {"GBR"},
"Checkbox": {"0"},
"IgnoredField": {"Content"},
"HiddenInput": {"test"},
}
details := YourDetails{EmbeddedStruct: EmbeddedStruct{SomeMultiselect: []string{"cake"}}}
t.Run("Validation passes", func(t *testing.T) {
dec := NewDecoder(vals)
dec.AddValidators(&minAgeValidator{min: 20})
dec.AddValidators(countryCodeValidator{})
if err := dec.Decode(&details); err != nil {
t.Error(err)
return
}
assertEquals(t, details.Name, "John Smith")
assertEquals(t, details.Age, 25)
assertEquals(t, details.Email, Email("[email protected]"))
assertEquals(t, details.ConfirmedEmail, true)
assertEquals(t, details.Description, description)
assertEquals(t, details.Password, Password("hunter2"))
assertEquals(t, details.Time.Format(time.RFC3339), "2020-05-28T15:28:00Z")
assertEquals(t, details.Pet, Pet("moose"))
assertEquals(t, details.ContactMethod, ContactMethod("email"))
assertEquals(t, details.Address.HouseName, "1 Example Road")
assertEquals(t, details.Address.AddressLine1, "Fake Town")
assertEquals(t, details.Address.AddressLine2, "Fake City")
assertEquals(t, details.Address.Postcode, "Postcode")
assertEquals(t, details.Address.TelephoneNumber, Tel("012345678910"))
assertEquals(t, details.Address.Country, "UK")
assertEquals(t, details.Type, uint32(4838374))
assertEquals(t, details.FavouriteNumber, 1.222)
assertEquals(t, details.Type, uint32(4838374))
assertEquals(t, len(details.SomeMultiselect), 0)
assertEquals(t, len(details.FavouriteFoods), 3)
assertEquals(t, len(details.EmptySliceTest), 0)
assertEquals(t, details.HiddenInput, "test")
// ignored field should not be decoded, as it is hidden by a show condition
assertEquals(t, details.IgnoredField, "")
})
t.Run("Validation fails", func(t *testing.T) {
vals.Set("CountryCode", "uk")
dec := NewDecoder(vals)
store := NewMemoryValidationStore()
dec.SetValidationStore(store)
dec.SetValueOnValidationError(true)
dec.AddValidators(&minAgeValidator{min: 20})
dec.AddValidators(countryCodeValidator{})
if err := dec.Decode(&details); err != ErrFormFailedValidation {
t.Fail()
}
validationErrors, err := store.GetValidationErrors("CountryCode")
if err != nil || len(validationErrors) != 1 {
t.Fail()
}
})
t.Run("Decode on non-ptr type", func(t *testing.T) {
dec := NewDecoder(nil)
out := struct{}{}
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic() on non-ptr type.")
}
}()
_ = dec.Decode(out)
})
t.Run("Decode on non-struct type", func(t *testing.T) {
dec := NewDecoder(nil)
var out int
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic() on non-struct type.")
}
}()
_ = dec.Decode(&out)
})
t.Run("Custom decoder", func(t *testing.T) {
dec := NewDecoder(url.Values{"CustomValue": {"1", "7", "33"}})
var out customDecoderTest
if err := dec.Decode(&out); err != nil {
t.Error(err)
}
if out[0] != 1 && out[1] != 7 && out[2] != 33 {
t.Fail()
}
})
t.Run("Decode into interface{} as struct field", func(t *testing.T) {
type test struct {
Data interface{}
}
x := test{Data: "this is a string"}
dec := NewDecoder(url.Values{"Data": {"new string"}})
if err := dec.Decode(&x); err != nil {
t.Error(err)
}
assertEquals(t, x.Data, "new string")
})
t.Run("Decode without losing value in interface{}", func(t *testing.T) {
type testData struct {
Value string
}
type test struct {
Data interface{}
}
x := test{Data: &testData{Value: "this is a string"}}
dec := NewDecoder(url.Values{})
if err := dec.Decode(&x); err != nil {
t.Error(err)
return
}
if x.Data == nil {
t.Fail()
return
}
v, ok := x.Data.(*testData)
if !ok || v == nil {
t.Fail()
return
}
assertEquals(t, v.Value, "this is a string")
})
t.Run("Decode where empty value overwrites already set value in struct", func(t *testing.T) {
type testData struct {
Value string
}
x := testData{Value: "this is a string"}
dec := NewDecoder(url.Values{"Value": []string{""}})
if err := dec.Decode(&x); err != nil {
t.Error(err)
return
}
if x.Value != "" {
t.Fail()
return
}
})
}
type customDecoderTest []int
func (c customDecoderTest) DecodeFormValue(form url.Values, name string, values []string) (reflect.Value, error) {
var out customDecoderTest
for _, value := range form["CustomValue"] {
i, err := strconv.ParseInt(value, 10, 0)
if err != nil {
return reflect.Value{}, err
}
out = append(out, int(i))
}
return reflect.ValueOf(out), nil
}
type emptySlice []string
func (e emptySlice) DecodeFormValue(form url.Values, name string, values []string) (reflect.Value, error) {
if len(values) == 0 {
return reflect.Value{}, nil
}
return reflect.ValueOf(emptySlice(values)), nil
}
func assertEquals(t *testing.T, a interface{}, b interface{}) {
if a == b {
return
}
_, file, line, ok := runtime.Caller(1)
if ok {
t.Logf("%s:%d : failed to assert that '%v' == '%v'", file, line, a, b)
} else {
t.Logf("failed to assert that '%v' == '%v'", a, b)
}
t.Fail()
}
func joinFields(fields ...string) string {
return strings.Join(fields, fieldSeparator)
}
type minAgeValidator struct {
min int
form url.Values
}
func (m *minAgeValidator) Validate(val interface{}) (ok bool, message string) {
switch b := val.(type) {
case int64:
if int(b) >= m.min {
return true, ""
}
return false, fmt.Sprintf("You must be over %d!", m.min)
default:
return false, "invalid type"
}
}
func (m *minAgeValidator) TagName() string {
return fmt.Sprintf("minAge(%d)", m.min)
}
func (m *minAgeValidator) SetForm(form url.Values) {
m.form = form
}
type countryCodeValidator struct{}
func (c countryCodeValidator) Validate(value interface{}) (ok bool, message string) {
switch a := value.(type) {
case string:
if len(a) == 3 && strings.ToUpper(a) == a {
return true, ""
}
return false, "Country codes must be 3 letters and uppercase"
default:
return false, "invalid type"
}
}
func (c countryCodeValidator) TagName() string {
return "countryCode"
}
func TestHTTPDecoder_SetValidationStore(t *testing.T) {
t.Run("Valid store", func(t *testing.T) {
dec := NewDecoder(nil)
v := NewMemoryValidationStore()
dec.SetValidationStore(v)
assertEquals(t, v, dec.validationStore)
})
t.Run("Nil store", func(t *testing.T) {
dec := NewDecoder(nil)
current := dec.validationStore
dec.SetValidationStore(nil)
assertEquals(t, dec.validationStore, current)
})
}