-
Notifications
You must be signed in to change notification settings - Fork 17
/
request_modifiers_test.go
194 lines (180 loc) · 6.01 KB
/
request_modifiers_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
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package vault
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func Test_printable(t *testing.T) {
cases := map[string]struct {
str string
expected bool
}{
"emtpy": {
str: "",
expected: true,
},
"simple": {
str: "printable",
expected: true,
},
"utf-8": {
str: "Hello, 世界",
expected: true,
},
"not-printable": {
str: "\u200b\u200b\u200b",
expected: false,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.expected, printable(tc.str))
})
}
}
func Test_validateCustomHeaders(t *testing.T) {
cases := map[string]struct {
headers http.Header
expectedError bool
}{
"nil": {
headers: nil,
expectedError: false,
},
"empty": {
headers: http.Header{},
expectedError: false,
},
"single": {
headers: http.Header{"a": []string{"b"}},
expectedError: false,
},
"multiple": {
headers: http.Header{"a": []string{"b"}, "c": []string{"d"}},
expectedError: false,
},
"has-x-vault": {
headers: http.Header{
"a": []string{"b"},
"x-vault-something": []string{"d"},
},
expectedError: true,
},
"has-x-vault-upper": {
headers: http.Header{
"a": []string{"b"},
"X-Vault-Something": []string{"d"},
},
expectedError: true,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
if tc.expectedError {
require.Error(t, validateCustomHeaders(tc.headers))
} else {
require.NoError(t, validateCustomHeaders(tc.headers))
}
})
}
}
func Test_mergeRequestModifiers_overwrite(t *testing.T) {
cases := map[string]struct {
name string
lhs requestModifiers
rhs requestModifiers
expected requestModifiers
}{
"empty": {
lhs: requestModifiers{},
rhs: requestModifiers{},
expected: requestModifiers{},
},
"token-in-lhs": {
lhs: requestModifiers{headers: requestHeaders{token: "token-lhs"}},
rhs: requestModifiers{},
expected: requestModifiers{headers: requestHeaders{token: "token-lhs"}},
},
"token-in-rhs": {
lhs: requestModifiers{},
rhs: requestModifiers{headers: requestHeaders{token: "token-rhs"}},
expected: requestModifiers{headers: requestHeaders{token: "token-rhs"}},
},
"token-in-both": {
lhs: requestModifiers{headers: requestHeaders{token: "token-lhs"}},
rhs: requestModifiers{headers: requestHeaders{token: "token-rhs"}},
expected: requestModifiers{headers: requestHeaders{token: "token-rhs"}},
},
"token-lhs-and-namespace-rhs": {
lhs: requestModifiers{headers: requestHeaders{token: "token-lhs"}},
rhs: requestModifiers{headers: requestHeaders{namespace: "namespace-rhs"}},
expected: requestModifiers{headers: requestHeaders{token: "token-lhs", namespace: "namespace-rhs"}},
},
"custom-headers-in-lhs": {
lhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Type": []string{"image/png"}}}},
rhs: requestModifiers{},
expected: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Type": []string{"image/png"}}}},
},
"custom-headers-in-rhs": {
lhs: requestModifiers{},
rhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Length": []string{"123"}}}},
expected: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Length": []string{"123"}}}},
},
"custom-headers-in-both": {
lhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Type": []string{"image/png"}}}},
rhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Length": []string{"123"}}}},
expected: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"Content-Type": []string{"image/png"}, "Content-Length": []string{"123"}}}},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
mergeRequestModifiers(&tc.lhs, &tc.rhs)
require.Equal(t, tc.expected, tc.lhs)
})
}
}
func Test_mergeRequestModifiers_append(t *testing.T) {
requestCallback1 := func(r *http.Request) {
t.Logf("callback 1: %v", *r)
}
requestCallback2 := func(r *http.Request) {
t.Logf("callback 2: %v", *r)
}
responseCallback := func(req *http.Request, resp *http.Response) {
t.Logf("callback 3: %v, %v", *req, *resp)
}
cases := map[string]struct {
name string
lhs requestModifiers
rhs requestModifiers
expected requestModifiers
}{
"custom-headers": {
lhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"a": []string{"hi"}, "b": nil}}},
rhs: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"b": nil, "c": nil}}},
expected: requestModifiers{headers: requestHeaders{customHeaders: http.Header{"a": []string{"hi"}, "b": nil, "c": nil}}},
},
"request-callbacks": {
lhs: requestModifiers{requestCallbacks: []RequestCallback{requestCallback1}},
rhs: requestModifiers{requestCallbacks: []RequestCallback{requestCallback2}},
expected: requestModifiers{requestCallbacks: []RequestCallback{requestCallback1, requestCallback2}},
},
"response-callbacks": {
lhs: requestModifiers{responseCallbacks: []ResponseCallback{}},
rhs: requestModifiers{responseCallbacks: []ResponseCallback{responseCallback}},
expected: requestModifiers{responseCallbacks: []ResponseCallback{responseCallback}},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
mergeRequestModifiers(&tc.lhs, &tc.rhs)
// testify doesn't currently work with func types; stringify instead
require.Equal(t, fmt.Sprintf("%v", tc.expected.requestCallbacks), fmt.Sprintf("%v", tc.lhs.requestCallbacks))
require.Equal(t, fmt.Sprintf("%v", tc.expected.responseCallbacks), fmt.Sprintf("%v", tc.lhs.responseCallbacks))
require.Equal(t, tc.expected.headers.customHeaders, tc.lhs.headers.customHeaders)
})
}
}