forked from coregx/fursy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_test.go
More file actions
565 lines (466 loc) · 13.9 KB
/
Copy pathvalidation_test.go
File metadata and controls
565 lines (466 loc) · 13.9 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright 2025 coregx. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fursy
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
// mockValidator is a mock validator for testing.
type mockValidator struct {
shouldFail bool
errors ValidationErrors
}
func (m *mockValidator) Validate(_ any) error {
if m.shouldFail {
if m.errors != nil {
return m.errors
}
return ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
{Field: "age", Tag: "min", Message: "age must be at least 18"},
}
}
return nil
}
// TestValidationError tests ValidationError.Error().
func TestValidationError(t *testing.T) {
tests := []struct {
name string
err ValidationError
wantMsg string
}{
{
name: "with message",
err: ValidationError{
Field: "email",
Tag: "required",
Message: "email is required",
},
wantMsg: "email is required",
},
{
name: "without message",
err: ValidationError{
Field: "age",
Tag: "min",
},
wantMsg: "field 'age' failed 'min' validation",
},
{
name: "nested field",
err: ValidationError{
Field: "Address.City",
Tag: "required",
Message: "city is required",
},
wantMsg: "city is required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.wantMsg {
t.Errorf("Error() = %q, want %q", got, tt.wantMsg)
}
})
}
}
// TestValidationErrors_Error tests ValidationErrors.Error().
func TestValidationErrors_Error(t *testing.T) {
tests := []struct {
name string
errors ValidationErrors
wantMsg string
}{
{
name: "empty",
errors: ValidationErrors{},
wantMsg: "validation failed",
},
{
name: "single error",
errors: ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
},
wantMsg: "email is required",
},
{
name: "multiple errors",
errors: ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
{Field: "age", Tag: "min", Message: "age must be at least 18"},
},
wantMsg: "validation failed: email is required; age must be at least 18",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.errors.Error(); got != tt.wantMsg {
t.Errorf("Error() = %q, want %q", got, tt.wantMsg)
}
})
}
}
// TestValidationErrors_Fields tests ValidationErrors.Fields().
func TestValidationErrors_Fields(t *testing.T) {
errs := ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
{Field: "age", Tag: "min", Message: "age must be at least 18"},
{Field: "password", Tag: "min", Message: "password must be at least 8 characters"},
}
fields := errs.Fields()
if len(fields) != 3 {
t.Errorf("Fields() returned %d fields, want 3", len(fields))
}
expectedFields := map[string]string{
"email": "email is required",
"age": "age must be at least 18",
"password": "password must be at least 8 characters",
}
for field, expectedMsg := range expectedFields {
if msg, ok := fields[field]; !ok {
t.Errorf("Fields() missing field %q", field)
} else if msg != expectedMsg {
t.Errorf("Fields()[%q] = %q, want %q", field, msg, expectedMsg)
}
}
}
// TestValidationErrors_IsEmpty tests ValidationErrors.IsEmpty().
func TestValidationErrors_IsEmpty(t *testing.T) {
tests := []struct {
name string
errors ValidationErrors
want bool
}{
{
name: "empty",
errors: ValidationErrors{},
want: true,
},
{
name: "nil",
errors: nil,
want: true,
},
{
name: "not empty",
errors: ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.errors.IsEmpty(); got != tt.want {
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
}
})
}
}
// TestValidationErrors_Add tests ValidationErrors.Add().
func TestValidationErrors_Add(t *testing.T) {
var errs ValidationErrors
if !errs.IsEmpty() {
t.Error("errs should be empty initially")
}
errs.Add("email", "required", "email is required")
if errs.IsEmpty() {
t.Error("errs should not be empty after Add()")
}
if len(errs) != 1 {
t.Errorf("len(errs) = %d, want 1", len(errs))
}
if errs[0].Field != "email" {
t.Errorf("Field = %q, want %q", errs[0].Field, "email")
}
if errs[0].Tag != "required" {
t.Errorf("Tag = %q, want %q", errs[0].Tag, "required")
}
if errs[0].Message != "email is required" {
t.Errorf("Message = %q, want %q", errs[0].Message, "email is required")
}
// Add more.
errs.Add("age", "min", "age must be at least 18")
if len(errs) != 2 {
t.Errorf("len(errs) = %d, want 2", len(errs))
}
}
// TestValidationErrors_AddError tests ValidationErrors.AddError().
func TestValidationErrors_AddError(t *testing.T) {
var errs ValidationErrors
err := ValidationError{
Field: "email",
Tag: "required",
Value: "",
Message: "email is required",
}
errs.AddError(err)
if len(errs) != 1 {
t.Errorf("len(errs) = %d, want 1", len(errs))
}
if errs[0].Field != "email" {
t.Errorf("Field = %q, want %q", errs[0].Field, "email")
}
}
// TestRouter_SetValidator tests Router.SetValidator().
func TestRouter_SetValidator(t *testing.T) {
r := New()
if r.validator != nil {
t.Error("validator should be nil by default")
}
validator := &mockValidator{}
r.SetValidator(validator)
if r.validator == nil {
t.Error("validator should be set")
}
// Test chainability.
result := r.SetValidator(validator)
if result != r {
t.Error("SetValidator() should return the router for chaining")
}
}
// TestContext_Bind_NoValidator tests binding without validator.
func TestContext_Bind_NoValidator(t *testing.T) {
r := New()
type Request struct {
Name string `json:"name"`
Email string `json:"email"`
}
r.POST("/test", func(c *Box[Request, Request]) error {
if c.ReqBody == nil {
return c.BadRequest(Request{Name: "Request body is nil"})
}
return c.OK(*c.ReqBody)
})
body := `{"name":"John","email":"john@example.com"}`
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected status 200, got %d", w.Code)
}
// Should succeed even with invalid email (no validator).
body = `{"name":"John","email":"invalid"}`
req = httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w = httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected status 200 (no validation), got %d", w.Code)
}
}
// TestContext_Bind_WithValidator tests binding with validator.
func TestContext_Bind_WithValidator(t *testing.T) {
r := New()
type Request struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Set failing validator.
r.SetValidator(&mockValidator{shouldFail: true})
r.POST("/test", func(c *Box[Request, Request]) error {
// This should not be reached due to validation failure.
return c.OK(*c.ReqBody)
})
body := `{"name":"John","email":"john@example.com"}`
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
// Should return 500 (handler returned error).
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected status 422 (validation failed), got %d", w.Code)
}
}
// TestContext_Bind_ValidatorSuccess tests binding with passing validator.
func TestContext_Bind_ValidatorSuccess(t *testing.T) {
r := New()
type Request struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Set passing validator.
r.SetValidator(&mockValidator{shouldFail: false})
r.POST("/test", func(c *Box[Request, Request]) error {
if c.ReqBody == nil {
return c.BadRequest(Request{Name: "Request body is nil"})
}
return c.OK(*c.ReqBody)
})
body := `{"name":"John","email":"john@example.com"}`
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected status 200, got %d", w.Code)
}
}
// TestContext_Bind_EmptyType tests binding with Empty type (no validation).
func TestContext_Bind_EmptyType(t *testing.T) {
r := New()
// Set failing validator - should not be called for Empty type.
r.SetValidator(&mockValidator{shouldFail: true})
r.GET("/test", func(c *Box[Empty, string]) error {
return c.OK("success")
})
req := httptest.NewRequest(http.MethodGet, "/test", http.NoBody)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
// Should succeed even with failing validator (Empty type skips binding/validation).
if w.Code != 200 {
t.Errorf("expected status 200 (Empty type), got %d", w.Code)
}
}
// TestContext_Bind_CustomValidationErrors tests custom validation errors.
func TestContext_Bind_CustomValidationErrors(t *testing.T) {
r := New()
type Request struct {
Email string `json:"email"`
Age int `json:"age"`
}
// Set validator with custom errors.
customErrors := ValidationErrors{
{Field: "email", Tag: "email", Message: "must be a valid email address"},
{Field: "age", Tag: "gte", Message: "must be at least 18 years old"},
}
r.SetValidator(&mockValidator{shouldFail: true, errors: customErrors})
r.POST("/test", func(c *Box[Request, Request]) error {
return c.OK(*c.ReqBody)
})
body := `{"email":"invalid","age":15}`
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
// Validation should fail.
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected status 422 (validation failed), got %d", w.Code)
}
}
// TestValidationErrors_AsError tests using ValidationErrors as error.
func TestValidationErrors_AsError(t *testing.T) {
validationErrs := ValidationErrors{
{Field: "email", Tag: "required", Message: "email is required"},
}
// Should be usable as error.
var err error = validationErrs
if validationErrs.IsEmpty() {
t.Error("ValidationErrors should not be empty")
}
// Should be detectable with errors.As.
var ve ValidationErrors
if !errors.As(err, &ve) {
t.Error("should be able to use errors.As with ValidationErrors")
}
if len(ve) != 1 {
t.Errorf("expected 1 validation error, got %d", len(ve))
}
}
// CreateUserRequest is a test struct for integration tests.
type CreateUserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
// UserResponse is a test struct for integration tests.
type UserResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// emailValidator is a mock validator that checks email contains "@".
type emailValidator struct{}
func (v *emailValidator) Validate(obj any) error {
req, ok := obj.(*CreateUserRequest)
if !ok {
return nil
}
var errs ValidationErrors
if req.Email == "" {
errs.Add("email", "required", "email is required")
} else if !contains(req.Email, "@") {
errs.Add("email", "email", "must be a valid email address")
}
if req.Age < 18 {
errs.Add("age", "gte", "must be at least 18 years old")
}
if !errs.IsEmpty() {
return errs
}
return nil
}
// Helper function for integration test.
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || substr == "" || findSubstr(s, substr))
}
func findSubstr(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
// TestValidation_Integration tests full integration scenario.
func TestValidation_Integration(t *testing.T) {
r := New()
r.SetValidator(&emailValidator{})
r.POST("/users", func(c *Box[CreateUserRequest, UserResponse]) error {
// If we get here, validation passed.
return c.Created("/users/1", UserResponse{
ID: 1,
Name: c.ReqBody.Name,
Email: c.ReqBody.Email,
})
})
// Test 1: Valid request.
t.Run("valid request", func(t *testing.T) {
body := `{"name":"John","email":"john@example.com","age":25}`
req := httptest.NewRequest(http.MethodPost, "/users", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Errorf("expected status 201, got %d", w.Code)
}
})
// Test 2: Invalid email.
t.Run("invalid email", func(t *testing.T) {
body := `{"name":"John","email":"invalid","age":25}`
req := httptest.NewRequest(http.MethodPost, "/users", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected status 422 (validation failed), got %d", w.Code)
}
})
// Test 3: Age too young.
t.Run("age too young", func(t *testing.T) {
body := `{"name":"John","email":"john@example.com","age":15}`
req := httptest.NewRequest(http.MethodPost, "/users", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected status 422 (validation failed), got %d", w.Code)
}
})
// Test 4: Multiple validation errors.
t.Run("multiple errors", func(t *testing.T) {
body := `{"name":"John","email":"invalid","age":15}`
req := httptest.NewRequest(http.MethodPost, "/users", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusUnprocessableEntity {
t.Errorf("expected status 422 (validation failed), got %d", w.Code)
}
})
}