-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
282 lines (251 loc) · 5.97 KB
/
context_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
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
package gintestutil
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
type testStringer struct {
input string
}
func (s testStringer) String() string {
return s.input
}
func TestPrepareRequest_CreatesExpectedContext(t *testing.T) {
t.Parallel()
tests := map[string]struct {
options []RequestOption
expectedBody string
expectedUrl string
expectedMethod string
expectedParams gin.Params
expectedHeaders http.Header
expectedError error
}{
"empty request": {
options: []RequestOption{},
expectedMethod: http.MethodGet,
expectedUrl: "https://example.com",
},
"method post": {
options: []RequestOption{WithMethod(http.MethodPost)},
expectedMethod: http.MethodPost,
expectedUrl: "https://example.com",
},
"with url params": {
options: []RequestOption{
WithUrlParams(
map[string]any{
"one": testStringer{input: "two"},
"three": []string{"four", "five"},
}),
},
expectedMethod: http.MethodGet,
expectedUrl: "https://example.com",
expectedParams: []gin.Param{
{
Key: "one",
Value: "two",
},
{
Key: "three",
Value: "four",
},
{
Key: "three",
Value: "five",
},
},
},
"with query params": {
options: []RequestOption{
WithUrl("https://maarten.dev"),
WithQueryParams(map[string]any{
"one": testStringer{input: "two"},
"three": []string{"four", "five"},
}),
},
expectedMethod: http.MethodGet,
expectedUrl: "https://maarten.dev?one=two&three=four&three=five",
},
"with headers": {
options: []RequestOption{
WithUrl("https://maarten.dev"),
WithHeaders(http.Header{"X-Test-Header": []string{"A", "B"}}),
},
expectedMethod: http.MethodGet,
expectedUrl: "https://maarten.dev",
expectedHeaders: http.Header{
"X-Test-Header": []string{"A", "B"},
},
},
"maarten.dev url": {
options: []RequestOption{WithUrl("https://maarten.dev")},
expectedUrl: "https://maarten.dev",
expectedMethod: http.MethodGet,
},
"json body": {
options: []RequestOption{WithJsonBody(t, map[string]any{"one": "two", "three": 4})},
expectedMethod: http.MethodGet,
expectedUrl: "https://example.com",
expectedBody: `{"one": "two", "three": 4}`,
},
"raw body": {
options: []RequestOption{WithBody([]byte("a b c"))},
expectedMethod: http.MethodGet,
expectedUrl: "https://example.com",
expectedBody: "a b c",
},
"expected error on nonsensical request": {
options: []RequestOption{WithUrl("://://::::///::::")},
expectedError: &url.Error{Op: "parse", URL: "://://::::///::::", Err: errors.New("missing protocol scheme")},
},
}
for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
t.Parallel()
// Arrange
mockT := new(mockT)
// Act
context, writer := PrepareRequest(mockT, testData.options...)
// Assert
assert.NotNil(t, writer)
if testData.expectedError != nil {
assert.Equal(t, testData.expectedError, mockT.ErrorCalls[0])
return
}
assert.Len(t, mockT.ErrorCalls, 0)
assert.Equal(t, testData.expectedMethod, context.Request.Method)
assert.Equal(t, testData.expectedUrl, context.Request.URL.String())
assert.Equal(t, testData.expectedHeaders, context.Request.Header)
assert.ElementsMatch(t, testData.expectedParams, context.Params)
if testData.expectedBody != "" {
if body, err := io.ReadAll(context.Request.Body); err != nil {
assert.Equal(t, []byte(testData.expectedBody), body)
}
}
})
}
}
func TestWithJsonBody_CallsErrorOnFaultyJson(t *testing.T) {
t.Parallel()
// Arrange
mock := &mockT{}
input := map[bool]string{
true: "A boolean map is not a thing in json",
false: "so it won't work :-)",
}
// Act
_ = WithJsonBody(mock, input)
// Assert
if assert.Len(t, mock.ErrorCalls, 1) {
assert.IsType(t, mock.ErrorCalls[0], &json.UnsupportedTypeError{})
}
}
func TestApplyQueryParams_SetsExpectedValues(t *testing.T) {
t.Parallel()
tests := map[string]struct {
input map[string]any
expected url.Values
}{
"empty": {
input: map[string]any{},
expected: map[string][]string{},
},
"simple": {
input: map[string]any{
"a": "b",
"c": "d",
},
expected: map[string][]string{
"a": {"b"},
"c": {"d"},
},
},
"multi": {
input: map[string]any{
"a": []string{"a", "b"},
"c": []string{"c", "d"},
},
expected: map[string][]string{
"a": {"a", "b"},
"c": {"c", "d"},
},
},
"level 1": {
input: map[string]any{
"a": map[string]any{"aa": "bb"},
"c": map[string]any{"cc": "dd"},
},
expected: map[string][]string{
"a[aa]": {"bb"},
"c[cc]": {"dd"},
},
},
"level 2": {
input: map[string]any{
"a": map[string]any{
"aa": map[string]any{
"aaa": "bbb",
},
},
"c": map[string]any{
"cc": map[string]any{
"ccc": "ddd",
},
},
},
expected: map[string][]string{
"a[aa][aaa]": {"bbb"},
"c[cc][ccc]": {"ddd"},
},
},
"level 6m ": {
input: map[string]any{
"a": map[string]any{
"aa": map[string]any{
"aaa": map[string]any{
"aaaa": map[string]any{
"aaaaa": map[string]any{
"aaaaaa": "bbbbbb",
},
},
},
},
},
"c": map[string]any{
"cc": map[string]any{
"ccc": map[string]any{
"cccc": map[string]any{
"ccccc": map[string]any{
"cccccc": "dddddd",
},
},
},
},
},
},
expected: map[string][]string{
"a[aa][aaa][aaaa][aaaaa][aaaaaa]": {"bbbbbb"},
"c[cc][ccc][cccc][ccccc][cccccc]": {"dddddd"},
},
},
}
for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
t.Parallel()
// Arrange
params := url.Values{}
// Act
applyQueryParams(testData.input, params, "")
// Assert
assert.Equal(t, testData.expected, params)
})
}
}