-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpclient_test.go
488 lines (440 loc) · 13.3 KB
/
httpclient_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package httpclient
import (
"bufio"
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
"golang.org/x/time/rate"
"github.com/moul/http2curl"
"github.com/stretchr/testify/assert"
)
type options struct {
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
Search string `url:"search,omitempty"`
}
type message struct {
Text string
}
func (m message) String() string {
return m.Text
}
// nolint: gochecknoglobals
var (
baseurl = "https://hostname.domain"
baseurlInvalid = "https://show you how deep the rabbit hole goes.domain"
username = "cognitive"
password = "distortions"
contentType = "application/yaml"
testMessage = message{
Text: "it's only rock'n'roll",
}
)
// nolint: funlen, gocognit, gocyclo
func TestClient(t *testing.T) {
t.Run("query options", func(t *testing.T) {
opt := options{1, 10, "name=testHost"}
u, err := QueryOptions(baseurl, opt)
assert.Nil(t, err)
assert.Equal(t, "https://hostname.domain?page=1&per_page=10&search=name%3DtestHost", u)
})
t.Run("query options nil", func(t *testing.T) {
var opt *options
u, err := QueryOptions(baseurl, opt)
assert.Nil(t, err)
assert.Equal(t, baseurl, u)
})
t.Run("baseurl invalid", func(t *testing.T) {
u, err := QueryOptions(baseurlInvalid, nil)
assert.NotNil(t, err)
assert.Equal(t, baseurlInvalid, u)
})
t.Run("query options not query values", func(t *testing.T) {
opt := 42
u, err := QueryOptions(baseurl, opt)
assert.NotNil(t, err)
assert.Equal(t, baseurl, u)
})
t.Run("new client invalid baseurl", func(t *testing.T) {
_, err := New(baseurlInvalid)
assert.NotNil(t, err)
})
t.Run("new client valid baseurl", func(t *testing.T) {
c, err := New(baseurl)
assert.Nil(t, err)
assert.NotNil(t, c)
u, _ := url.Parse(baseurl)
assert.Equal(t, u, c.BaseURL)
})
t.Run("new client valid baseurl invalid username", func(t *testing.T) {
_, err := New(baseurl, WithUsername(""))
assert.NotNil(t, err)
})
t.Run("new client valid baseurl valid username", func(t *testing.T) {
c, err := New(baseurl, WithUsername(username))
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, username, c.username)
})
t.Run("new client valid baseurl invalid password", func(t *testing.T) {
_, err := New(baseurl, WithPassword(""))
assert.NotNil(t, err)
})
t.Run("new client valid baseurl valid password", func(t *testing.T) {
c, err := New(baseurl, WithPassword(password))
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, password, c.password)
})
t.Run("new client with headers", func(t *testing.T) {
customHeader := http.Header{
"X-Requested-By": []string{"test"},
}
c, err := New(baseurl, WithHeader(customHeader))
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "/test", nil)
assert.Nil(t, err)
assert.Equal(t, req.Header["X-Requested-By"], []string{"test"})
assert.Contains(t, req.Header, "Content-Type")
assert.Contains(t, req.Header, "Accept")
})
t.Run("new client with headers and basic auth", func(t *testing.T) {
username := "user1" // nolint: govet
password := "123456" // nolint: govet
fakeAuthHeader := http.Header{
"Authorization": []string{"fake"},
}
c, err := New(baseurl, WithHeader(fakeAuthHeader), WithUsername(username), WithPassword(password))
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "/test", nil)
assert.Nil(t, err)
user, passwd, ok := req.BasicAuth()
assert.True(t, ok)
assert.Equal(t, user, username)
assert.Equal(t, passwd, passwd)
})
t.Run("new client valid baseurl valid HTTP client", func(t *testing.T) {
httpC := &http.Client{}
c, err := New(baseurl, WithHTTPClient(httpC))
assert.Nil(t, err)
assert.NotNil(t, c)
assert.True(t, httpC == c.client)
})
t.Run("new client valid baseurl valid rate limiter", func(t *testing.T) {
limiter := rate.NewLimiter(1.0, 1)
c, err := New(baseurl, WithRateLimiter(limiter))
assert.Nil(t, err)
assert.NotNil(t, c)
assert.True(t, limiter == c.limiter)
})
t.Run("new client valid baseurl valid content type", func(t *testing.T) {
_, err := New(baseurl, WithContentType(contentType))
assert.Nil(t, err)
})
t.Run("new client valid baseurl invalid content type", func(t *testing.T) {
_, err := New(baseurl, WithContentType(""))
assert.NotNil(t, err)
})
t.Run("new request with Marshaler == nil", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover())
}()
c, _ := New(baseurl)
c.Marshaler = nil
_, _ = c.NewRequest(http.MethodGet, "node", nil)
assert.Fail(t, "NewRequest did not panic")
})
t.Run("new request with RequestCallback == nil", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover())
}()
c, _ := New(baseurl)
c.RequestCallback = nil
_, _ = c.NewRequest(http.MethodGet, "node", nil)
assert.Fail(t, "NewRequest did not panic")
})
t.Run("new request using RequestCallback to dump request", func(t *testing.T) {
c, _ := New(baseurl)
var dump []byte
c.RequestCallback = func(r *http.Request) *http.Request {
dump, _ = httputil.DumpRequestOut(r, true)
return r
}
_, _ = c.NewRequest(http.MethodGet, "node", nil)
assert.True(t, len(dump) > 0)
})
t.Run("new request using RequestCallback to dump request", func(t *testing.T) {
c, _ := New(baseurl)
var command *http2curl.CurlCommand
c.RequestCallback = func(r *http.Request) *http.Request {
command, _ = http2curl.GetCurlCommand(r)
return r
}
_, _ = c.NewRequest(http.MethodGet, "node", nil)
assert.True(t, len(command.String()) > 0)
})
t.Run("new request without basic auth", func(t *testing.T) {
c, err := New(baseurl)
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "node", nil)
assert.Nil(t, err)
assert.NotNil(t, req)
_, _, ok := req.BasicAuth()
assert.False(t, ok)
})
t.Run("new request with basic auth", func(t *testing.T) {
c, err := New(baseurl, WithUsername(username), WithPassword(password))
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "node", nil)
assert.Nil(t, err)
assert.NotNil(t, req)
u, p, ok := req.BasicAuth()
assert.True(t, ok)
assert.Equal(t, username, u)
assert.Equal(t, password, p)
})
t.Run("new request with content type application/json", func(t *testing.T) {
c, err := New(baseurl)
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
buf := new(bytes.Buffer)
_, err = io.Copy(buf, req.Body)
assert.Nil(t, err)
assert.Equal(t, "{\"Text\":\"it's only rock'n'roll\"}\n", buf.String())
})
t.Run("new request with content type application/yaml", func(t *testing.T) {
c, err := New(baseurl)
c.ContentType = ContentTypeYAML
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
buf := new(bytes.Buffer)
_, err = io.Copy(buf, req.Body)
assert.Nil(t, err)
assert.Equal(t, "text: it's only rock'n'roll\n", buf.String())
})
t.Run("new request with content type text/plain", func(t *testing.T) {
c, err := New(baseurl)
c.ContentType = ContentTypeText
assert.Nil(t, err)
assert.NotNil(t, c)
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
buf := new(bytes.Buffer)
_, err = io.Copy(buf, req.Body)
assert.Nil(t, err)
assert.Equal(t, "it's only rock'n'roll", buf.String())
})
t.Run("new request with unknown content type ", func(t *testing.T) {
c, err := New(baseurl)
c.ContentType = "unknown/unknown"
assert.Nil(t, err)
assert.NotNil(t, c)
_, err = c.NewRequest(http.MethodGet, "node", struct{ Message string }{Message: "it's only rock'n'roll"})
assert.NotNil(t, err)
})
// Test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/node" {
http.Error(w, "invalid", http.StatusNotFound)
return
}
// check post data
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
switch r.Header.Get("Content-Type") {
case ContentTypeJSON:
w.Header().Set("Content-Type", ContentTypeJSON)
case ContentTypeYAML:
w.Header().Set("Content-Type", ContentTypeYAML)
case ContentTypeText:
w.Header().Set("Content-Type", ContentTypeText)
default:
w.Header().Set("Content-Type", "unknown/unknown")
}
_, err = w.Write(body)
if err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
t.Log("test server URL:", ts.URL)
t.Run("do a request with Unmarshaler == nil", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover())
}()
c, _ := New(ts.URL)
c.Unmarshaler = nil
ctx := context.Background()
act := &message{}
req, err := c.NewRequest(http.MethodGet, "node", act)
assert.Nil(t, err)
assert.NotNil(t, req)
resp, _ := c.Do(ctx, req, act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Fail(t, "Do did not panic")
})
t.Run("do a request with ResponseCallback == nil", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover())
}()
c, _ := New(ts.URL)
c.ResponseCallback = nil
ctx := context.Background()
act := &message{}
req, err := c.NewRequest(http.MethodGet, "node", act)
assert.Nil(t, err)
assert.NotNil(t, req)
resp, _ := c.Do(ctx, req, act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Fail(t, "Do did not panic")
})
t.Run("do a request with a writer", func(t *testing.T) {
c, _ := New(ts.URL)
ctx := context.Background()
act := &message{}
req, err := c.NewRequest(http.MethodGet, "node", act)
assert.Nil(t, err)
assert.NotNil(t, req)
var buf bytes.Buffer
resp, err := c.Do(ctx, req, bufio.NewWriter(&buf))
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Nil(t, err)
assert.True(t, buf.Len() > 0)
})
t.Run("do a request with content type application/json", func(t *testing.T) {
c, _ := New(ts.URL)
ctx := context.Background()
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
act := &message{}
resp, err := c.Do(ctx, req, act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Nil(t, err)
assert.Equal(t, &testMessage, act)
})
t.Run("do a request with content type application/yaml", func(t *testing.T) {
c, _ := New(ts.URL)
c.ContentType = ContentTypeYAML
ctx := context.Background()
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
act := &message{}
resp, err := c.Do(ctx, req, act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Nil(t, err)
assert.Equal(t, &testMessage, act)
})
t.Run("do a request with content type text/plain", func(t *testing.T) {
c, _ := New(ts.URL)
c.ContentType = ContentTypeText
ctx := context.Background()
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
act := ""
resp, err := c.Do(ctx, req, &act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Nil(t, err)
assert.Equal(t, testMessage.String(), act)
})
t.Run("do a request with content type text/plain and provide non-string-type", func(t *testing.T) {
c, _ := New(ts.URL)
c.ContentType = ContentTypeText
ctx := context.Background()
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
act := 42
resp, err := c.Do(ctx, req, &act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.NotNil(t, err)
})
t.Run("do a request with error in response using default ResponseCallback", func(t *testing.T) {
c, _ := New(ts.URL)
ctx := context.Background()
act := &message{}
req, err := c.NewRequest(http.MethodGet, "invalid", act)
assert.Nil(t, err)
assert.NotNil(t, req)
var buf bytes.Buffer
resp, err := c.Do(ctx, req, bufio.NewWriter(&buf))
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.NotNil(t, err)
assert.Equal(t, "404 Not Found", err.Error())
})
t.Run("do a request using ResponseCallback to dump the response", func(t *testing.T) {
c, _ := New(ts.URL)
var dump []byte
c.ResponseCallback = func(r *http.Response) (*http.Response, error) {
dump, _ = httputil.DumpResponse(r, true)
return r, nil
}
ctx := context.Background()
act := &message{}
req, err := c.NewRequest(http.MethodGet, "node", act)
assert.Nil(t, err)
assert.NotNil(t, req)
var buf bytes.Buffer
resp, err := c.Do(ctx, req, bufio.NewWriter(&buf))
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.Nil(t, err)
assert.True(t, len(dump) > 0)
})
t.Run("do a request with content type unknown/unknown in response to test unmarshal behavior", func(t *testing.T) {
c, _ := New(ts.URL)
c.ContentType = ContentTypeText
c.ResponseCallback = func(r *http.Response) (*http.Response, error) {
r.Header.Set("Content-Type", "unknown/unknown")
return r, nil
}
ctx := context.Background()
req, err := c.NewRequest(http.MethodGet, "node", testMessage)
assert.Nil(t, err)
assert.NotNil(t, req)
act := 42
resp, err := c.Do(ctx, req, &act)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
assert.NotNil(t, err)
})
}