-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequestutils_test.go
More file actions
333 lines (283 loc) · 12.6 KB
/
requestutils_test.go
File metadata and controls
333 lines (283 loc) · 12.6 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
package ginvalidator
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
type ginCtxReqOpts struct {
method string
url string
body string
contentType string
headers map[string]string
cookies []*http.Cookie
params gin.Params
}
func createTestGinCtx(opts ginCtxReqOpts) *gin.Context {
gin.SetMode(gin.TestMode)
if opts.method == "" {
opts.method = http.MethodPost
}
if opts.url == "" {
opts.url = "/test"
}
if opts.contentType == "" {
opts.contentType = "application/json"
}
if opts.headers == nil {
opts.headers = make(map[string]string)
}
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
req, _ := http.NewRequest(opts.method, opts.url, bytes.NewBufferString(opts.body))
req.Header.Set("Content-Type", opts.contentType)
for name, value := range opts.headers {
req.Header.Set(name, value)
}
for _, cookie := range opts.cookies {
req.AddCookie(cookie)
}
for _, param := range opts.params {
ctx.Params = append(opts.params, param)
}
ctx.Request = req
return ctx
}
func setupRouter() *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.Default()
r.GET("/ping", func(ctx *gin.Context) {
ctx.String(200, "pong")
})
return r
}
func TestExtractFieldValFromBody(t *testing.T) {
tests := []struct {
name string
opts ginCtxReqOpts
field string
value string
err error
}{
// json extraction
{name: "Valid json extraction", field: "name", opts: ginCtxReqOpts{body: `{"name":"John"}`, contentType: "application/json"}, value: `John`, err: nil},
{name: "Nested field extraction", field: "user.name", opts: ginCtxReqOpts{body: `{"user": {"name":"John"}}`, contentType: "application/json"}, value: `John`, err: nil},
{name: "Missing field", field: "age", opts: ginCtxReqOpts{body: `{"name":"John"}`, contentType: "application/json"}, value: ``, err: nil},
{name: "Array extraction", field: "names.0", opts: ginCtxReqOpts{body: `{"names": ["John", "Doe"]}`, contentType: "application/json"}, value: `John`, err: nil},
{name: "Deeply nested field extraction", field: "a.b.c.d.e", opts: ginCtxReqOpts{body: `{"a": {"b": {"c": {"d": {"e": "value"}}}}}`, contentType: "application/json"}, value: `value`, err: nil},
{name: "Field extraction with numeric values", field: "age", opts: ginCtxReqOpts{body: `{"name":"John", "age": 30}`, contentType: "application/json"}, value: `30`, err: nil},
{name: "Empty JSON object", field: "name", opts: ginCtxReqOpts{body: `{}`, contentType: "application/json"}, value: ``, err: nil},
{name: "Null field value", field: "name", opts: ginCtxReqOpts{body: `{"name": null}`, contentType: "application/json"}, value: ``, err: nil},
// // x-www-form-urlencoded extraction
{name: "Valid x-www-form-urlencoded extraction (name)", field: "name", opts: ginCtxReqOpts{body: `name=John`, contentType: "application/x-www-form-urlencoded"}, value: `John`, err: nil},
{name: "Valid x-www-form-urlencoded extraction (age)", field: "age", opts: ginCtxReqOpts{body: `name=John&age=30`, contentType: "application/x-www-form-urlencoded"}, value: `30`, err: nil},
{name: "Valid x-www-form-urlencoded extraction (email)", field: "email", opts: ginCtxReqOpts{body: `name=John&age=30&email=john@example.com`, contentType: "application/x-www-form-urlencoded"}, value: `john@example.com`, err: nil},
{name: "Invalid x-www-form-urlencoded extraction (missing field)", field: "address", opts: ginCtxReqOpts{body: `name=John&age=30`, contentType: "application/x-www-form-urlencoded"}, value: ``, err: nil},
{name: "Valid x-www-form-urlencoded extraction (special characters)", field: "name", opts: ginCtxReqOpts{body: `name=John%20Doe&age=30`, contentType: "application/x-www-form-urlencoded"}, value: `John Doe`, err: nil},
{name: "Valid x-www-form-urlencoded extraction (numeric value as string)", field: "age", opts: ginCtxReqOpts{body: `name=John&age=42`, contentType: "application/x-www-form-urlencoded"}, value: `42`, err: nil},
// multipart/form-data extraction
{
name: "Valid multipart/form-data extraction (name)",
field: "name",
opts: ginCtxReqOpts{
contentType: "multipart/form-data; boundary=6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9",
body: `--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9
Content-Disposition: form-data; name="name"
John
--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9--`,
},
value: `John`,
err: nil,
},
{
name: "Valid multipart/form-data extraction (age)",
field: "age",
opts: ginCtxReqOpts{
contentType: "multipart/form-data; boundary=6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9",
body: `--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9
Content-Disposition: form-data; name="age"
30
--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9--`,
},
value: `30`,
err: nil,
},
{
name: "Valid multipart/form-data extraction (email)",
field: "email",
opts: ginCtxReqOpts{
contentType: "multipart/form-data; boundary=6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9",
body: `--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9
Content-Disposition: form-data; name="email"
John@example.com
--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9--`,
},
value: `John@example.com`,
err: nil,
},
{
name: "Valid multipart/form-data extraction (address)",
field: "address",
opts: ginCtxReqOpts{
contentType: "multipart/form-data; boundary=6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9",
body: `--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9
Content-Disposition: form-data; name="address"
home address
--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9--`,
},
value: `home address`,
err: nil,
},
{
name: "Valid multipart/form-data extraction (special characters)",
field: "name",
opts: ginCtxReqOpts{
contentType: "multipart/form-data; boundary=6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9",
body: `--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9
Content-Disposition: form-data; name="name"
John%20Doe
--6c55825619090769257acc8079eeba85e5e9874c7116b71da35065945dd9--`,
},
value: `John%20Doe`,
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := createTestGinCtx(test.opts)
ans, err := extractFieldValFromBody(ctx, test.field)
if err != nil {
if !errors.Is(test.err, test.err) {
t.Errorf("got error %+v, want %+v", err, test.err)
}
return
}
if ans != test.value {
t.Errorf("got %q, want %q", ans, test.value)
}
})
}
}
func TestExtractFieldValFromCookie(t *testing.T) {
tests := []struct {
name string
opts ginCtxReqOpts
field string
value string
err error
}{
{name: "Valid cookie extraction", field: "name", opts: ginCtxReqOpts{cookies: []*http.Cookie{{Name: "name", Value: "John"}}}, value: `John`, err: nil},
{name: "Valid cookie extraction (multiple cookies)", field: "session", opts: ginCtxReqOpts{cookies: []*http.Cookie{{Name: "session", Value: "abc123"}}}, value: `abc123`, err: nil},
{name: "Valid cookie extraction (empty cookie)", field: "empty", opts: ginCtxReqOpts{cookies: []*http.Cookie{{Name: "empty", Value: ""}}}, value: ``, err: nil},
{name: "Invalid cookie extraction (missing cookie)", field: "missing", opts: ginCtxReqOpts{cookies: nil}, value: ``, err: nil},
{name: "Valid cookie extraction (special characters)", field: "name", opts: ginCtxReqOpts{cookies: []*http.Cookie{{Name: "name", Value: "John%20Doe"}}}, value: `John Doe`, err: nil},
{name: "Valid cookie extraction (numeric value)", field: "age", opts: ginCtxReqOpts{cookies: []*http.Cookie{{Name: "age", Value: "42"}}}, value: `42`, err: nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := createTestGinCtx(test.opts)
ans, err := extractFieldValFromCookie(ctx, test.field)
if err != nil {
if !errors.Is(test.err, test.err) {
t.Errorf("got error %+v, want %+v", err, test.err)
}
}
if ans != test.value {
t.Errorf("got %q, want %q", ans, test.value)
}
})
}
}
func TestExtractFieldValFromHeader(t *testing.T) {
tests := []struct {
name string
opts ginCtxReqOpts
field string
value string
err error
}{
{name: "Valid header extraction", field: "name", opts: ginCtxReqOpts{headers: map[string]string{"name": "John"}}, value: `John`, err: nil},
{name: "Valid header extraction (multiple headers)", field: "session", opts: ginCtxReqOpts{headers: map[string]string{"session": "abc123"}}, value: `abc123`, err: nil},
{name: "Valid header extraction (empty header)", field: "empty", opts: ginCtxReqOpts{headers: map[string]string{"empty": ""}}, value: ``, err: nil},
{name: "Invalid header extraction (missing header)", field: "missing", opts: ginCtxReqOpts{headers: map[string]string{}}, value: ``, err: nil},
{name: "Valid header extraction (special characters)", field: "name", opts: ginCtxReqOpts{headers: map[string]string{"name": "John%20Doe"}}, value: `John%20Doe`, err: nil},
{name: "Valid header extraction (numeric value)", field: "age", opts: ginCtxReqOpts{headers: map[string]string{"age": "42"}}, value: `42`, err: nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := createTestGinCtx(test.opts)
ans, err := extractFieldValFromHeader(ctx, test.field)
if err != nil {
if !errors.Is(test.err, test.err) {
t.Errorf("got error %+v, want %+v", err, test.err)
}
}
if ans != test.value {
t.Errorf("got %q, want %q", ans, test.value)
}
})
}
}
func TestExtractFieldValFromParam(t *testing.T) {
tests := []struct {
name string
opts ginCtxReqOpts
field string
value string
err error
}{
{name: "Valid header extraction", field: "name", opts: ginCtxReqOpts{headers: map[string]string{"name": "John"}, params: gin.Params{gin.Param{Key: "name", Value: "John"}}}, value: `John`, err: nil},
{name: "Valid header extraction (multiple headers)", field: "session", opts: ginCtxReqOpts{headers: map[string]string{"session": "abc123"}, params: gin.Params{gin.Param{Key: "session", Value: "abc123"}}}, value: `abc123`, err: nil},
{name: "Valid header extraction (empty header)", field: "empty", opts: ginCtxReqOpts{headers: map[string]string{"empty": ""}, params: gin.Params{gin.Param{Key: "empty", Value: ""}}}, value: ``, err: nil},
{name: "Invalid header extraction (missing header)", field: "missing", opts: ginCtxReqOpts{headers: map[string]string{}, params: gin.Params{}}, value: ``, err: nil},
{name: "Valid header extraction (special characters)", field: "name", opts: ginCtxReqOpts{headers: map[string]string{"name": "John%20Doe"}, params: gin.Params{gin.Param{Key: "name", Value: "John%20Doe"}}}, value: `John Doe`, err: nil},
{name: "Valid header extraction (numeric value)", field: "age", opts: ginCtxReqOpts{headers: map[string]string{"age": "42"}, params: gin.Params{gin.Param{Key: "age", Value: "42"}}}, value: `42`, err: nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := createTestGinCtx(test.opts)
ans, err := extractFieldValFromParam(ctx, test.field)
if err != nil {
if !errors.Is(test.err, test.err) {
t.Errorf("got error %+v, want %+v", err, test.err)
}
}
if ans != test.value {
t.Errorf("got %q, want %q", ans, test.value)
}
})
}
}
func TestExtractFieldValFromQuery(t *testing.T) {
tests := []struct {
name string
opts ginCtxReqOpts
field string
value string
err error
}{
{name: "Valid query extraction", field: "name", opts: ginCtxReqOpts{url: "/test?name=John"}, value: `John`, err: nil},
{name: "Valid query extraction (multiple queries)", field: "session", opts: ginCtxReqOpts{url: "/test?session=abc123"}, value: `abc123`, err: nil},
{name: "Valid query extraction (empty query)", field: "empty", opts: ginCtxReqOpts{url: "/test?empty="}, value: ``, err: nil},
{name: "Invalid query extraction (missing query)", field: "missing", opts: ginCtxReqOpts{url: "/test"}, value: ``, err: nil},
{name: "Valid query extraction (special characters)", field: "name", opts: ginCtxReqOpts{url: "/test?name=John%20Doe"}, value: `John Doe`, err: nil},
{name: "Valid query extraction (numeric value)", field: "age", opts: ginCtxReqOpts{url: "/test?age=42"}, value: `42`, err: nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := createTestGinCtx(test.opts)
ans, err := extractFieldValFromQuery(ctx, test.field)
if err != nil {
if !errors.Is(test.err, test.err) {
t.Errorf("got error %+v, want %+v", err, test.err)
}
}
if ans != test.value {
t.Errorf("got %q, want %q", ans, test.value)
}
})
}
}