-
Notifications
You must be signed in to change notification settings - Fork 7
/
http_test.go
331 lines (318 loc) · 10.3 KB
/
http_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
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
package cloudconfigclient_test
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"github.com/Piszmog/cloudconfigclient/v2"
"github.com/stretchr/testify/require"
)
type RoundTripFunc func(req *http.Request) *http.Response
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// NewMockHttpClient creates a mocked HTTP Client
func NewMockHttpClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
// NewMockHttpResponse creates a mocked HTTP response
func NewMockHttpResponse(code int, body string) *http.Response {
return &http.Response{
StatusCode: code,
// Send response to be tested
Body: io.NopCloser(bytes.NewBufferString(body)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
}
func TestHTTPClient_Get(t *testing.T) {
tests := []struct {
name string
baseURL string
auth string
paths []string
params map[string]string
response *http.Response
checker func(*testing.T, *http.Request)
err error
}{
{
name: "Invalid URL",
baseURL: "\n",
err: errors.New("failed to create url: failed to parse url \n: parse \"\\n\": net/url: invalid control character in URL"),
},
{
name: "HTTP Error",
baseURL: "http://foobar",
err: errors.New("failed to retrieve from http://foobar: Get \"http://foobar\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"),
},
{
name: "Correct Request URI",
baseURL: "http://something",
response: NewMockHttpResponse(200, ""),
checker: func(t *testing.T, request *http.Request) {
require.Equal(t, "/", request.URL.RequestURI())
require.Empty(t, request.Header.Get("Authorization"))
},
},
{
name: "Correct Request URI With Path",
baseURL: "http://something",
paths: []string{"/foo", "/bar"},
response: NewMockHttpResponse(200, ""),
checker: func(t *testing.T, request *http.Request) {
require.Equal(t, "/foo/bar", request.URL.RequestURI())
},
},
{
name: "Correct Request URI With Params",
baseURL: "http://something",
params: map[string]string{"field": "value"},
response: NewMockHttpResponse(200, ""),
checker: func(t *testing.T, request *http.Request) {
require.Equal(t, "/?field=value", request.URL.RequestURI())
},
},
{
name: "Correct Request URI With Path and Params",
baseURL: "http://something",
paths: []string{"/foo", "/bar"},
params: map[string]string{"field": "value"},
response: NewMockHttpResponse(200, ""),
checker: func(t *testing.T, request *http.Request) {
require.Equal(t, "/foo/bar?field=value", request.URL.RequestURI())
},
},
{
name: "Correct Request URI With Auth",
baseURL: "http://something",
auth: "Basic dXNlcjpwYXNz",
response: NewMockHttpResponse(200, ""),
checker: func(t *testing.T, request *http.Request) {
require.Equal(t, "Basic dXNlcjpwYXNz", request.Header.Get("Authorization"))
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := NewMockHttpClient(func(req *http.Request) *http.Response {
if test.checker != nil {
test.checker(t, req)
}
return test.response
})
httpClient := cloudconfigclient.HTTPClient{BaseURL: test.baseURL, Client: client, Authorization: test.auth}
_, err := httpClient.Get(test.paths, test.params)
if err != nil {
require.Error(t, err)
require.Equal(t, test.err.Error(), err.Error())
} else {
require.NoError(t, err)
}
})
}
}
func TestHTTPClient_GetResource(t *testing.T) {
tests := []struct {
name string
paths []string
params map[string]string
destination interface{}
response *http.Response
expected interface{}
err error
}{
{
name: "HTTP Error",
paths: []string{"file.yaml"},
err: errors.New("failed to retrieve from http://something/file.yaml: Get \"http://something/file.yaml\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"),
},
{
name: "Internal Server Error",
paths: []string{"file.yaml"},
response: NewMockHttpResponse(http.StatusInternalServerError, "Invalid HTTP Call"),
err: errors.New("server responded with status code '500' and body 'Invalid HTTP Call'"),
},
{
name: "YAML Response",
paths: []string{"file.yaml"},
params: map[string]string{"useDefault": "true"},
destination: make(map[string]interface{}),
response: NewMockHttpResponse(http.StatusOK, `foo: bar`),
expected: map[string]interface{}{"foo": "bar"},
},
{
name: "YAML Response Malformed",
paths: []string{"file.yaml"},
params: map[string]string{"useDefault": "true"},
destination: make(map[string]interface{}),
response: NewMockHttpResponse(http.StatusOK, ""),
err: errors.New("failed to decode response from url: EOF"),
},
{
name: "YML Response",
paths: []string{"file.yml"},
params: map[string]string{"useDefault": "true"},
destination: make(map[string]interface{}),
response: NewMockHttpResponse(http.StatusOK, `foo: bar`),
expected: map[string]interface{}{"foo": "bar"},
},
{
name: "JSON Response",
paths: []string{"file.json"},
params: map[string]string{"useDefault": "true"},
destination: make(map[string]interface{}),
response: NewMockHttpResponse(http.StatusOK, `{"foo":"bar"}`),
expected: map[string]interface{}{"foo": "bar"},
},
{
name: "JSON Response Malformed",
paths: []string{"file.json"},
params: map[string]string{"useDefault": "true"},
destination: make(map[string]interface{}),
response: NewMockHttpResponse(http.StatusOK, `{"foo":"bar"`),
err: errors.New("failed to decode response from url: unexpected EOF"),
},
{
name: "XML Response",
paths: []string{"file.xml"},
params: map[string]string{"useDefault": "true"},
destination: new(xmlResp),
response: NewMockHttpResponse(http.StatusOK, `"<data><foo>bar</foo></data>"`),
expected: &xmlResp{Foo: "bar"},
},
{
name: "XML Response Malformed",
paths: []string{"file.xml"},
params: map[string]string{"useDefault": "true"},
destination: new(xmlResp),
response: NewMockHttpResponse(http.StatusOK, `"<data><foo>bar</foo></data"`),
err: errors.New("failed to decode response from url: XML syntax error on line 1: invalid characters between </data and >"),
},
{
name: "Read Error",
paths: []string{"file.yml"},
params: map[string]string{"useDefault": "true"},
destination: new(xmlResp),
response: &http.Response{
StatusCode: http.StatusOK,
// Send response to be tested
Body: errorReader{},
// Must be set to non-nil value or it panics
Header: make(http.Header),
},
err: errors.New("failed to decode response from url: yaml: input error: failed"),
},
{
name: "Internal Error Read Error",
paths: []string{"file.yml"},
params: map[string]string{"useDefault": "true"},
destination: new(xmlResp),
response: &http.Response{
StatusCode: http.StatusInternalServerError,
// Send response to be tested
Body: errorReader{},
// Must be set to non-nil value or it panics
Header: make(http.Header),
},
err: errors.New("failed to read body with status code '500': failed"),
},
{
name: "No Resource Specified",
destination: new(xmlResp),
err: errors.New("no resource specified to be retrieved"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := NewMockHttpClient(func(req *http.Request) *http.Response {
return test.response
})
httpClient := cloudconfigclient.HTTPClient{BaseURL: "http://something", Client: client}
err := httpClient.GetResource(test.paths, test.params, &test.destination)
if err != nil {
require.Error(t, err)
require.Equal(t, test.err.Error(), err.Error())
} else {
require.NoError(t, err)
require.Equal(t, test.expected, test.destination)
}
})
}
}
func TestHTTPClient_GetResourceRaw(t *testing.T) {
tests := []struct {
name string
paths []string
params map[string]string
response *http.Response
expected []byte
err error
}{
{
name: "HTTP Error",
paths: []string{"file.text"},
err: errors.New("failed to retrieve from http://something/file.text: Get \"http://something/file.text\": http: RoundTripper implementation (cloudconfigclient_test.RoundTripFunc) returned a nil *Response with a nil error"),
},
{
name: "Internal Server Error",
paths: []string{"file.text"},
response: NewMockHttpResponse(http.StatusInternalServerError, "Invalid HTTP Call"),
err: errors.New("server responded with status code '500' and body 'Invalid HTTP Call'"),
},
{
name: "Text Response",
paths: []string{"file.text"},
params: map[string]string{"useDefault": "true"},
response: NewMockHttpResponse(http.StatusOK, `foo-bar`),
expected: []byte("foo-bar"),
},
{
name: "Read Error",
paths: []string{"file.text"},
params: map[string]string{"useDefault": "true"},
response: &http.Response{
StatusCode: http.StatusOK,
// Send response to be tested
Body: errorReader{},
// Must be set to non-nil value or it panics
Header: make(http.Header),
},
err: errors.New("failed to read body with status code '200': failed"),
},
{
name: "No Resource Specified",
err: errors.New("no resource specified to be retrieved"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := NewMockHttpClient(func(req *http.Request) *http.Response {
return test.response
})
httpClient := cloudconfigclient.HTTPClient{BaseURL: "http://something", Client: client}
resp, err := httpClient.GetResourceRaw(test.paths, test.params)
if err != nil {
require.Error(t, err)
require.Equal(t, test.err.Error(), err.Error())
} else {
require.NoError(t, err)
require.Equal(t, test.expected, resp)
}
})
}
}
type errorReader struct {
}
func (e errorReader) Read(p []byte) (n int, err error) {
return 0, errors.New("failed")
}
func (e errorReader) Close() error {
return errors.New("failed")
}
type xmlResp struct {
Foo string `xml:"foo"`
}