-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
239 lines (212 loc) · 4.71 KB
/
utils_test.go
File metadata and controls
239 lines (212 loc) · 4.71 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
package httpx
import (
"encoding"
"fmt"
"net/url"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// CustomType implements encoding.TextUnmarshaler
type CustomType struct {
Value string
}
func (c *CustomType) UnmarshalText(text []byte) error {
c.Value = "custom:" + string(text)
return nil
}
type Address struct {
City string `query:"city"`
ZipCode int `query:"zip"`
}
// CSVInts implements encoding.TextUnmarshaler
type CSVInts []int
func (c *CSVInts) UnmarshalText(b []byte) error {
parts := strings.Split(string(b), ",")
out := make([]int, 0, len(parts))
for _, p := range parts {
i, err := strconv.Atoi(p)
if err != nil {
return fmt.Errorf("UnmarshalText err: %w", err)
}
out = append(out, i)
}
*c = out
return nil
}
var _ encoding.TextUnmarshaler = (*CSVInts)(nil)
// delete with go 1.26
func Pointer[T any](obj T) *T {
return &obj
}
type AllFields struct {
Str string `query:"str"`
Int int `query:"int"`
Int64 int64 `query:"int64"`
Uint uint `query:"uint"`
Bool bool `query:"bool"`
Float float64 `query:"float"`
Skipped bool `query:"-"`
WithoutTag bool
StrPtr *string `query:"str_ptr"`
IntPtr *int `query:"int_ptr"`
Int64Ptr *int64 `query:"int64_ptr"`
UintPtr *uint `query:"uint_ptr"`
BoolPtr *bool `query:"bool_ptr"`
FloatPtr *float64 `query:"float_ptr"`
Strs []string `query:"strs"`
Ints []int `query:"ints"`
Bools []bool `query:"bools"`
Floats []float64 `query:"floats"`
CSV CSVInts `query:"csv"`
CSVPtr *CSVInts `query:"csv_ptr"`
Nested Address `query:"nested"`
CustomStruct CustomType `query:"custom_struct"`
}
type Unexported struct {
X int `query:"x"`
y int `query:"y"` //lint:ignore U1000 for test prupose
}
type InvalidInt struct {
X int `query:"x"`
}
type InvalidSlice struct {
X []int `query:"x"`
}
type Unsupported struct {
X map[string]int `query:"x"`
}
func TestDecodeQueryParams(t *testing.T) {
tests := []struct {
name string
rawURL string
out any
expect any
err error
}{
{
name: "nominal",
rawURL: "/test?" +
"str=hello" +
"&int=42" +
"&int64=9001" +
"&uint=7" +
"&bool=true" +
"&float=3.14" +
"&Skipped=true" +
"&WithoutTag=true" +
"&str_ptr=hello" +
"&int_ptr=42" +
"&int64_ptr=9001" +
"&uint_ptr=7" +
"&bool_ptr=true" +
"&float_ptr=3.14" +
"&strs=a&strs=b" +
"&ints=1&ints=2" +
"&bools=true&bools=false" +
"&floats=1.1&floats=2.2" +
"&csv=10,20,30" +
"&csv_ptr=10,20,30" +
"&nested.city=Paris" +
"&nested.zip=75001" +
"&custom_struct=myvalue",
out: &AllFields{},
expect: &AllFields{
Str: "hello",
Int: 42,
Int64: 9001,
Uint: 7,
Bool: true,
Float: 3.14,
Skipped: false,
WithoutTag: true,
StrPtr: Pointer("hello"),
IntPtr: Pointer(42),
Int64Ptr: Pointer(int64(9001)),
UintPtr: Pointer(uint(7)),
BoolPtr: Pointer(true),
FloatPtr: Pointer(3.14),
Strs: []string{"a", "b"},
Ints: []int{1, 2},
Bools: []bool{true, false},
Floats: []float64{1.1, 2.2},
CSV: CSVInts{10, 20, 30},
CSVPtr: &CSVInts{10, 20, 30},
Nested: Address{
City: "Paris",
ZipCode: 75001,
},
CustomStruct: CustomType{
Value: "custom:myvalue",
},
},
},
{
name: "unexported fields are ignored",
rawURL: "/test?x=1&y=99",
out: &Unexported{},
expect: &Unexported{
X: 1,
},
},
{
name: "empty query does nothing",
rawURL: "/test",
out: &AllFields{
Int: 99,
},
expect: &AllFields{
Int: 99,
},
},
{
name: "invalid scalar value",
rawURL: "/test?x=abc",
out: &InvalidInt{},
err: strconv.ErrSyntax,
},
{
name: "invalid slice element",
rawURL: "/test?x=1&x=bad",
out: &InvalidSlice{},
err: strconv.ErrSyntax,
},
{
name: "unsupported field type",
rawURL: "/test?x=1",
out: &Unsupported{},
err: ErrUnsupportedType,
},
{
name: "out is not a pointer",
rawURL: "/test?x=1",
out: AllFields{},
err: ErrNotPointer,
},
{
name: "out is nil pointer",
rawURL: "/test?x=1",
out: (*AllFields)(nil),
err: ErrNotPointer,
},
{
name: "out is pointer to non-struct",
rawURL: "/test?x=1",
out: new(int),
err: ErrNotStruct,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := url.Parse(tt.rawURL)
require.NoError(t, err)
err = DecodeQueryParams(*u, tt.out)
if tt.err == nil {
assert.Equal(t, tt.expect, tt.out)
}
assert.ErrorIs(t, err, tt.err)
})
}
}