-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbind_struct_test.go
More file actions
312 lines (273 loc) · 8.42 KB
/
Copy pathbind_struct_test.go
File metadata and controls
312 lines (273 loc) · 8.42 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
package celeris_test
import (
"errors"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/goceleris/celeris"
"github.com/goceleris/celeris/celeristest"
)
func TestBindQueryDefaultsAndConversion(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x",
celeristest.WithQuery("page", "3"),
celeristest.WithQuery("tag", "a"),
celeristest.WithQuery("tag", "b"),
)
var got struct {
Page int `query:"page" default:"1"`
Limit int `query:"limit" default:"20"`
Tags []string `query:"tag"`
}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Page != 3 {
t.Errorf("Page = %d, want 3", got.Page)
}
if got.Limit != 20 {
t.Errorf("Limit = %d, want 20 from the default tag", got.Limit)
}
if len(got.Tags) != 2 || got.Tags[0] != "a" || got.Tags[1] != "b" {
t.Errorf("Tags = %v, want [a b]", got.Tags)
}
}
func TestBindParamsAndHeader(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/items/42",
celeristest.WithParam("id", "42"),
celeristest.WithHeader("X-Request-Id", "trace-abc"),
)
var got struct {
ID int `param:"id"`
Trace string `header:"X-Request-Id"`
}
if err := ctx.BindParams(&got); err != nil {
t.Fatalf("BindParams: %v", err)
}
if err := ctx.BindHeader(&got); err != nil {
t.Fatalf("BindHeader: %v", err)
}
if got.ID != 42 {
t.Errorf("ID = %d, want 42", got.ID)
}
if got.Trace != "trace-abc" {
t.Errorf("Trace = %q, want trace-abc", got.Trace)
}
}
func TestBindAllBodyWinsOverQuery(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "POST", "/x",
celeristest.WithQuery("name", "fromquery"),
celeristest.WithQuery("page", "7"),
celeristest.WithContentType("application/json"),
celeristest.WithBody([]byte(`{"name":"frombody"}`)),
)
var got struct {
Name string `query:"name" json:"name"`
Page int `query:"page" json:"-"`
}
if err := ctx.BindAll(&got); err != nil {
t.Fatalf("BindAll: %v", err)
}
// The body is applied last, so it wins for a field it mentions.
if got.Name != "frombody" {
t.Errorf("Name = %q, want frombody (body must win)", got.Name)
}
// A field the body does not mention keeps its query-derived value.
if got.Page != 7 {
t.Errorf("Page = %d, want 7 (query value preserved)", got.Page)
}
}
func TestBindAllWithoutBodyIsNotAnError(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("page", "5"))
var got struct {
Page int `query:"page"`
}
if err := ctx.BindAll(&got); err != nil {
t.Fatalf("BindAll on a bodyless GET should succeed, got %v", err)
}
if got.Page != 5 {
t.Errorf("Page = %d, want 5", got.Page)
}
}
func TestBindConversionErrorIsTyped(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("page", "notanumber"))
var v struct {
Page int `query:"page"`
}
err := ctx.BindQuery(&v)
if err == nil {
t.Fatal("expected an error binding a non-numeric value into an int")
}
var be *celeris.BindError
if !errors.As(err, &be) {
t.Fatalf("error %v is not a *celeris.BindError", err)
}
if be.Field != "Page" || be.Source != "query" || be.Key != "page" || be.Value != "notanumber" {
t.Errorf("BindError = %+v, want Field=Page Source=query Key=page Value=notanumber", be)
}
}
func TestBindDurationAndBool(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x",
celeristest.WithQuery("timeout", "1500ms"),
celeristest.WithQuery("debug", "true"),
)
var got struct {
Timeout time.Duration `query:"timeout"`
Debug bool `query:"debug"`
}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Timeout != 1500*time.Millisecond {
t.Errorf("Timeout = %v, want 1.5s (must parse as a duration, not an int64)", got.Timeout)
}
if !got.Debug {
t.Error("Debug = false, want true")
}
}
func TestBindPointerFieldStaysNilWhenAbsent(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x")
var got struct {
Cursor *string `query:"cursor"`
}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Cursor != nil {
t.Errorf("Cursor = %q, want nil when the param is absent", *got.Cursor)
}
}
func TestBindPointerFieldSetWhenPresent(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("cursor", "abc"))
var got struct {
Cursor *string `query:"cursor"`
}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Cursor == nil || *got.Cursor != "abc" {
t.Errorf("Cursor = %v, want pointer to abc", got.Cursor)
}
}
func TestBindRequiresNonNilStructPointer(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x")
var s struct{}
if err := ctx.BindQuery(s); err == nil {
t.Error("binding into a non-pointer should error")
}
var p *struct{}
if err := ctx.BindQuery(p); err == nil {
t.Error("binding into a nil pointer should error")
}
n := 5
if err := ctx.BindQuery(&n); err == nil {
t.Error("binding into a pointer-to-non-struct should error")
}
}
func TestUntaggedFieldsAreLeftAlone(t *testing.T) {
ctx, _ := celeristest.NewContextT(t, "GET", "/x",
celeristest.WithQuery("page", "1"),
celeristest.WithQuery("Internal", "hacked"),
)
got := struct {
Page int `query:"page"`
Internal string
}{Internal: "preset"}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Internal != "preset" {
t.Errorf("Internal = %q, want preset — an untagged field must never bind", got.Internal)
}
}
func TestBindEmbeddedStruct(t *testing.T) {
type Pagination struct {
Page int `query:"page" default:"1"`
Limit int `query:"limit" default:"10"`
}
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("page", "4"))
var got struct {
Pagination
Q string `query:"q"`
}
if err := ctx.BindQuery(&got); err != nil {
t.Fatalf("BindQuery: %v", err)
}
if got.Page != 4 {
t.Errorf("embedded Page = %d, want 4", got.Page)
}
if got.Limit != 10 {
t.Errorf("embedded Limit = %d, want 10 from the default tag", got.Limit)
}
}
func TestBindForm(t *testing.T) {
form := url.Values{"name": {"ada"}, "age": {"36"}}
ctx, _ := celeristest.NewContextT(t, "POST", "/x",
celeristest.WithContentType("application/x-www-form-urlencoded"),
celeristest.WithBody([]byte(form.Encode())),
)
var got struct {
Name string `form:"name"`
Age int `form:"age"`
}
if err := ctx.BindForm(&got); err != nil {
t.Fatalf("BindForm: %v", err)
}
if got.Name != "ada" || got.Age != 36 {
t.Errorf("got %+v, want {ada 36}", got)
}
}
// --- validator hook -------------------------------------------------------
type stubValidator struct{ err error }
func (s stubValidator) Validate(any) error { return s.err }
func TestValidateWithoutValidatorIsAnError(t *testing.T) {
celeris.SetValidator(nil)
ctx, _ := celeristest.NewContextT(t, "GET", "/x")
err := ctx.Validate(&struct{}{})
if !errors.Is(err, celeris.ErrNoValidator) {
t.Fatalf("Validate() = %v, want ErrNoValidator — passing silently would hide a missing SetValidator", err)
}
}
func TestBindAndValidateRunsTheValidator(t *testing.T) {
sentinel := errors.New("Page is required")
celeris.SetValidator(stubValidator{err: sentinel})
t.Cleanup(func() { celeris.SetValidator(nil) })
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("page", "2"))
var v struct {
Page int `query:"page"`
}
err := ctx.BindAndValidate(&v)
if !errors.Is(err, sentinel) {
t.Fatalf("BindAndValidate() = %v, want the validator's error", err)
}
if v.Page != 2 {
t.Errorf("Page = %d, want 2 — binding must run before validation", v.Page)
}
}
func TestBindAndValidateSucceedsWhenValidatorPasses(t *testing.T) {
celeris.SetValidator(stubValidator{err: nil})
t.Cleanup(func() { celeris.SetValidator(nil) })
ctx, _ := celeristest.NewContextT(t, "GET", "/x", celeristest.WithQuery("page", "9"))
var v struct {
Page int `query:"page"`
}
if err := ctx.BindAndValidate(&v); err != nil {
t.Fatalf("BindAndValidate: %v", err)
}
if v.Page != 9 {
t.Errorf("Page = %d, want 9", v.Page)
}
}
func TestBindErrorMessageNamesFieldAndSource(t *testing.T) {
be := &celeris.BindError{Field: "Page", Source: "query", Key: "page", Value: "x", Err: strconv.ErrSyntax}
msg := be.Error()
for _, want := range []string{"query", "page", "Page"} {
if !strings.Contains(msg, want) {
t.Errorf("BindError.Error() = %q, missing %q", msg, want)
}
}
if !errors.Is(be, strconv.ErrSyntax) {
t.Error("BindError should unwrap to the underlying conversion error")
}
}