-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
502 lines (405 loc) · 13.7 KB
/
validate_test.go
File metadata and controls
502 lines (405 loc) · 13.7 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
package api_test
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
type validatedReq struct {
Body struct {
Name string `json:"name"`
}
}
func (r *validatedReq) Validate(_ context.Context) error {
if r.Body.Name == "" {
return api.Error(http.StatusBadRequest, "name required")
}
return nil
}
func TestValidator_perType(t *testing.T) {
t.Parallel()
type Resp struct {
Name string `json:"name"`
}
r := api.New()
api.Post(r, "/users", func(_ context.Context, req *validatedReq) (*Resp, error) {
return &Resp{Name: req.Body.Name}, nil
})
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
tests := map[string]struct {
body string
wantStatus int
}{
"valid": {`{"name":"Alice"}`, http.StatusOK},
"invalid - empty name": {`{"name":""}`, http.StatusBadRequest},
"invalid - missing": {`{}`, http.StatusBadRequest},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/users", strings.NewReader(tc.body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
})
}
}
type ctxValidatedReq struct {
Body struct {
Value string `json:"value"`
}
}
type tenantKey struct{}
func (r *ctxValidatedReq) Validate(ctx context.Context) error {
if ctx.Value(tenantKey{}) == nil {
return api.ValidationErrors{
{Field: "ctx.tenant", Message: "tenant missing from context"},
}
}
return nil
}
func TestValidator_usesContext(t *testing.T) {
t.Parallel()
type Resp struct {
OK bool `json:"ok"`
}
r := api.New()
// Middleware that stashes a tenant in context when header is present.
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if t := req.Header.Get("X-Tenant"); t != "" {
req = req.WithContext(context.WithValue(req.Context(), tenantKey{}, t))
}
next.ServeHTTP(w, req)
})
})
api.Post(r, "/ctx", func(_ context.Context, _ *ctxValidatedReq) (*Resp, error) {
return &Resp{OK: true}, nil
})
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
tests := map[string]struct {
tenant string
wantStatus int
}{
"with tenant": {"acme", http.StatusOK},
"missing tenant": {"", http.StatusBadRequest},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/ctx", strings.NewReader(`{"value":"x"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
if tc.tenant != "" {
req.Header.Set("X-Tenant", tc.tenant)
}
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, tc.wantStatus, resp.StatusCode)
})
}
}
type nonValidationErrReq struct {
Body struct {
Name string `json:"name"`
}
}
func (r *nonValidationErrReq) Validate(_ context.Context) error {
return api.Error(http.StatusUnauthorized, "login required")
}
func TestValidator_nonValidationErrorForwarded(t *testing.T) {
t.Parallel()
r := api.New()
api.Post(r, "/auth", func(_ context.Context, _ *nonValidationErrReq) (*api.Void, error) {
return &api.Void{}, nil
})
srv := httptest.NewServer(r)
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/auth", strings.NewReader(`{"name":"x"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
}
func TestRouterValidator_runs(t *testing.T) {
t.Parallel()
type Req struct {
Name string `json:"name"`
}
type Resp struct {
Name string `json:"name"`
}
called := false
r := api.New(api.WithValidator(func(_ any) error {
called = true
return nil
}))
api.Post(r, "/users", func(_ context.Context, req *Req) (*Resp, error) {
return &Resp{Name: req.Name}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/users", strings.NewReader(`{"name":"Bob"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, called, "router validator should have been invoked")
}
func TestRouterValidator_returnsValidationErrors(t *testing.T) {
t.Parallel()
type Req struct {
Name string `json:"name"`
}
type Resp struct {
OK bool `json:"ok"`
}
r := api.New(api.WithValidator(func(_ any) error {
return api.ValidationErrors{
{Field: "name", Message: "router says no"},
}
}))
api.Post(r, "/x", func(_ context.Context, _ *Req) (*Resp, error) {
return &Resp{OK: true}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/x", strings.NewReader(`{"name":"x"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
var pd api.ProblemDetail
require.NoError(t, json.NewDecoder(resp.Body).Decode(&pd))
assert.Equal(t, "Validation Failed", pd.Title)
require.Len(t, pd.Errors, 1)
assert.Equal(t, "name", pd.Errors[0].Field)
}
func TestValidationErrors_Error(t *testing.T) {
t.Parallel()
tests := map[string]struct {
ve api.ValidationErrors
want string
}{
"empty": {api.ValidationErrors{}, "0 validation error(s)"},
"single": {api.ValidationErrors{{Field: "a"}}, "1 validation error(s)"},
"multiple": {api.ValidationErrors{{Field: "a"}, {Field: "b"}, {Field: "c"}}, "3 validation error(s)"},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, tc.ve.Error())
})
}
}
// --- ValidationMode tests ---
type modeReq struct {
Body struct {
Name string `json:"name" minLength:"5"`
}
}
func (r *modeReq) Validate(_ context.Context) error {
return api.ValidationErrors{
{Field: "body.name", Message: "from Validator"},
}
}
func TestValidationMode_Last_PerTypeFirst(t *testing.T) {
t.Parallel()
r := api.New() // default: Last
api.Post(r, "/m", func(_ context.Context, _ *modeReq) (*api.Void, error) {
return &api.Void{}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/m", strings.NewReader(`{"name":"ab"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
var pd api.ProblemDetail
require.NoError(t, json.NewDecoder(resp.Body).Decode(&pd))
require.Len(t, pd.Errors, 1)
assert.Equal(t, "from Validator", pd.Errors[0].Message, "per-type Validator runs first in Last mode")
}
func TestValidationMode_First_ConstraintsFirst(t *testing.T) {
t.Parallel()
r := api.New(api.WithValidationMode(api.ValidateConstraintsFirst))
api.Post(r, "/m", func(_ context.Context, _ *modeReq) (*api.Void, error) {
return &api.Void{}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/m", strings.NewReader(`{"name":"ab"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
var pd api.ProblemDetail
require.NoError(t, json.NewDecoder(resp.Body).Decode(&pd))
require.Len(t, pd.Errors, 1)
assert.Contains(t, pd.Errors[0].Message, "at least 5 characters", "constraint fires first in First mode")
}
type offReq struct {
Body struct {
Name string `json:"name" minLength:"5"`
}
}
func TestValidationMode_Off_NoConstraintRuntime(t *testing.T) {
t.Parallel()
type Resp struct {
Name string `json:"name"`
}
r := api.New(api.WithValidationMode(api.ValidateConstraintsOff))
api.Post(r, "/m", func(_ context.Context, req *offReq) (*Resp, error) {
return &Resp{Name: req.Body.Name}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/m", strings.NewReader(`{"name":"ab"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusOK, resp.StatusCode, "constraint tags must not fire at runtime in Off mode")
}
func TestValidationMode_Off_SpecStillHasConstraints(t *testing.T) {
t.Parallel()
r := api.New(
api.WithTitle("Off Mode Spec"),
api.WithValidationMode(api.ValidateConstraintsOff),
)
api.Post(r, "/m", func(_ context.Context, _ *offReq) (*api.Void, error) {
return &api.Void{}, nil
})
spec := r.Spec()
require.NotNil(t, spec)
// Find the minLength in the generated schema — specifics of the structure
// are framework-internal; we just assert the tag reached the spec.
raw, err := json.Marshal(spec)
require.NoError(t, err)
assert.Contains(t, string(raw), `"minLength":5`, "OpenAPI schema must still include minLength in Off mode")
}
func TestValidationMode_PerRouteOverride(t *testing.T) {
t.Parallel()
r := api.New(api.WithValidationMode(api.ValidateConstraintsFirst))
api.Post(r, "/override",
func(_ context.Context, req *offReq) (*api.Void, error) {
_ = req
return &api.Void{}, nil
},
api.WithMode(api.ValidateConstraintsOff),
)
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/override", strings.NewReader(`{"name":"ab"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
}
// --- ValidationErrorBuilder tests ---
type domainError struct {
Code string `json:"code"`
Msg string `json:"message"`
Fields []api.ValidationError `json:"fields,omitempty"`
}
func (e *domainError) Error() string { return e.Msg }
func (e *domainError) StatusCode() int { return http.StatusUnprocessableEntity }
type domainBuilder struct{}
func (domainBuilder) Build(v api.ValidationErrors) error {
return &domainError{Code: "INVALID_INPUT", Msg: "bad request", Fields: v}
}
func TestValidationErrorBuilder_constraintPath(t *testing.T) {
t.Parallel()
r := api.New(
api.WithValidationErrorBuilder(domainBuilder{}),
api.WithErrorHandler(func(w http.ResponseWriter, _ *http.Request, err error) {
var de *domainError
if errors.As(err, &de) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(de.StatusCode())
assert.NoError(t, json.NewEncoder(w).Encode(de))
return
}
assert.Fail(t, "unexpected error type", "got %T: %v", err, err)
}),
)
api.Post(r, "/b",
func(_ context.Context, _ *offReq) (*api.Void, error) {
return &api.Void{}, nil
},
api.WithMode(api.ValidateConstraintsFirst),
)
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/b", strings.NewReader(`{"name":"ab"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
var de domainError
require.NoError(t, json.NewDecoder(resp.Body).Decode(&de))
assert.Equal(t, "INVALID_INPUT", de.Code)
require.Len(t, de.Fields, 1)
assert.Equal(t, "body.name", de.Fields[0].Field)
}
func TestValidationErrorBuilder_perTypePath(t *testing.T) {
t.Parallel()
r := api.New(
api.WithValidationErrorBuilder(domainBuilder{}),
api.WithErrorHandler(func(w http.ResponseWriter, _ *http.Request, err error) {
var de *domainError
if errors.As(err, &de) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(de.StatusCode())
assert.NoError(t, json.NewEncoder(w).Encode(de))
return
}
assert.Fail(t, "unexpected error type", "got %T: %v", err, err)
}),
)
api.Post(r, "/b", func(_ context.Context, _ *modeReq) (*api.Void, error) {
return &api.Void{}, nil
})
srv := httptest.NewServer(r)
defer srv.Close()
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/b", strings.NewReader(`{"name":"abcdef"}`))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { require.NoError(t, resp.Body.Close()) }()
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
var de domainError
require.NoError(t, json.NewDecoder(resp.Body).Decode(&de))
assert.Equal(t, "INVALID_INPUT", de.Code)
require.Len(t, de.Fields, 1)
assert.Equal(t, "from Validator", de.Fields[0].Message)
}