-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_default_test.go
More file actions
299 lines (240 loc) · 9.09 KB
/
Copy pathrequest_default_test.go
File metadata and controls
299 lines (240 loc) · 9.09 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ============ default tag tests ============
type defaultBasicStruct struct {
Name string `default:"alice"`
Age int `default:"42"`
}
func TestDefaultTag_Basic(t *testing.T) {
captured, rec := doRequest[defaultBasicStruct](t, captureHandler[defaultBasicStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "alice", captured.request.Name, "Name")
assert.Equal(t, 42, captured.request.Age, "Age")
}
type defaultMultipleStruct struct {
A string `default:"foo"`
B int `default:"100"`
C bool `default:"true"`
}
func TestDefaultTag_MultipleFields(t *testing.T) {
captured, rec := doRequest[defaultMultipleStruct](t, captureHandler[defaultMultipleStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "foo", captured.request.A, "A")
assert.Equal(t, 100, captured.request.B, "B")
assert.Equal(t, true, captured.request.C, "C")
}
type defaultOverriddenStruct struct {
Name string `default:"alice" query:"name"`
Age int `default:"42" query:"age"`
}
func TestDefaultTag_OverriddenByRequest(t *testing.T) {
captured, rec := doRequest[defaultOverriddenStruct](t, captureHandler[defaultOverriddenStruct],
http.MethodGet, "/?name=bob&age=99")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "bob", captured.request.Name, "Name")
assert.Equal(t, 99, captured.request.Age, "Age")
}
func TestDefaultTag_RequestOverridesPartially(t *testing.T) {
captured, rec := doRequest[defaultOverriddenStruct](t, captureHandler[defaultOverriddenStruct],
http.MethodGet, "/?name=bob")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "bob", captured.request.Name, "Name")
assert.Equal(t, 42, captured.request.Age, "Age (default)")
}
// ============ default tag: string-specific tests ============
type defaultStringMultiWordStruct struct {
Value string `default:"hello world"`
}
func TestDefaultTag_StringWithSpaces(t *testing.T) {
captured, rec := doRequest[defaultStringMultiWordStruct](t, captureHandler[defaultStringMultiWordStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "hello world", captured.request.Value)
}
type defaultStringWithSpecialCharsStruct struct {
Value string `default:"a=b&c d:e[f]"`
}
func TestDefaultTag_StringWithSpecialChars(t *testing.T) {
captured, rec := doRequest[defaultStringWithSpecialCharsStruct](t, captureHandler[defaultStringWithSpecialCharsStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "a=b&c d:e[f]", captured.request.Value)
}
type defaultEmptyStringStruct struct {
Value string `default:""`
}
func TestDefaultTag_EmptyString(t *testing.T) {
captured, rec := doRequest[defaultEmptyStringStruct](t, captureHandler[defaultEmptyStringStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "", captured.request.Value)
}
// ============ default tag: numeric edge cases ============
type defaultNumericEdgeStruct struct {
NegInt int `default:"-42"`
NegFloat float64 `default:"-3.14"`
BigInt int64 `default:"9223372036854775807"`
PosUint uint `default:"42"`
}
func TestDefaultTag_NumericEdgeCases(t *testing.T) {
captured, rec := doRequest[defaultNumericEdgeStruct](t, captureHandler[defaultNumericEdgeStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, -42, captured.request.NegInt)
assert.Equal(t, -3.14, captured.request.NegFloat)
assert.Equal(t, int64(9223372036854775807), captured.request.BigInt)
assert.Equal(t, uint(42), captured.request.PosUint)
}
type defaultBoolLikeValues struct {
A bool `default:"true"`
B bool `default:"false"`
C bool `default:"1"`
D bool `default:"0"`
}
func TestDefaultTag_BoolLikeValues(t *testing.T) {
captured, rec := doRequest[defaultBoolLikeValues](t, captureHandler[defaultBoolLikeValues], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, true, captured.request.A)
assert.Equal(t, false, captured.request.B)
assert.Equal(t, true, captured.request.C)
assert.Equal(t, false, captured.request.D)
}
type defaultInvalidIntFormat struct {
Age int `default:"123abc"`
}
func TestDefaultTag_InvalidIntFormat_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultInvalidIntFormat])
})
}
type defaultInvalidBoolFormat struct {
Flag bool `default:"yes"`
}
func TestDefaultTag_InvalidBoolFormat_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultInvalidBoolFormat])
})
}
type defaultInvalidUintStruct struct {
V uint `default:"not-a-number"`
}
func TestDefaultTag_InvalidUint_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultInvalidUintStruct])
})
}
type defaultInvalidFloatStruct struct {
V float64 `default:"not-a-number"`
}
func TestDefaultTag_InvalidFloat_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultInvalidFloatStruct])
})
}
type defaultInvalidComplexStruct struct {
C complex64 `default:"not-a-complex"`
}
func TestDefaultTag_InvalidComplex_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultInvalidComplexStruct])
})
}
// ============ default tag: TextUnmarshaler ============
type defaultTextUnmarshalerStruct struct {
Value textUnmarshalerType `default:"hello"`
}
type textUnmarshalerType string
func (t *textUnmarshalerType) UnmarshalText(text []byte) error {
*t = textUnmarshalerType("unmarshaled:" + string(text))
return nil
}
func TestDefaultTag_TextUnmarshaler(t *testing.T) {
captured, rec := doRequest[defaultTextUnmarshalerStruct](t, captureHandler[defaultTextUnmarshalerStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, textUnmarshalerType("unmarshaled:hello"), captured.request.Value)
}
type defaultTextUnmarshalerStructEmptyDefault struct {
Value textUnmarshalerType `default:""`
}
func TestDefaultTag_TextUnmarshaler_EmptyString(t *testing.T) {
captured, rec := doRequest[defaultTextUnmarshalerStructEmptyDefault](t, captureHandler[defaultTextUnmarshalerStructEmptyDefault], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, textUnmarshalerType("unmarshaled:"), captured.request.Value)
}
// ============ default tag: MapstructureUnmarshaler ============
type mapstructureUnmarshalerType struct {
Value string
}
func (t *mapstructureUnmarshalerType) UnmarshalMapstructure(value any) error {
if s, ok := value.(string); ok {
t.Value = "mapstruct:" + s
return nil
}
return fmt.Errorf("expected string, got %T", value)
}
type defaultMapstructureUnmarshalerStruct struct {
Value mapstructureUnmarshalerType `default:"world"`
}
func TestDefaultTag_MapstructureUnmarshaler(t *testing.T) {
captured, rec := doRequest[defaultMapstructureUnmarshalerStruct](t, captureHandler[defaultMapstructureUnmarshalerStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "mapstruct:world", captured.request.Value.Value)
}
// ============ default tag: unknown type panics ============
type defaultUnknownTypeStruct struct {
Value struct{ Name string } `default:"anything"`
}
func TestDefaultTag_UnknownType_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[defaultUnknownTypeStruct])
})
}
// ============ default tag: complex numbers ============
type defaultComplexStruct struct {
C64 complex64 `default:"1+2i"`
C128 complex128 `default:"3+4i"`
}
func TestDefaultTag_ComplexNumbers(t *testing.T) {
captured, rec := doRequest[defaultComplexStruct](t, captureHandler[defaultComplexStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, complex64(1+2i), captured.request.C64, "C64")
assert.Equal(t, 3+4i, captured.request.C128, "C128")
}
// ============ default tag: no aliasing between requests ============
type sliceDefaultType []string
func (s *sliceDefaultType) UnmarshalText(text []byte) error {
*s = strings.Split(string(text), ",")
return nil
}
type defaultSliceStruct struct {
Items sliceDefaultType `default:"a,b,c"`
}
func TestDefaultTag_NoAliasing_BetweenRequests(t *testing.T) {
var secondRequestItems sliceDefaultType
callCount := 0
handler := asTestHTTPHandler(RequestParser(func(ctx *Context, req defaultSliceStruct) {
callCount++
if callCount == 1 {
req.Items[0] = "MUTATED"
} else {
secondRequestItems = req.Items
}
ctx.NewResponse(http.StatusOK)
}))
req1, _ := http.NewRequest(http.MethodGet, "/", nil)
handler.ServeHTTP(httptest.NewRecorder(), req1)
req2, _ := http.NewRequest(http.MethodGet, "/", nil)
handler.ServeHTTP(httptest.NewRecorder(), req2)
assert.Equal(t, 2, callCount)
assert.Equal(t, sliceDefaultType{"a", "b", "c"}, secondRequestItems,
"second request should see fresh default, not mutated value (no aliasing)")
}