-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_validate_test.go
More file actions
102 lines (89 loc) · 2.01 KB
/
request_validate_test.go
File metadata and controls
102 lines (89 loc) · 2.01 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
package httpsuite
import (
"net/http"
"sync"
"testing"
)
func TestValidateRequest(t *testing.T) {
t.Parallel()
problem := &ProblemDetails{
Type: GetProblemTypeURL("validation_error"),
Title: "Validation Error",
Status: http.StatusBadRequest,
Detail: "One or more fields failed validation.",
}
if got := ValidateRequest(&testRequest{}, nil); got != nil {
t.Fatalf("expected nil validation problem, got %#v", got)
}
if got := ValidateRequest(&testRequest{}, stubValidator{problem: problem}); got != problem {
t.Fatalf("expected validation problem to be returned")
}
}
func TestValidationProblemStatus(t *testing.T) {
t.Parallel()
tests := []struct {
name string
problem *ProblemDetails
want int
}{
{
name: "nil problem",
want: http.StatusBadRequest,
},
{
name: "valid status",
problem: &ProblemDetails{
Status: http.StatusUnprocessableEntity,
},
want: http.StatusUnprocessableEntity,
},
{
name: "invalid status",
problem: &ProblemDetails{
Status: 0,
},
want: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validationProblemStatus(tt.problem); got != tt.want {
t.Fatalf("expected status %d, got %d", tt.want, got)
}
})
}
}
func TestDefaultValidatorLifecycle(t *testing.T) {
ClearValidator()
t.Cleanup(ClearValidator)
if DefaultValidator() != nil {
t.Fatalf("expected nil default validator")
}
validator := stubValidator{}
SetValidator(validator)
if DefaultValidator() == nil {
t.Fatalf("expected default validator to be set")
}
ClearValidator()
if DefaultValidator() != nil {
t.Fatalf("expected default validator to be cleared")
}
}
func TestDefaultValidatorConcurrentAccess(t *testing.T) {
ClearValidator()
t.Cleanup(ClearValidator)
var wg sync.WaitGroup
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if i%2 == 0 {
SetValidator(stubValidator{})
} else {
_ = DefaultValidator()
ClearValidator()
}
}(i)
}
wg.Wait()
}