-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource_test.go
More file actions
508 lines (423 loc) · 12.5 KB
/
source_test.go
File metadata and controls
508 lines (423 loc) · 12.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package binding
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestValueGetter_GetAll tests all GetAll implementations
func TestValueGetter_GetAll(t *testing.T) {
t.Parallel()
t.Run("queryGetter", func(t *testing.T) {
t.Parallel()
values := url.Values{}
values.Add("tags", "go")
values.Add("tags", "rust")
values.Add("tags", "python")
getter := NewQueryGetter(values)
all := getter.GetAll("tags")
assert.Equal(t, []string{"go", "rust", "python"}, all)
none := getter.GetAll("nonexistent")
assert.Nil(t, none)
})
t.Run("paramsGetter", func(t *testing.T) {
t.Parallel()
params := map[string]string{"id": "123"}
getter := NewPathGetter(params)
all := getter.GetAll("id")
assert.Equal(t, []string{"123"}, all)
none := getter.GetAll("nonexistent")
assert.Nil(t, none)
})
t.Run("cookieGetter", func(t *testing.T) {
t.Parallel()
cookies := []*http.Cookie{
{Name: "session", Value: url.QueryEscape("abc123")},
{Name: "session", Value: url.QueryEscape("def456")},
}
getter := NewCookieGetter(cookies)
all := getter.GetAll("session")
assert.Len(t, all, 2)
none := getter.GetAll("nonexistent")
assert.Empty(t, none)
})
t.Run("headerGetter", func(t *testing.T) {
t.Parallel()
headers := http.Header{}
headers.Add("X-Tags", "tag1")
headers.Add("X-Tags", "tag2")
headers.Add("X-Tags", "tag3")
getter := NewHeaderGetter(headers)
all := getter.GetAll("X-Tags")
assert.Len(t, all, 3)
})
t.Run("formGetter", func(t *testing.T) {
t.Parallel()
values := url.Values{}
values.Add("items", "item1")
values.Add("items", "item2")
getter := NewFormGetter(values)
all := getter.GetAll("items")
assert.Len(t, all, 2)
})
}
// TestCookieGetter_Has tests the Has method
func TestCookieGetter_Has(t *testing.T) {
t.Parallel()
cookies := []*http.Cookie{
{Name: "session", Value: "abc123"},
{Name: "user_id", Value: "42"},
}
getter := NewCookieGetter(cookies)
assert.True(t, getter.Has("session"))
assert.True(t, getter.Has("user_id"))
assert.False(t, getter.Has("nonexistent"))
}
// TestCookieGetter_Get_UnescapingError tests cookieGetter.Get with URL unescaping error
func TestCookieGetter_Get_UnescapingError(t *testing.T) {
t.Parallel()
cookies := []*http.Cookie{
{Name: "data", Value: "%ZZ"}, // Invalid percent encoding
}
getter := NewCookieGetter(cookies)
// Should fallback to raw cookie value on unescaping error
value := getter.Get("data")
assert.Equal(t, "%ZZ", value, "Expected raw value %%ZZ on unescaping error")
}
// TestCookieGetter_Get_NotFound tests cookieGetter.Get when cookie is not found
func TestCookieGetter_Get_NotFound(t *testing.T) {
t.Parallel()
cookies := []*http.Cookie{
{Name: "session_id", Value: "abc123"},
{Name: "theme", Value: "dark"},
}
getter := NewCookieGetter(cookies)
// Should return empty string when cookie key is not found
value := getter.Get("nonexistent")
assert.Empty(t, value, "Expected empty string for nonexistent cookie")
// Verify existing cookies still work
session := getter.Get("session_id")
assert.Equal(t, "abc123", session, "Expected session_id to be 'abc123'")
}
// TestPathGetter_GetAll_NonExistent tests paramsGetter.GetAll for non-existent key
func TestPathGetter_GetAll_NonExistent(t *testing.T) {
t.Parallel()
params := map[string]string{"id": "123"}
getter := NewPathGetter(params)
// Test non-existent key returns nil
none := getter.GetAll("nonexistent")
assert.Nil(t, none, "Expected nil for non-existent key")
// Test existing key returns slice
all := getter.GetAll("id")
require.Len(t, all, 1, "Expected slice with 1 element")
assert.Equal(t, "123", all[0], "Expected first element to be '123'")
}
// TestBind_Cookies tests cookie binding functionality
func TestBind_Cookies(t *testing.T) {
t.Parallel()
type SessionCookies struct {
SessionID string `cookie:"session_id"`
Theme string `cookie:"theme"`
RememberMe bool `cookie:"remember_me"`
}
tests := []struct {
name string
cookies []*http.Cookie
params any
validate func(t *testing.T, params any)
}{
{
name: "valid cookies",
cookies: []*http.Cookie{
{Name: "session_id", Value: "abc123"},
{Name: "theme", Value: "dark"},
{Name: "remember_me", Value: "true"},
},
params: &SessionCookies{},
validate: func(t *testing.T, params any) {
t.Helper()
cookies, ok := params.(*SessionCookies)
require.True(t, ok)
assert.Equal(t, "abc123", cookies.SessionID)
assert.Equal(t, "dark", cookies.Theme)
assert.True(t, cookies.RememberMe)
},
},
{
name: "URL encoded cookies",
cookies: []*http.Cookie{
{Name: "session_id", Value: url.QueryEscape("value with spaces")},
},
params: &struct {
SessionID string `cookie:"session_id"`
}{},
validate: func(t *testing.T, params any) {
t.Helper()
cookies, ok := params.(*struct {
SessionID string `cookie:"session_id"`
})
require.True(t, ok)
assert.Equal(t, "value with spaces", cookies.SessionID)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
getter := NewCookieGetter(tt.cookies)
err := Raw(getter, TagCookie, tt.params)
require.NoError(t, err)
tt.validate(t, tt.params)
})
}
}
// TestBind_Headers tests HTTP header binding functionality
func TestBind_Headers(t *testing.T) {
t.Parallel()
type RequestHeaders struct {
UserAgent string `header:"User-Agent"`
Token string `header:"Authorization"`
Accept string `header:"Accept"`
}
tests := []struct {
name string
headers http.Header
params any
validate func(t *testing.T, params any)
}{
{
name: "valid headers",
headers: func() http.Header {
h := http.Header{}
h.Set("User-Agent", "Mozilla/5.0")
h.Set("Authorization", "Bearer token123")
h.Set("Accept", "application/json")
return h
}(),
params: &RequestHeaders{},
validate: func(t *testing.T, params any) {
t.Helper()
headers, ok := params.(*RequestHeaders)
require.True(t, ok)
assert.Equal(t, "Mozilla/5.0", headers.UserAgent)
assert.Equal(t, "Bearer token123", headers.Token)
assert.Equal(t, "application/json", headers.Accept)
},
},
{
name: "case insensitive",
headers: func() http.Header {
h := http.Header{}
h.Set("User-Agent", "Test")
return h
}(),
params: &struct {
UserAgent string `header:"User-Agent"`
}{},
validate: func(t *testing.T, params any) {
t.Helper()
headers, ok := params.(*struct {
UserAgent string `header:"User-Agent"`
})
require.True(t, ok)
assert.Equal(t, "Test", headers.UserAgent)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
getter := NewHeaderGetter(tt.headers)
err := Raw(getter, TagHeader, tt.params)
require.NoError(t, err)
tt.validate(t, tt.params)
})
}
}
// TestBind_GetAll tests GetAll functionality through actual binding for all getter types
func TestBind_GetAll(t *testing.T) {
t.Parallel()
t.Run("PathGetter", func(t *testing.T) {
t.Parallel()
type Params struct {
ID string `path:"id"`
}
params := map[string]string{"id": "123"}
getter := NewPathGetter(params)
var p Params
require.NoError(t, Raw(getter, TagPath, &p), "BindPath should succeed")
assert.Equal(t, "123", p.ID, "Expected ID=123")
// Test that GetAll is used internally for slices
type ParamsWithSlice struct {
IDs []string `path:"id"`
}
var paramsSlice ParamsWithSlice
params = map[string]string{"id": "456"}
getter = NewPathGetter(params)
require.NoError(t, Raw(getter, TagPath, ¶msSlice), "BindPath should succeed for slice")
require.Len(t, paramsSlice.IDs, 1, "Expected 1 ID")
assert.Equal(t, "456", paramsSlice.IDs[0], "Expected first ID to be '456'")
})
t.Run("CookieGetter", func(t *testing.T) {
t.Parallel()
type CookiesWithSession struct {
Session []string `cookie:"session"`
}
type CookiesWithData struct {
Data []string `cookie:"data"`
}
tests := []struct {
name string
setup func() []*http.Cookie
params any
validate func(t *testing.T, cookies any)
}{
{
name: "multiple cookies with same name",
setup: func() []*http.Cookie {
return []*http.Cookie{
{Name: "session", Value: "abc123"},
{Name: "session", Value: "def456"},
}
},
params: &CookiesWithSession{},
validate: func(t *testing.T, cookies any) {
t.Helper()
c, ok := cookies.(*CookiesWithSession)
require.True(t, ok)
require.Len(t, c.Session, 2, "Expected 2 session cookies")
assert.Equal(t, "abc123", c.Session[0], "Expected first session")
assert.Equal(t, "def456", c.Session[1], "Expected second session")
},
},
{
name: "URL unescaping error path",
setup: func() []*http.Cookie {
return []*http.Cookie{{Name: "data", Value: "%ZZ"}} // Invalid percent encoding
},
params: &CookiesWithData{},
validate: func(t *testing.T, cookies any) {
t.Helper()
c, ok := cookies.(*CookiesWithData)
require.True(t, ok)
require.Len(t, c.Data, 1, "Expected 1 data cookie")
assert.Equal(t, "%ZZ", c.Data[0], "Expected raw value %%ZZ")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cookies := tt.setup()
getter := NewCookieGetter(cookies)
require.NoError(t, Raw(getter, TagCookie, tt.params), "BindCookies should succeed")
tt.validate(t, tt.params)
})
}
})
t.Run("HeaderGetter", func(t *testing.T) {
t.Parallel()
type Headers struct {
Tags []string `header:"X-Tags"`
}
headers := http.Header{}
headers.Add("X-Tags", "tag1")
headers.Add("X-Tags", "tag2")
headers.Add("X-Tags", "tag3")
getter := NewHeaderGetter(headers)
var h Headers
require.NoError(t, Raw(getter, TagHeader, &h), "BindHeaders should succeed")
require.Len(t, h.Tags, 3, "Expected 3 tags")
assert.Equal(t, "tag1", h.Tags[0], "Expected first tag")
assert.Equal(t, "tag2", h.Tags[1], "Expected second tag")
assert.Equal(t, "tag3", h.Tags[2], "Expected third tag")
})
}
// TestValueGetter_HasSemantics tests that Has() correctly distinguishes empty vs missing
func TestValueGetter_HasSemantics(t *testing.T) {
t.Parallel()
tests := []struct {
name string
setupGetter func() ValueGetter
key string
wantHas bool
wantGet string
validate func(t *testing.T, getter ValueGetter)
}{
{
name: "query getter - empty value",
setupGetter: func() ValueGetter {
values := url.Values{}
values.Set("name", "") // Empty but present
return NewQueryGetter(values)
},
key: "name",
wantHas: true,
wantGet: "",
},
{
name: "query getter - missing key",
setupGetter: func() ValueGetter {
values := url.Values{}
values.Set("other", "value")
return NewQueryGetter(values)
},
key: "name",
wantHas: false,
wantGet: "",
},
{
name: "form getter - empty value",
setupGetter: func() ValueGetter {
values := url.Values{}
values.Set("email", "")
return NewFormGetter(values)
},
key: "email",
wantHas: true,
wantGet: "",
},
{
name: "params getter - empty value",
setupGetter: func() ValueGetter {
params := map[string]string{
"id": "",
}
return NewPathGetter(params)
},
key: "id",
wantHas: true,
wantGet: "",
validate: func(t *testing.T, getter ValueGetter) {
t.Helper()
assert.False(t, getter.Has("missing"), "Has() should return false for missing key")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
getter := tt.setupGetter()
assert.Equal(t, tt.wantHas, getter.Has(tt.key), "Has() should return %v for key %q", tt.wantHas, tt.key)
assert.Equal(t, tt.wantGet, getter.Get(tt.key), "Get() should return %q for key %q", tt.wantGet, tt.key)
if tt.validate != nil {
tt.validate(t, getter)
}
})
}
}