forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
273 lines (220 loc) · 7.59 KB
/
client_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
package rest
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestClient_RateLimit(t *testing.T) {
r := RateLimit{
Limit: 10,
Remaining: 10,
Period: 10,
}
if r.PercentageLeft() != 100 {
t.Error("PercentLeft != 100")
}
if r.WaitTime() != time.Second {
t.Error("WaitTime is wrong duration ", r.WaitTime())
}
if r.WaitTimeRemaining() != (time.Duration(1) * time.Second) {
t.Error("WaitTimeRemaining is wrong duration ", r.WaitTimeRemaining())
}
r.Remaining = 5
if r.PercentageLeft() != 50 {
t.Error("PercentLeft != 50")
}
if r.WaitTime() != time.Second {
t.Error("WaitTime is wrong duration ", r.WaitTime())
}
if r.WaitTimeRemaining() != (time.Duration(2) * time.Second) {
t.Error("WaitTimeRemaining is wrong duration ", r.WaitTimeRemaining())
}
r.Remaining = 0
if r.PercentageLeft() != 0 {
t.Error("PercentLeft != 0")
}
if r.WaitTime() != time.Second {
t.Error("WaitTime is wrong duration ", r.WaitTime())
}
if r.WaitTimeRemaining() != (time.Duration(10) * time.Second) {
t.Error("WaitTimeRemaining is wrong duration ", r.WaitTimeRemaining())
}
}
func TestClient_Do(t *testing.T) {
// It should return the response without error
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 200,
}
httpClient.On("Do", req).Return(&mockResp, nil)
resp, err := client.Do(req, "")
httpClient.AssertExpectations(t)
assert.Equal(t, &mockResp, resp)
assert.Nil(t, err)
}
func TestClient_DoWithHTTPClientError(t *testing.T) {
// It should return nil response and the error
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
mockError := errors.New("Some Error")
httpClient.On("Do", req).Return(nil, mockError)
resp, err := client.Do(req, "")
httpClient.AssertExpectations(t)
assert.Nil(t, resp)
assert.Equal(t, mockError, err)
}
func TestClient_DoWithHTTPClientErrorRateLimit(t *testing.T) {
// It should return nil response and the error, also should run the rate limit func
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
var rateLimit *RateLimit = nil
client.RateLimitFunc = func(r RateLimit) {
rateLimit = &r
}
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
headerResponse := make(http.Header)
headerResponse.Add(headerRateLimit, "10")
headerResponse.Add(headerRateRemaining, "10")
headerResponse.Add(headerRatePeriod, "10")
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
Header: headerResponse,
StatusCode: 400,
}
httpClient.On("Do", req).Return(&mockResp, nil)
client.Do(req, "")
httpClient.AssertExpectations(t)
assert.NotNil(t, rateLimit)
}
func TestClient_DoWithNon2XXResponse(t *testing.T) {
// It should return a pointer to the response, and a pointer to Error (with
// the response)
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 404,
}
httpClient.On("Do", req).Return(&mockResp, nil)
resp, err := client.Do(req, "")
httpClient.AssertExpectations(t)
assert.Equal(t, &mockResp, resp)
assert.Equal(t, &Error{Resp: &mockResp}, err)
}
func TestClient_DoWithNonJSONResponse(t *testing.T) {
// It should return a nil response, and the error from JSON Decoder
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("INVALID")),
StatusCode: 200,
}
httpClient.On("Do", req).Return(&mockResp, nil)
resp, err := client.Do(req, "")
httpClient.AssertExpectations(t)
assert.Nil(t, resp)
assert.IsType(t, &json.SyntaxError{}, err)
}
func TestClient_DoWithPagination(t *testing.T) {
// It should call nextFunc
// It should return the last response without error
// It should not set HTTPS on the Link when endpoint is HTTP
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint("http://"))
req, _ := http.NewRequest("GET", "http://example.com", new(bytes.Buffer))
firstResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
Header: http.Header{"Link": []string{`<http://example.com/?after=1&limit=2>; rel="next"`}},
StatusCode: 200,
}
nextResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 200,
}
var v interface{}
httpClient.On("Do", req).Return(&firstResp, nil)
httpClient.On("nextFunc", &v, "http://example.com/?after=1&limit=2").Return(&nextResp, nil)
resp, err := client.DoWithPagination(req, v, httpClient.nextFunc)
httpClient.AssertExpectations(t)
assert.Equal(t, &nextResp, resp)
assert.Nil(t, err)
}
func TestClient_DoWithPaginationForceHTTPS(t *testing.T) {
// When the endpoint is HTTPS, it should Force HTTPS when following Link
// headers
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint("https://"))
req, _ := http.NewRequest("GET", "https://example.com", new(bytes.Buffer))
firstResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
Header: http.Header{"Link": []string{`<http://example.com/?after=1&limit=2>; rel="next"`}},
StatusCode: 200,
}
nextResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 200,
}
var v interface{}
httpClient.On("Do", req).Return(&firstResp, nil)
httpClient.On("nextFunc", &v, "https://example.com/?after=1&limit=2").Return(&nextResp, nil)
resp, err := client.DoWithPagination(req, v, httpClient.nextFunc)
httpClient.AssertExpectations(t)
assert.Equal(t, &nextResp, resp)
assert.Nil(t, err)
}
func TestClient_getURI(t *testing.T) {
// It should delegate to client.Do
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 200,
}
httpClient.On("Do", mock.Anything).Return(&mockResp, nil)
var v interface{}
resp, err := client.getURI(v, "http://example.com")
assert.Equal(t, &mockResp, resp)
assert.Nil(t, err)
}
func TestClient_getURIWithNon2XXResponse(t *testing.T) {
// It should return a pointer to the response, and a pointer to Error (with
// the response
httpClient := mockHTTPClient{}
client := NewClient(&httpClient, SetEndpoint(""))
mockResp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 418,
}
httpClient.On("Do", mock.Anything).Return(&mockResp, nil)
var v interface{}
resp, err := client.getURI(v, "http://example.com")
assert.Equal(t, &mockResp, resp)
assert.Equal(t, &Error{Resp: &mockResp}, err)
}
type mockHTTPClient struct {
mock.Mock
}
func (c *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
args := c.Called(req)
if res := args.Get(0); res == nil {
return nil, args.Error(1)
}
return args.Get(0).(*http.Response), args.Error(1)
}
// Hanging this off of mockHTTPClient for convent access to mocking stuff
func (c *mockHTTPClient) nextFunc(v *interface{}, uri string) (*http.Response, error) {
args := c.Called(v, uri)
return args.Get(0).(*http.Response), args.Error(1)
}