-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathbindform_test.go
More file actions
277 lines (255 loc) · 10.4 KB
/
bindform_test.go
File metadata and controls
277 lines (255 loc) · 10.4 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
package runtime
import (
"bytes"
"mime/multipart"
"net/url"
"testing"
"github.com/oapi-codegen/nullable"
"github.com/oapi-codegen/runtime/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBindURLForm(t *testing.T) {
type testSubStruct struct {
Int int `json:"int"`
String string `json:"string"`
AdditionalProperties map[string]string `json:"-"`
}
type testStruct struct {
Int int `json:"int"`
Bool bool `json:"bool,omitempty"`
String string `json:"string"`
IntSlice []int `json:"int_slice"`
Struct testSubStruct `json:"struct"`
StructSlice []testSubStruct `json:"struct_slice"`
OptInt *int `json:"opt_int,omitempty"`
OptBool *bool `json:"opt_bool,omitempty"`
OptString *string `json:"opt_string,omitempty"`
OptStruct *testSubStruct `json:"opt_struct,omitempty"`
OptStructSlice *[]testSubStruct `json:"opt_struct_slice,omitempty"`
NotSerializable int `json:"-"`
unexported int //nolint:unused
}
testCases := map[string]testStruct{
"int=123": {Int: 123},
"bool=true": {Bool: true},
"string=example": {String: "example"},
"int_slice=1&int_slice=2&int_slice=3": {IntSlice: []int{1, 2, 3}},
"int_slice[]=1&int_slice[]=2&int_slice[]=3": {IntSlice: []int{1, 2, 3}},
"int_slice[2]=3&int_slice[1]=2&int_slice[0]=1": {IntSlice: []int{1, 2, 3}},
"struct[int]=789&struct[string]=abc": {Struct: testSubStruct{Int: 789, String: "abc"}},
"struct_slice[0][int]=3&struct_slice[0][string]=a&struct_slice[1][int]=2&struct_slice[1][string]=b&struct_slice[2][int]=1&struct_slice[2][string]=c": {
StructSlice: []testSubStruct{{Int: 3, String: "a"}, {Int: 2, String: "b"}, {Int: 1, String: "c"}},
},
"opt_int=456": {OptInt: func(v int) *int { return &v }(456)},
"opt_bool=true": {OptBool: func(v bool) *bool { return &v }(true)},
"opt_string=def": {OptString: func(v string) *string { return &v }("def")},
"opt_struct[int]=456&opt_struct[string]=def": {OptStruct: &testSubStruct{Int: 456, String: "def"}},
"opt_struct_slice[0][int]=123&opt_struct_slice[0][string]=abc&opt_struct_slice[1][int]=456&opt_struct_slice[1][string]=def": {
OptStructSlice: &([]testSubStruct{{Int: 123, String: "abc"}, {Int: 456, String: "def"}}),
},
"opt_struct[additional_property]=123": {
OptStruct: &testSubStruct{AdditionalProperties: map[string]string{"additional_property": "123"}},
},
}
for k, v := range testCases {
values, err := url.ParseQuery(k)
assert.NoError(t, err)
var result testStruct
err = BindForm(&result, values, nil, nil)
assert.NoError(t, err)
assert.Equal(t, v, result)
}
}
// TestBindURLFormNullable reproduces oapi-codegen/runtime#129: BindForm fails on
// a nullable.Nullable[T] field because Nullable's underlying type is map[bool]T
// and neither bindFormImpl nor BindStringToObjectWithOptions handles maps.
func TestBindURLFormNullable(t *testing.T) {
type testStruct struct {
Name nullable.Nullable[string] `json:"name"`
Age nullable.Nullable[int] `json:"age"`
}
t.Run("string value is bound", func(t *testing.T) {
values, err := url.ParseQuery("name=alice")
require.NoError(t, err)
var result testStruct
err = BindForm(&result, values, nil, nil)
require.NoError(t, err)
got, err := result.Name.Get()
require.NoError(t, err)
assert.Equal(t, "alice", got)
})
t.Run("int value is bound", func(t *testing.T) {
values, err := url.ParseQuery("age=42")
require.NoError(t, err)
var result testStruct
err = BindForm(&result, values, nil, nil)
require.NoError(t, err)
got, err := result.Age.Get()
require.NoError(t, err)
assert.Equal(t, 42, got)
})
t.Run("missing field stays unspecified", func(t *testing.T) {
var result testStruct
err := BindForm(&result, url.Values{}, nil, nil)
require.NoError(t, err)
assert.False(t, result.Name.IsSpecified())
assert.False(t, result.Age.IsSpecified())
})
}
// TestBindURLFormGenericMap covers binding into a non-Nullable map[K]V field
// using the `name[key]=value` form encoding.
func TestBindURLFormGenericMap(t *testing.T) {
t.Run("string keys, int values", func(t *testing.T) {
type testStruct struct {
Attrs map[string]int `json:"attrs"`
}
values, err := url.ParseQuery("attrs[a]=1&attrs[b]=2")
require.NoError(t, err)
var result testStruct
err = BindForm(&result, values, nil, nil)
require.NoError(t, err)
assert.Equal(t, map[string]int{"a": 1, "b": 2}, result.Attrs)
})
t.Run("int keys, string values", func(t *testing.T) {
type testStruct struct {
Labels map[int]string `json:"labels"`
}
values, err := url.ParseQuery("labels[1]=one&labels[2]=two")
require.NoError(t, err)
var result testStruct
err = BindForm(&result, values, nil, nil)
require.NoError(t, err)
assert.Equal(t, map[int]string{1: "one", 2: "two"}, result.Labels)
})
t.Run("absent map stays nil", func(t *testing.T) {
type testStruct struct {
Attrs map[string]int `json:"attrs"`
}
var result testStruct
err := BindForm(&result, url.Values{}, nil, nil)
require.NoError(t, err)
assert.Nil(t, result.Attrs)
})
}
func TestBindMultipartForm(t *testing.T) {
var testStruct struct {
File types.File `json:"file"`
OptFile *types.File `json:"opt_file,omitempty"`
Files []types.File `json:"files"`
OptFiles *[]types.File `json:"opt_files"`
}
form, err := makeMultipartFilesForm([]fileData{{field: "file", filename: "123.txt", content: []byte("123")}})
assert.NoError(t, err)
err = BindForm(&testStruct, form.Value, form.File, nil)
assert.NoError(t, err)
assert.Equal(t, "123.txt", testStruct.File.Filename())
content, err := testStruct.File.Bytes()
assert.NoError(t, err)
assert.Equal(t, []byte("123"), content)
form, err = makeMultipartFilesForm([]fileData{
{field: "files", filename: "123.pdf", content: []byte("123")},
{field: "files", filename: "456.pdf", content: []byte("456")},
{field: "files", filename: "789.pdf", content: []byte("789")},
})
assert.NoError(t, err)
err = BindForm(&testStruct, form.Value, form.File, nil)
assert.NoError(t, err)
assert.Equal(t, 3, len(testStruct.Files))
assert.Equal(t, "123.pdf", testStruct.Files[0].Filename())
assert.Equal(t, "456.pdf", testStruct.Files[1].Filename())
assert.Equal(t, "789.pdf", testStruct.Files[2].Filename())
form, err = makeMultipartFilesForm([]fileData{{field: "opt_file", filename: "456.png", content: []byte("456")}})
assert.NoError(t, err)
err = BindForm(&testStruct, form.Value, form.File, nil)
assert.NoError(t, err)
assert.Equal(t, "456.png", testStruct.OptFile.Filename())
content, err = testStruct.OptFile.Bytes()
assert.NoError(t, err)
assert.Equal(t, []byte("456"), content)
form, err = makeMultipartFilesForm([]fileData{
{field: "opt_files[2]", filename: "123.pdf", content: []byte("123")},
{field: "opt_files[1]", filename: "456.pdf", content: []byte("456")},
{field: "opt_files[0]", filename: "789.pdf", content: []byte("789")},
})
assert.NoError(t, err)
err = BindForm(&testStruct, form.Value, form.File, nil)
assert.NoError(t, err)
assert.NotNil(t, testStruct.OptFiles)
assert.Equal(t, 3, len(*testStruct.OptFiles))
assert.Equal(t, "789.pdf", (*testStruct.OptFiles)[0].Filename())
assert.Equal(t, "456.pdf", (*testStruct.OptFiles)[1].Filename())
assert.Equal(t, "123.pdf", (*testStruct.OptFiles)[2].Filename())
}
func TestMarshalForm(t *testing.T) {
type testSubStruct struct {
Int int `json:"int"`
String string `json:"string"`
}
type testStruct struct {
Int int `json:"int,omitempty"`
Bool bool `json:"bool,omitempty"`
String string `json:"string,omitempty"`
IntSlice []int `json:"int_slice,omitempty"`
Struct testSubStruct `json:"struct,omitempty"`
StructSlice []testSubStruct `json:"struct_slice,omitempty"`
OptInt *int `json:"opt_int,omitempty"`
OptBool *bool `json:"opt_bool,omitempty"`
OptBoolNullable *bool `json:"opt_bool_nullable"`
OptString *string `json:"opt_string,omitempty"`
OptStruct *testSubStruct `json:"opt_struct,omitempty"`
OptStructSlice *[]testSubStruct `json:"opt_struct_slice,omitempty"`
NotSerializable int `json:"-"`
unexported int //nolint:unused
}
testCases := map[string]testStruct{
"int=123": {Int: 123},
"bool=true": {Bool: true},
"string=example": {String: "example"},
"int_slice[0]=1&int_slice[1]=2&int_slice[2]=3": {IntSlice: []int{1, 2, 3}},
"struct[int]=789&struct[string]=abc": {Struct: testSubStruct{Int: 789, String: "abc"}},
"struct_slice[0][int]=3&struct_slice[0][string]=a&struct_slice[1][int]=2&struct_slice[1][string]=b&struct_slice[2][int]=1&struct_slice[2][string]=c": {
StructSlice: []testSubStruct{{Int: 3, String: "a"}, {Int: 2, String: "b"}, {Int: 1, String: "c"}},
},
"opt_int=456": {OptInt: func(v int) *int { return &v }(456)},
"opt_bool=true": {OptBool: func(v bool) *bool { return &v }(true)},
"": {OptBoolNullable: nil},
"opt_string=def": {OptString: func(v string) *string { return &v }("def")},
"opt_struct[int]=456&opt_struct[string]=def": {OptStruct: &testSubStruct{Int: 456, String: "def"}},
"opt_struct_slice[0][int]=123&opt_struct_slice[0][string]=abc&opt_struct_slice[1][int]=456&opt_struct_slice[1][string]=def": {
OptStructSlice: &([]testSubStruct{{Int: 123, String: "abc"}, {Int: 456, String: "def"}}),
},
}
for k, v := range testCases {
marshaled, err := MarshalForm(v, nil)
assert.NoError(t, err)
encoded, err := url.QueryUnescape(marshaled.Encode())
assert.NoError(t, err)
assert.Equal(t, k, encoded)
}
}
type fileData struct {
field string
filename string
content []byte
}
func makeMultipartFilesForm(files []fileData) (*multipart.Form, error) {
var buffer bytes.Buffer
mw := multipart.NewWriter(&buffer)
for _, file := range files {
w, err := mw.CreateFormFile(file.field, file.filename)
if err != nil {
return nil, err
}
_, err = w.Write(file.content)
if err != nil {
return nil, err
}
}
err := mw.Close()
if err != nil {
return nil, err
}
mr := multipart.NewReader(&buffer, mw.Boundary())
return mr.ReadForm(1024)
}