-
Notifications
You must be signed in to change notification settings - Fork 242
/
event_v2_test.go
383 lines (355 loc) · 8.67 KB
/
event_v2_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
package pagerduty
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
)
func TestEventV2_ManageEvent(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/enqueue", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
_, _ = w.Write([]byte(`{"status": "ok", "dedup_key": "yes", "message": "ok"}`))
})
client := defaultTestClient(server.URL, "foo")
evt := &V2Event{
RoutingKey: "abc123",
}
res, err := client.ManageEvent(evt)
if err != nil {
t.Fatal(err)
}
want := &V2EventResponse{
Status: "ok",
DedupKey: "yes",
Message: "ok",
}
testEqual(t, want, res)
}
func TestEventsAPIV2Error_BadRequest(t *testing.T) {
tests := []struct {
name string
e EventsAPIV2Error
want bool
}{
{
name: "bad_request",
e: EventsAPIV2Error{
StatusCode: http.StatusBadRequest,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "invalid",
Message: "Event object is invalid",
Errors: []string{"Length of 'routing_key' is incorrect (should be 32 characters)", "'event_action' is missing or blank"},
},
},
},
want: true,
},
{
name: "rate_limited",
e: EventsAPIV2Error{
StatusCode: http.StatusTooManyRequests,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "throttle exceeded",
Message: "Requests for this service are arriving too quickly. Please retry later.",
Errors: []string{"Enhance Your Calm."},
},
},
},
want: false,
},
{
name: "InternalServerError",
e: EventsAPIV2Error{
StatusCode: http.StatusInternalServerError,
},
want: false,
},
{
name: "ServiceUnavailable",
e: EventsAPIV2Error{
StatusCode: http.StatusServiceUnavailable,
},
want: false,
},
{
name: "RequestTimeout",
e: EventsAPIV2Error{
StatusCode: http.StatusRequestTimeout,
},
want: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.BadRequest(); got != tt.want {
t.Fatalf("tt.e.BadRequest() = %t, want %t", got, tt.want)
}
})
}
}
func TestEventsAPIV2Error_APITimeout(t *testing.T) {
tests := []struct {
name string
e EventsAPIV2Error
want bool
}{
{
name: "bad_request",
e: EventsAPIV2Error{
StatusCode: http.StatusBadRequest,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "invalid",
Message: "Event object is invalid",
Errors: []string{"Length of 'routing_key' is incorrect (should be 32 characters)", "'event_action' is missing or blank"},
},
},
},
want: false,
},
{
name: "rate_limited",
e: EventsAPIV2Error{
StatusCode: http.StatusTooManyRequests,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "throttle exceeded",
Message: "Requests for this service are arriving too quickly. Please retry later.",
Errors: []string{"Enhance Your Calm."},
},
},
},
want: false,
},
{
name: "InternalServerError",
e: EventsAPIV2Error{
StatusCode: http.StatusInternalServerError,
},
want: false,
},
{
name: "ServiceUnavailable",
e: EventsAPIV2Error{
StatusCode: http.StatusServiceUnavailable,
},
want: false,
},
{
name: "RequestTimeout",
e: EventsAPIV2Error{
StatusCode: http.StatusRequestTimeout,
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.APITimeout(); got != tt.want {
t.Fatalf("tt.e.BadRequest() = %t, want %t", got, tt.want)
}
})
}
}
func TestEventsAPIV2Error_RateLimited(t *testing.T) {
tests := []struct {
name string
e EventsAPIV2Error
want bool
}{
{
name: "rate_limited",
e: EventsAPIV2Error{
StatusCode: http.StatusTooManyRequests,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "throttle exceeded",
Message: "Requests for this service are arriving too quickly. Please retry later.",
Errors: []string{"Enhance Your Calm"},
},
},
},
want: true,
},
{
name: "not_found",
e: EventsAPIV2Error{
StatusCode: http.StatusNotFound,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "Not Found",
Message: "Not Found",
Errors: []string{"Not Found"},
},
},
},
want: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.RateLimited(); got != tt.want {
t.Fatalf("tt.e.RateLimited() = %t, want %t", got, tt.want)
}
})
}
}
func TestEventsAPIV2Error_Temporary(t *testing.T) {
tests := []struct {
name string
e EventsAPIV2Error
want bool
}{
{
name: "rate_limited",
e: EventsAPIV2Error{
StatusCode: http.StatusTooManyRequests,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "throttle exceeded",
Message: "Requests for this service are arriving too quickly. Please retry later.",
Errors: []string{"Enhance Your Calm"},
},
},
},
want: true,
},
{
name: "InternalServerError",
e: EventsAPIV2Error{
StatusCode: http.StatusInternalServerError,
},
want: true,
},
{
name: "ServiceUnavailable",
e: EventsAPIV2Error{
StatusCode: http.StatusServiceUnavailable,
},
want: true,
},
{
name: "RequestTimeout",
e: EventsAPIV2Error{
StatusCode: http.StatusRequestTimeout,
},
want: true,
},
{
name: "not_found",
e: EventsAPIV2Error{
StatusCode: http.StatusNotFound,
APIError: NullEventsAPIV2ErrorObject{
Valid: true,
ErrorObject: EventsAPIV2ErrorObject{
Status: "Not Found",
Message: "Not Found",
Errors: []string{"Not Found"},
},
},
},
want: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.Temporary(); got != tt.want {
t.Fatalf("tt.e.Temporary() = %t, want %t", got, tt.want)
}
})
}
}
func TestEventsAPIV2Error_Error(t *testing.T) {
t.Run("json_tests", func(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "no_error",
input: `{"status": "Calming", "message": "Enhance Your Calm"}`,
want: "HTTP response failed with status code 429, status: Calming, message: Enhance Your Calm",
},
{
name: "one_error",
input: `{"message": "Enhance Your Calm", "status": "Calming", "errors": ["No Seriously, Enhance Your Calm"]}`,
want: "HTTP response failed with status code 429, status: Calming, message: Enhance Your Calm: No Seriously, Enhance Your Calm",
},
{
name: "two_error",
input: `{"message": "Enhance Your Calm", "status": "Calming", "errors":["No Seriously, Enhance Your Calm", "Slow Your Roll"]}`,
want: "HTTP response failed with status code 429, status: Calming, message: Enhance Your Calm: No Seriously, Enhance Your Calm (and 1 more error...)",
},
{
name: "three_error",
input: `{"message": "Enhance Your Calm", "status": "Calming", "errors":["No Seriously, Enhance Your Calm", "Slow Your Roll", "No, really..."]}`,
want: "HTTP response failed with status code 429, status: Calming, message: Enhance Your Calm: No Seriously, Enhance Your Calm (and 2 more errors...)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var a EventsAPIV2Error
if err := json.Unmarshal([]byte(tt.input), &a); err != nil {
t.Fatalf("failed to unmarshal JSON: %s", err)
}
a.StatusCode = 429
if got := a.Error(); got != tt.want {
t.Errorf("a.Error() = %q, want %q", got, tt.want)
}
})
}
})
tests := []struct {
name string
a EventsAPIV2Error
want string
}{
{
name: "message",
a: EventsAPIV2Error{
message: "test message",
},
want: "test message",
},
{
name: "APIError_nil",
a: EventsAPIV2Error{
StatusCode: http.StatusServiceUnavailable,
},
want: "HTTP response failed with status code 503 and no JSON error object was present",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := tt.a.Error(); got != tt.want {
fmt.Println(got)
fmt.Println(tt.want)
t.Fatalf("tt.a.Error() = %q, want %q", got, tt.want)
}
})
}
}
func TestEventsAPIV2Error_UnmarshalJSON(t *testing.T) {
v := &EventsAPIV2Error{}
if err := v.UnmarshalJSON([]byte(`{`)); !strings.Contains(err.Error(), "unexpected end of JSON input") {
t.Error("exepcted error not seen")
}
}