-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_client_wrapper_test.go
67 lines (59 loc) · 1.97 KB
/
http_client_wrapper_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
package shared
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func Test_httpClientWrapper_ExecuteRequest_failsTooManyRetries(t *testing.T) {
const baseURL = "http://test.com"
retryCount := uint(5)
hcw, _ := NewHTTPClientWrapper(baseURL, "", 1, nil, true, WithRetry(retryCount))
httpmock.Activate()
defer httpmock.DeactivateAndReset()
path := "/api/vehicle/v2/makes"
httpmock.RegisterResponder(http.MethodGet, baseURL+path, httpmock.NewStringResponder(503, "error: too many requests"))
response, err := hcw.ExecuteRequest(path, "GET", nil)
countInfo := httpmock.GetCallCountInfo()
c := countInfo["GET "+baseURL+path]
assert.Equal(t, int(retryCount), c, "expected five retries")
assert.Error(t, err, "expected error")
assert.ErrorIs(t, err, err.(HTTPResponseError), "expected HTTPResponseError")
assert.Equal(t, 503, err.(HTTPResponseError).StatusCode, "expected 409")
assert.Equal(t, 503, response.StatusCode)
}
func Test_httpClientWrapper_ExecuteRequest_doesNotRetryCertainStatusCodes(t *testing.T) {
const baseURL = "http://test.com"
hcw, _ := NewHTTPClientWrapper(baseURL, "", 1, nil, true)
tests := []struct {
name string
statusCode int
}{
{
name: "don't retry 400",
statusCode: 400,
},
{
name: "don't retry 401",
statusCode: 401,
},
{
name: "don't retry 404",
statusCode: 404,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
path := "/api/vehicle/v2/makes"
httpmock.RegisterResponder(http.MethodGet, baseURL+path, httpmock.NewStringResponder(test.statusCode, "error: whatever"))
response, err := hcw.ExecuteRequest(path, "GET", nil)
countInfo := httpmock.GetCallCountInfo()
c := countInfo["GET "+baseURL+path]
assert.Equal(t, 1, c, "expected single call")
assert.Equal(t, test.statusCode, response.StatusCode)
assert.Error(t, err, "expected error")
})
}
}