-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
119 lines (106 loc) · 2.55 KB
/
utils_test.go
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
package corset
import (
"net/http"
"strings"
"testing"
)
func TestDeriveHeaders(t *testing.T) {
raw := "x-test-1,x-test-2,x-test-3"
req, _ := http.NewRequest(http.MethodGet, "whatever", nil)
req.Header.Add(requestHeadersHeader, "x-test-1, x-test-2, x-test-3")
expected := strings.Split(raw, ",")
actual := deriveHeaders(req)
for i := range expected {
if expected[i] != actual[i] {
t.Errorf("expected %s but got %s", expected[i], actual[i])
}
}
}
func TestIsPreflightRequest(t *testing.T) {
type testCase struct {
name string
method string
requestMethod bool
expected bool
withOrigin bool
}
tests := []testCase{
{
name: "Preflight",
method: http.MethodOptions,
requestMethod: true,
expected: true,
withOrigin: true,
},
{
name: "NonPreflightMethodGET",
method: http.MethodGet,
requestMethod: false,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightMethodHEAD",
method: http.MethodHead,
requestMethod: false,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightMethodPOST",
method: http.MethodPost,
requestMethod: false,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightMethodWithMethodRequestGET",
method: http.MethodGet,
requestMethod: true,
expected: false,
},
{
name: "NonPreflightMethodWithMethodRequestHEAD",
method: http.MethodHead,
requestMethod: true,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightMethodWithMethodRequestPOST",
method: http.MethodPost,
requestMethod: true,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightNoMethodRequest",
method: http.MethodOptions,
requestMethod: false,
expected: false,
withOrigin: true,
},
{
name: "NonPreflightNoOrigin",
method: http.MethodOptions,
requestMethod: true,
expected: false,
withOrigin: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req, _ := http.NewRequest(test.method, "test", nil)
if test.requestMethod {
req.Header.Add(requestMethodHeader, http.MethodPut)
}
if test.withOrigin {
req.Header.Add(originHeader, "test")
}
actual := isPreflightRequest(req)
if actual != test.expected {
t.Errorf("expected isPreflightRequest to return %v but got %v", test.expected, actual)
}
})
}
}