-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_test.go
334 lines (273 loc) · 7.71 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
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
package yext
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/jonboulle/clockwork"
)
func TestResponseDeserialzation(t *testing.T) {
tests := []struct {
data string
statuscode int
want *Response
obj interface{}
}{
{
data: `{"meta": {"errors": [{
"message": "We had a problem with our software. Please contact support!",
"code": 9,
"type": "FATAL_ERROR"
}], "uuid": ""}}`,
want: &Response{
Meta: Meta{
Errors: Errors{
&Error{
Code: 9,
Type: ErrorTypeFatal,
Message: "We had a problem with our software. Please contact support!",
}}}},
},
{
data: `{"meta": {"errors": [{
"message": "We had a problem with our software. Please contact support!",
"code": 9,
"type": "FATAL_ERROR"
}], "uuid": "abcd-1234"}}`,
want: &Response{
Meta: Meta{
UUID: "abcd-1234",
Errors: Errors{
&Error{
Code: 9,
Type: ErrorTypeFatal,
Message: "We had a problem with our software. Please contact support!",
RequestUUID: "abcd-1234",
}}}},
},
{
data: `{"meta": {"errors": [], "uuid": ""}}`,
want: &Response{},
},
{
data: `{"meta": {"errors": [], "uuid": ""}, "response": {"foo": true}}`,
want: &Response{Response: map[string]interface{}{"foo": true}},
obj: map[string]interface{}{},
},
{
data: `{"meta": {"errors": [], "uuid": "0556abaf-e5fb-475f-8e2a-79688bf4bc18"}}`,
want: &Response{Meta: Meta{UUID: "0556abaf-e5fb-475f-8e2a-79688bf4bc18"}},
},
}
for _, test := range tests {
setup()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(test.data))
})
resp, _ := client.DoRequest("", "", test.obj)
// Avoid DeepEqual issues comparing yext.Errors(nil) and yext.Errors{} as
// the API always returns `errors: []` vs `errors: null` to indicate 'no error'
if len(resp.Meta.Errors) == 0 {
resp.Meta.Errors = nil
}
if !reflect.DeepEqual(resp, test.want) {
t.Errorf("ResponseData: %s\n\tWanted: %#v\n\tGot: %#v", test.data, test.want, resp)
}
teardown()
}
}
func TestRetries(t *testing.T) {
setup().WithRetries(3)
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(http.StatusInternalServerError)
})
client.DoRequest("GET", "", nil)
if requests != 4 {
t.Error("Expected 4 net attempts when error encountered, only got", requests)
}
}
func TestLastRetryError(t *testing.T) {
setup().WithRetries(3)
defer teardown()
request := 0
errf := func(n int) string {
return fmt.Sprintf("error from request #%d", n)
}
wraperr := func(m string) string {
return fmt.Sprintf(`{"meta": {"errors": [{"message": "%s"}], "uuid": ""}}`, m)
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
request++
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(wraperr(errf(request))))
})
_, err := client.DoRequest("GET", "", nil)
expectedErr := errf(*client.Config.RetryCount + 1)
if !strings.Contains(err.Error(), expectedErr) {
t.Errorf("Expected to get error `%s`, instead got `%s`", expectedErr, err)
}
}
func TestBailout(t *testing.T) {
setup().WithRetries(3)
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
if requests == 4 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
_, err := client.DoRequest("GET", "", nil)
if err != nil {
t.Error("Expected error to be nil when final attempt succeeded:", err)
}
}
func TestRetryWithBody(t *testing.T) {
setup().WithRetries(3)
defer teardown()
body := map[string]interface{}{"foo": "bar"}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
var payload map[string]interface{}
json.Unmarshal(b, &payload)
if !reflect.DeepEqual(body, payload) {
t.Error("Expected to get identical body in retry scenario, got", string(b), "instead")
}
// Force retries
w.WriteHeader(http.StatusInternalServerError)
})
client.DoRequestJSON("POST", "", body, nil)
}
func TestRetryWith400Error(t *testing.T) {
setup().WithRetries(3)
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(http.StatusBadRequest)
})
client.DoRequest("GET", "", nil)
if requests != 1 {
t.Errorf("Expected 1 net attempts when %d encountered, got %d", http.StatusBadRequest, requests)
}
}
func TestRetryWith404Error(t *testing.T) {
setup().WithRetries(3)
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(http.StatusNotFound)
})
client.DoRequest("GET", "", nil)
if requests != 1 {
t.Errorf("Expected 1 net attempts when %d encountered, got %d", http.StatusNotFound, requests)
}
}
func TestAddListOptions(t *testing.T) {
tests := []struct {
requrl string
opts *ListOptions
want string
}{
{
requrl: "locations",
want: "locations",
},
{
requrl: "locations",
opts: &ListOptions{Limit: 99},
want: "locations?limit=99",
},
{
requrl: "locations",
opts: &ListOptions{Offset: 99},
want: "locations?offset=99",
},
}
for _, test := range tests {
if got, err := addListOptions(test.requrl, test.opts); err != nil {
t.Errorf("addListOptions(%s, %#v) error %s", test.requrl, test.opts, err.Error())
} else if got != test.want {
t.Errorf("addListOptions(%s, %#v) = %s, wanted %s", test.requrl, test.opts, got, test.want)
}
}
}
func TestNoRetryOnDeserError(t *testing.T) {
setup().WithRetries(3)
defer teardown()
requests := 0
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requests++
w.Write([]byte(`{"meta": {"errors": [], "uuid": ""}, "response": false}`))
})
var v map[string]interface{}
_, err := client.DoRequest("GET", "", &v)
if err == nil {
t.Error("Expected deserialization error")
}
if requests != 1 {
t.Errorf("Expected 1 net attempts when %s encountered, got %d", err.Error(), requests)
}
}
func TestRateLimitParsing(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Rate-Limit-Limit", "5000")
w.Header().Set("Rate-Limit-Remaining", "4000")
w.Header().Set("Rate-Limit-Reset", "1490799600")
w.WriteHeader(http.StatusOK)
})
resp, err := client.DoRequest("GET", "", nil)
if err != nil {
t.Error(err)
}
if v := resp.RateLimitLimit; v != 5000 {
t.Errorf("Expected RateLimitLimit of 5000, got %d", v)
}
if v := resp.RateLimitRemaining; v != 4000 {
t.Errorf("Expected RateLimitRemaining of 4000, got %d", v)
}
if v := resp.RateLimitReset; v != 1490799600 {
t.Errorf("Expected RateLimitReset of 1490799600, got %d", v)
}
}
func TestRateLimitWaiting(t *testing.T) {
conf := setup().WithRateLimitRetry().WithMockClock()
defer teardown()
fClock := conf.Clock.(clockwork.FakeClock)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
resetTime := strconv.FormatInt(fClock.Now().Unix()+1, 10)
w.Header().Set("Rate-Limit-Limit", "5000")
w.Header().Set("Rate-Limit-Remaining", "4000")
w.Header().Set("Rate-Limit-Reset", resetTime)
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{"meta": {"errors": [{"message": "", "code": 1}], "uuid": ""}}`))
})
timeBefore := fClock.Now()
go func() {
client.DoRequest("GET", "", nil)
}()
go func() {
time.Sleep(500 * time.Millisecond)
fClock.Advance(time.Hour)
fClock.Sleep(time.Hour)
}()
fClock.BlockUntil(1)
if fClock.Since(timeBefore) < time.Minute {
return
} else {
t.Errorf("No sleep initiated after waiting .5 seconds")
}
}