1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
"net/http" |
7
|
|
|
"testing" |
8
|
|
|
|
9
|
|
|
"github.com/VictorAvelar/mollie-api-go/v4/testdata" |
10
|
|
|
"github.com/stretchr/testify/assert" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
func TestSubscriptionsService_Get(t *testing.T) { |
14
|
|
|
setEnv() |
15
|
|
|
defer unsetEnv() |
16
|
|
|
|
17
|
|
|
type args struct { |
18
|
|
|
ctx context.Context |
19
|
|
|
customer string |
20
|
|
|
subscription string |
21
|
|
|
} |
22
|
|
|
cases := []struct { |
23
|
|
|
name string |
24
|
|
|
args args |
25
|
|
|
wantErr bool |
26
|
|
|
err error |
27
|
|
|
pre func() |
28
|
|
|
handler http.HandlerFunc |
29
|
|
|
}{ |
30
|
|
|
{ |
31
|
|
|
"get subscription works as expected.", |
32
|
|
|
args{ |
33
|
|
|
context.Background(), |
34
|
|
|
"cst_stTC2WHAuS", |
35
|
|
|
"sub_rVKGtNd6s3", |
36
|
|
|
}, |
37
|
|
|
false, |
38
|
|
|
nil, |
39
|
|
|
noPre, |
40
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
41
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
42
|
|
|
testMethod(t, r, "GET") |
43
|
|
|
|
44
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
45
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
46
|
|
|
} |
47
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
48
|
|
|
}, |
49
|
|
|
}, |
50
|
|
|
{ |
51
|
|
|
"get subscription, an error is returned from the server", |
52
|
|
|
args{ |
53
|
|
|
context.Background(), |
54
|
|
|
"cst_stTC2WHAuS", |
55
|
|
|
"sub_rVKGtNd6s3", |
56
|
|
|
}, |
57
|
|
|
true, |
58
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
59
|
|
|
noPre, |
60
|
|
|
errorHandler, |
61
|
|
|
}, |
62
|
|
|
{ |
63
|
|
|
"get subscription, an error occurs when parsing json", |
64
|
|
|
args{ |
65
|
|
|
context.Background(), |
66
|
|
|
"cst_stTC2WHAuS", |
67
|
|
|
"sub_rVKGtNd6s3", |
68
|
|
|
}, |
69
|
|
|
true, |
70
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
71
|
|
|
noPre, |
72
|
|
|
encodingHandler, |
73
|
|
|
}, |
74
|
|
|
{ |
75
|
|
|
"get subscription, invalid url when building request", |
76
|
|
|
args{ |
77
|
|
|
context.Background(), |
78
|
|
|
"cst_stTC2WHAuS", |
79
|
|
|
"sub_rVKGtNd6s3", |
80
|
|
|
}, |
81
|
|
|
true, |
82
|
|
|
errBadBaseURL, |
83
|
|
|
crashSrv, |
84
|
|
|
errorHandler, |
85
|
|
|
}, |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
for _, c := range cases { |
89
|
|
|
setup() |
90
|
|
|
defer teardown() |
91
|
|
|
|
92
|
|
|
t.Run(c.name, func(t *testing.T) { |
93
|
|
|
c.pre() |
94
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions/%s", c.args.customer, c.args.subscription), c.handler) |
95
|
|
|
|
96
|
|
|
res, m, err := tClient.Subscriptions.Get(c.args.ctx, c.args.customer, c.args.subscription) |
97
|
|
|
if c.wantErr { |
98
|
|
|
assert.NotNil(t, err) |
99
|
|
|
assert.EqualError(t, err, c.err.Error()) |
100
|
|
|
} else { |
101
|
|
|
assert.Nil(t, err) |
102
|
|
|
assert.IsType(t, &Subscription{}, m) |
103
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
104
|
|
|
} |
105
|
|
|
}) |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
func TestSubscriptionsService_Create(t *testing.T) { |
110
|
|
|
setEnv() |
111
|
|
|
defer unsetEnv() |
112
|
|
|
|
113
|
|
|
type args struct { |
114
|
|
|
ctx context.Context |
115
|
|
|
customer string |
116
|
|
|
subscription CreateSubscription |
117
|
|
|
} |
118
|
|
|
cases := []struct { |
119
|
|
|
name string |
120
|
|
|
args args |
121
|
|
|
wantErr bool |
122
|
|
|
err error |
123
|
|
|
pre func() |
124
|
|
|
handler http.HandlerFunc |
125
|
|
|
}{ |
126
|
|
|
{ |
127
|
|
|
"create subscription works as expected.", |
128
|
|
|
args{ |
129
|
|
|
context.Background(), |
130
|
|
|
"cst_stTC2WHAuS", |
131
|
|
|
CreateSubscription{ |
132
|
|
|
Amount: &Amount{ |
133
|
|
|
Currency: "EUR", |
134
|
|
|
Value: "100.00", |
135
|
|
|
}, |
136
|
|
|
Times: 12, |
137
|
|
|
}, |
138
|
|
|
}, |
139
|
|
|
false, |
140
|
|
|
nil, |
141
|
|
|
noPre, |
142
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
143
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
144
|
|
|
testMethod(t, r, "POST") |
145
|
|
|
|
146
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
147
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
148
|
|
|
} |
149
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
150
|
|
|
}, |
151
|
|
|
}, |
152
|
|
|
{ |
153
|
|
|
"create subscription with access tokens works as expected.", |
154
|
|
|
args{ |
155
|
|
|
context.Background(), |
156
|
|
|
"cst_stTC2WHAuS", |
157
|
|
|
CreateSubscription{ |
158
|
|
|
Amount: &Amount{ |
159
|
|
|
Currency: "EUR", |
160
|
|
|
Value: "100.00", |
161
|
|
|
}, |
162
|
|
|
Times: 12, |
163
|
|
|
}, |
164
|
|
|
}, |
165
|
|
|
false, |
166
|
|
|
nil, |
167
|
|
|
setAccessToken, |
168
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
169
|
|
|
testHeader(t, r, AuthHeader, "Bearer access_token_test") |
170
|
|
|
testMethod(t, r, "POST") |
171
|
|
|
testQuery(t, r, "testmode=true") |
172
|
|
|
|
173
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
174
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
175
|
|
|
} |
176
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
177
|
|
|
}, |
178
|
|
|
}, |
179
|
|
|
{ |
180
|
|
|
"create subscription, an error is returned from the server", |
181
|
|
|
args{ |
182
|
|
|
context.Background(), |
183
|
|
|
"cst_stTC2WHAuS", |
184
|
|
|
CreateSubscription{ |
185
|
|
|
Amount: &Amount{ |
186
|
|
|
Currency: "EUR", |
187
|
|
|
Value: "100.00", |
188
|
|
|
}, |
189
|
|
|
Times: 12, |
190
|
|
|
}, |
191
|
|
|
}, |
192
|
|
|
true, |
193
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
194
|
|
|
noPre, |
195
|
|
|
errorHandler, |
196
|
|
|
}, |
197
|
|
|
{ |
198
|
|
|
"create subscription, an error occurs when parsing json", |
199
|
|
|
args{ |
200
|
|
|
context.Background(), |
201
|
|
|
"cst_stTC2WHAuS", |
202
|
|
|
CreateSubscription{ |
203
|
|
|
Amount: &Amount{ |
204
|
|
|
Currency: "EUR", |
205
|
|
|
Value: "100.00", |
206
|
|
|
}, |
207
|
|
|
Times: 12, |
208
|
|
|
}, |
209
|
|
|
}, |
210
|
|
|
true, |
211
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
212
|
|
|
noPre, |
213
|
|
|
encodingHandler, |
214
|
|
|
}, |
215
|
|
|
{ |
216
|
|
|
"create subscription, invalid url when building request", |
217
|
|
|
args{ |
218
|
|
|
context.Background(), |
219
|
|
|
"cst_stTC2WHAuS", |
220
|
|
|
CreateSubscription{ |
221
|
|
|
Amount: &Amount{ |
222
|
|
|
Currency: "EUR", |
223
|
|
|
Value: "100.00", |
224
|
|
|
}, |
225
|
|
|
Times: 12, |
226
|
|
|
}, |
227
|
|
|
}, |
228
|
|
|
true, |
229
|
|
|
errBadBaseURL, |
230
|
|
|
crashSrv, |
231
|
|
|
errorHandler, |
232
|
|
|
}, |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
for _, c := range cases { |
236
|
|
|
setup() |
237
|
|
|
defer teardown() |
238
|
|
|
|
239
|
|
|
t.Run(c.name, func(t *testing.T) { |
240
|
|
|
c.pre() |
241
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions", c.args.customer), c.handler) |
242
|
|
|
|
243
|
|
|
res, m, err := tClient.Subscriptions.Create(c.args.ctx, c.args.customer, c.args.subscription) |
244
|
|
|
if c.wantErr { |
245
|
|
|
assert.NotNil(t, err) |
246
|
|
|
assert.EqualError(t, err, c.err.Error()) |
247
|
|
|
} else { |
248
|
|
|
assert.Nil(t, err) |
249
|
|
|
assert.IsType(t, &Subscription{}, m) |
250
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
251
|
|
|
} |
252
|
|
|
}) |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
func TestSubscriptionsService_Update(t *testing.T) { |
257
|
|
|
setEnv() |
258
|
|
|
defer unsetEnv() |
259
|
|
|
|
260
|
|
|
type args struct { |
261
|
|
|
ctx context.Context |
262
|
|
|
customer string |
263
|
|
|
sid string |
264
|
|
|
subscription UpdateSubscription |
265
|
|
|
} |
266
|
|
|
cases := []struct { |
267
|
|
|
name string |
268
|
|
|
args args |
269
|
|
|
wantErr bool |
270
|
|
|
err error |
271
|
|
|
pre func() |
272
|
|
|
handler http.HandlerFunc |
273
|
|
|
}{ |
274
|
|
|
{ |
275
|
|
|
"update subscription works as expected.", |
276
|
|
|
args{ |
277
|
|
|
context.Background(), |
278
|
|
|
"cst_stTC2WHAuS", |
279
|
|
|
"sub_rVKGtNd6s3", |
280
|
|
|
UpdateSubscription{ |
281
|
|
|
Amount: &Amount{ |
282
|
|
|
Currency: "EUR", |
283
|
|
|
Value: "100.00", |
284
|
|
|
}, |
285
|
|
|
Times: 12, |
286
|
|
|
}, |
287
|
|
|
}, |
288
|
|
|
false, |
289
|
|
|
nil, |
290
|
|
|
noPre, |
291
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
292
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
293
|
|
|
testMethod(t, r, "PATCH") |
294
|
|
|
|
295
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
296
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
297
|
|
|
} |
298
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
299
|
|
|
}, |
300
|
|
|
}, |
301
|
|
|
{ |
302
|
|
|
"update subscription with access tokens works as expected.", |
303
|
|
|
args{ |
304
|
|
|
context.Background(), |
305
|
|
|
"cst_stTC2WHAuS", |
306
|
|
|
"sub_rVKGtNd6s3", |
307
|
|
|
UpdateSubscription{ |
308
|
|
|
Amount: &Amount{ |
309
|
|
|
Currency: "EUR", |
310
|
|
|
Value: "100.00", |
311
|
|
|
}, |
312
|
|
|
Times: 12, |
313
|
|
|
}, |
314
|
|
|
}, |
315
|
|
|
false, |
316
|
|
|
nil, |
317
|
|
|
setAccessToken, |
318
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
319
|
|
|
testHeader(t, r, AuthHeader, "Bearer access_token_test") |
320
|
|
|
testMethod(t, r, "PATCH") |
321
|
|
|
testQuery(t, r, "testmode=true") |
322
|
|
|
|
323
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
324
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
325
|
|
|
} |
326
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
327
|
|
|
}, |
328
|
|
|
}, |
329
|
|
|
{ |
330
|
|
|
"update subscription, an error is returned from the server", |
331
|
|
|
args{ |
332
|
|
|
context.Background(), |
333
|
|
|
"cst_stTC2WHAuS", |
334
|
|
|
"sub_rVKGtNd6s3", |
335
|
|
|
UpdateSubscription{ |
336
|
|
|
Amount: &Amount{ |
337
|
|
|
Currency: "EUR", |
338
|
|
|
Value: "100.00", |
339
|
|
|
}, |
340
|
|
|
Times: 12, |
341
|
|
|
}, |
342
|
|
|
}, |
343
|
|
|
true, |
344
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
345
|
|
|
noPre, |
346
|
|
|
errorHandler, |
347
|
|
|
}, |
348
|
|
|
{ |
349
|
|
|
"update subscription, an error occurs when parsing json", |
350
|
|
|
args{ |
351
|
|
|
context.Background(), |
352
|
|
|
"cst_stTC2WHAuS", |
353
|
|
|
"sub_rVKGtNd6s3", |
354
|
|
|
UpdateSubscription{ |
355
|
|
|
Amount: &Amount{ |
356
|
|
|
Currency: "EUR", |
357
|
|
|
Value: "100.00", |
358
|
|
|
}, |
359
|
|
|
Times: 12, |
360
|
|
|
}, |
361
|
|
|
}, |
362
|
|
|
true, |
363
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
364
|
|
|
noPre, |
365
|
|
|
encodingHandler, |
366
|
|
|
}, |
367
|
|
|
{ |
368
|
|
|
"update subscription, invalid url when building request", |
369
|
|
|
args{ |
370
|
|
|
context.Background(), |
371
|
|
|
"cst_stTC2WHAuS", |
372
|
|
|
"sub_rVKGtNd6s3", |
373
|
|
|
UpdateSubscription{ |
374
|
|
|
Amount: &Amount{ |
375
|
|
|
Currency: "EUR", |
376
|
|
|
Value: "100.00", |
377
|
|
|
}, |
378
|
|
|
Times: 12, |
379
|
|
|
}, |
380
|
|
|
}, |
381
|
|
|
true, |
382
|
|
|
errBadBaseURL, |
383
|
|
|
crashSrv, |
384
|
|
|
errorHandler, |
385
|
|
|
}, |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
for _, c := range cases { |
389
|
|
|
setup() |
390
|
|
|
defer teardown() |
391
|
|
|
|
392
|
|
|
t.Run(c.name, func(t *testing.T) { |
393
|
|
|
c.pre() |
394
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions/%s", c.args.customer, c.args.sid), c.handler) |
395
|
|
|
|
396
|
|
|
res, m, err := tClient.Subscriptions.Update(c.args.ctx, c.args.customer, c.args.sid, c.args.subscription) |
397
|
|
|
if c.wantErr { |
398
|
|
|
assert.NotNil(t, err) |
399
|
|
|
assert.EqualError(t, err, c.err.Error()) |
400
|
|
|
} else { |
401
|
|
|
assert.Nil(t, err) |
402
|
|
|
assert.IsType(t, &Subscription{}, m) |
403
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
404
|
|
|
} |
405
|
|
|
}) |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
func TestSubscriptionsService_Cancel(t *testing.T) { |
410
|
|
|
setEnv() |
411
|
|
|
defer unsetEnv() |
412
|
|
|
|
413
|
|
|
type args struct { |
414
|
|
|
ctx context.Context |
415
|
|
|
customer string |
416
|
|
|
sid string |
417
|
|
|
} |
418
|
|
|
cases := []struct { |
419
|
|
|
name string |
420
|
|
|
args args |
421
|
|
|
wantErr bool |
422
|
|
|
err error |
423
|
|
|
pre func() |
424
|
|
|
handler http.HandlerFunc |
425
|
|
|
}{ |
426
|
|
|
{ |
427
|
|
|
"delete subscription works as expected.", |
428
|
|
|
args{ |
429
|
|
|
context.Background(), |
430
|
|
|
"cst_stTC2WHAuS", |
431
|
|
|
"sub_rVKGtNd6s3", |
432
|
|
|
}, |
433
|
|
|
false, |
434
|
|
|
nil, |
435
|
|
|
noPre, |
436
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
437
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
438
|
|
|
testMethod(t, r, "DELETE") |
439
|
|
|
|
440
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
441
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
442
|
|
|
} |
443
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
444
|
|
|
}, |
445
|
|
|
}, |
446
|
|
|
{ |
447
|
|
|
"delete subscription with access tokens works as expected.", |
448
|
|
|
args{ |
449
|
|
|
context.Background(), |
450
|
|
|
"cst_stTC2WHAuS", |
451
|
|
|
"sub_rVKGtNd6s3", |
452
|
|
|
}, |
453
|
|
|
false, |
454
|
|
|
nil, |
455
|
|
|
setAccessToken, |
456
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
457
|
|
|
testHeader(t, r, AuthHeader, "Bearer access_token_test") |
458
|
|
|
testMethod(t, r, "DELETE") |
459
|
|
|
testQuery(t, r, "testmode=true") |
460
|
|
|
|
461
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
462
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
463
|
|
|
} |
464
|
|
|
_, _ = w.Write([]byte(testdata.GetSubscriptionResponse)) |
465
|
|
|
}, |
466
|
|
|
}, |
467
|
|
|
{ |
468
|
|
|
"delete subscription, an error is returned from the server", |
469
|
|
|
args{ |
470
|
|
|
context.Background(), |
471
|
|
|
"cst_stTC2WHAuS", |
472
|
|
|
"sub_rVKGtNd6s3", |
473
|
|
|
}, |
474
|
|
|
true, |
475
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
476
|
|
|
noPre, |
477
|
|
|
errorHandler, |
478
|
|
|
}, |
479
|
|
|
{ |
480
|
|
|
"delete subscription, an error occurs when parsing json", |
481
|
|
|
args{ |
482
|
|
|
context.Background(), |
483
|
|
|
"cst_stTC2WHAuS", |
484
|
|
|
"sub_rVKGtNd6s3", |
485
|
|
|
}, |
486
|
|
|
true, |
487
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
488
|
|
|
noPre, |
489
|
|
|
encodingHandler, |
490
|
|
|
}, |
491
|
|
|
{ |
492
|
|
|
"delete subscription, invalid url when building request", |
493
|
|
|
args{ |
494
|
|
|
context.Background(), |
495
|
|
|
"cst_stTC2WHAuS", |
496
|
|
|
"sub_rVKGtNd6s3", |
497
|
|
|
}, |
498
|
|
|
true, |
499
|
|
|
errBadBaseURL, |
500
|
|
|
crashSrv, |
501
|
|
|
errorHandler, |
502
|
|
|
}, |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
for _, c := range cases { |
506
|
|
|
setup() |
507
|
|
|
defer teardown() |
508
|
|
|
|
509
|
|
|
t.Run(c.name, func(t *testing.T) { |
510
|
|
|
c.pre() |
511
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions/%s", c.args.customer, c.args.sid), c.handler) |
512
|
|
|
|
513
|
|
|
res, m, err := tClient.Subscriptions.Cancel(c.args.ctx, c.args.customer, c.args.sid) |
514
|
|
|
if c.wantErr { |
515
|
|
|
assert.NotNil(t, err) |
516
|
|
|
assert.EqualError(t, err, c.err.Error()) |
517
|
|
|
} else { |
518
|
|
|
assert.Nil(t, err) |
519
|
|
|
assert.IsType(t, &Subscription{}, m) |
520
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
521
|
|
|
} |
522
|
|
|
}) |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
func TestSubscriptionsService_List(t *testing.T) { |
527
|
|
|
setEnv() |
528
|
|
|
defer unsetEnv() |
529
|
|
|
|
530
|
|
|
type args struct { |
531
|
|
|
ctx context.Context |
532
|
|
|
customer string |
533
|
|
|
options *ListSubscriptionsOptions |
534
|
|
|
} |
535
|
|
|
cases := []struct { |
536
|
|
|
name string |
537
|
|
|
args args |
538
|
|
|
wantErr bool |
539
|
|
|
err error |
540
|
|
|
pre func() |
541
|
|
|
handler http.HandlerFunc |
542
|
|
|
}{ |
543
|
|
|
{ |
544
|
|
|
"list subscriptions works as expected.", |
545
|
|
|
args{ |
546
|
|
|
context.Background(), |
547
|
|
|
"cst_stTC2WHAuS", |
548
|
|
|
nil, |
549
|
|
|
}, |
550
|
|
|
false, |
551
|
|
|
nil, |
552
|
|
|
noPre, |
553
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
554
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
555
|
|
|
testMethod(t, r, "GET") |
556
|
|
|
|
557
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
558
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
559
|
|
|
} |
560
|
|
|
_, _ = w.Write([]byte(testdata.ListAllSubscriptionsResponse)) |
561
|
|
|
}, |
562
|
|
|
}, |
563
|
|
|
{ |
564
|
|
|
"list subscriptions with options works as expected.", |
565
|
|
|
args{ |
566
|
|
|
context.Background(), |
567
|
|
|
"cst_stTC2WHAuS", |
568
|
|
|
&ListSubscriptionsOptions{ |
569
|
|
|
Limit: 10, |
570
|
|
|
}, |
571
|
|
|
}, |
572
|
|
|
false, |
573
|
|
|
nil, |
574
|
|
|
noPre, |
575
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
576
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
577
|
|
|
testMethod(t, r, "GET") |
578
|
|
|
testQuery(t, r, "limit=10") |
579
|
|
|
|
580
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
581
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
582
|
|
|
} |
583
|
|
|
_, _ = w.Write([]byte(testdata.ListAllSubscriptionsResponse)) |
584
|
|
|
}, |
585
|
|
|
}, |
586
|
|
|
{ |
587
|
|
|
"list subscriptions, an error is returned from the server", |
588
|
|
|
args{ |
589
|
|
|
context.Background(), |
590
|
|
|
"cst_stTC2WHAuS", |
591
|
|
|
nil, |
592
|
|
|
}, |
593
|
|
|
true, |
594
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
595
|
|
|
noPre, |
596
|
|
|
errorHandler, |
597
|
|
|
}, |
598
|
|
|
{ |
599
|
|
|
"list subscriptions, an error occurs when parsing json", |
600
|
|
|
args{ |
601
|
|
|
context.Background(), |
602
|
|
|
"cst_stTC2WHAuS", |
603
|
|
|
nil, |
604
|
|
|
}, |
605
|
|
|
true, |
606
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
607
|
|
|
noPre, |
608
|
|
|
encodingHandler, |
609
|
|
|
}, |
610
|
|
|
{ |
611
|
|
|
"list subscriptions, invalid url when building request", |
612
|
|
|
args{ |
613
|
|
|
context.Background(), |
614
|
|
|
"cst_stTC2WHAuS", |
615
|
|
|
nil, |
616
|
|
|
}, |
617
|
|
|
true, |
618
|
|
|
errBadBaseURL, |
619
|
|
|
crashSrv, |
620
|
|
|
errorHandler, |
621
|
|
|
}, |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
for _, c := range cases { |
625
|
|
|
setup() |
626
|
|
|
defer teardown() |
627
|
|
|
|
628
|
|
|
t.Run(c.name, func(t *testing.T) { |
629
|
|
|
c.pre() |
630
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions", c.args.customer), c.handler) |
631
|
|
|
|
632
|
|
|
res, m, err := tClient.Subscriptions.List(c.args.ctx, c.args.customer, c.args.options) |
633
|
|
|
if c.wantErr { |
634
|
|
|
assert.NotNil(t, err) |
635
|
|
|
assert.EqualError(t, err, c.err.Error()) |
636
|
|
|
} else { |
637
|
|
|
assert.Nil(t, err) |
638
|
|
|
assert.IsType(t, &SubscriptionsList{}, m) |
639
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
640
|
|
|
} |
641
|
|
|
}) |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
func TestSubscriptionsService_All(t *testing.T) { |
646
|
|
|
setEnv() |
647
|
|
|
defer unsetEnv() |
648
|
|
|
|
649
|
|
|
type args struct { |
650
|
|
|
ctx context.Context |
651
|
|
|
options *ListSubscriptionsOptions |
652
|
|
|
} |
653
|
|
|
cases := []struct { |
654
|
|
|
name string |
655
|
|
|
args args |
656
|
|
|
wantErr bool |
657
|
|
|
err error |
658
|
|
|
pre func() |
659
|
|
|
handler http.HandlerFunc |
660
|
|
|
}{ |
661
|
|
|
{ |
662
|
|
|
"list subscriptions works as expected.", |
663
|
|
|
args{ |
664
|
|
|
context.Background(), |
665
|
|
|
nil, |
666
|
|
|
}, |
667
|
|
|
false, |
668
|
|
|
nil, |
669
|
|
|
noPre, |
670
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
671
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
672
|
|
|
testMethod(t, r, "GET") |
673
|
|
|
|
674
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
675
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
676
|
|
|
} |
677
|
|
|
_, _ = w.Write([]byte(testdata.ListAllSubscriptionsResponse)) |
678
|
|
|
}, |
679
|
|
|
}, |
680
|
|
|
{ |
681
|
|
|
"list subscriptions with options works as expected.", |
682
|
|
|
args{ |
683
|
|
|
context.Background(), |
684
|
|
|
&ListSubscriptionsOptions{ |
685
|
|
|
Limit: 10, |
686
|
|
|
}, |
687
|
|
|
}, |
688
|
|
|
false, |
689
|
|
|
nil, |
690
|
|
|
noPre, |
691
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
692
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
693
|
|
|
testMethod(t, r, "GET") |
694
|
|
|
testQuery(t, r, "limit=10") |
695
|
|
|
|
696
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
697
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
698
|
|
|
} |
699
|
|
|
_, _ = w.Write([]byte(testdata.ListAllSubscriptionsResponse)) |
700
|
|
|
}, |
701
|
|
|
}, |
702
|
|
|
{ |
703
|
|
|
"list subscriptions, an error is returned from the server", |
704
|
|
|
args{ |
705
|
|
|
context.Background(), |
706
|
|
|
nil, |
707
|
|
|
}, |
708
|
|
|
true, |
709
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
710
|
|
|
noPre, |
711
|
|
|
errorHandler, |
712
|
|
|
}, |
713
|
|
|
{ |
714
|
|
|
"list subscriptions, an error occurs when parsing json", |
715
|
|
|
args{ |
716
|
|
|
context.Background(), |
717
|
|
|
nil, |
718
|
|
|
}, |
719
|
|
|
true, |
720
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
721
|
|
|
noPre, |
722
|
|
|
encodingHandler, |
723
|
|
|
}, |
724
|
|
|
{ |
725
|
|
|
"list subscriptions, invalid url when building request", |
726
|
|
|
args{ |
727
|
|
|
context.Background(), |
728
|
|
|
nil, |
729
|
|
|
}, |
730
|
|
|
true, |
731
|
|
|
errBadBaseURL, |
732
|
|
|
crashSrv, |
733
|
|
|
errorHandler, |
734
|
|
|
}, |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
for _, c := range cases { |
738
|
|
|
setup() |
739
|
|
|
defer teardown() |
740
|
|
|
|
741
|
|
|
t.Run(c.name, func(t *testing.T) { |
742
|
|
|
c.pre() |
743
|
|
|
tMux.HandleFunc("/v2/subscriptions", c.handler) |
744
|
|
|
|
745
|
|
|
res, m, err := tClient.Subscriptions.All(c.args.ctx, c.args.options) |
746
|
|
|
if c.wantErr { |
747
|
|
|
assert.NotNil(t, err) |
748
|
|
|
assert.EqualError(t, err, c.err.Error()) |
749
|
|
|
} else { |
750
|
|
|
assert.Nil(t, err) |
751
|
|
|
assert.IsType(t, &SubscriptionsList{}, m) |
752
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
753
|
|
|
} |
754
|
|
|
}) |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
func TestSubscriptionsService_GetPayments(t *testing.T) { |
759
|
|
|
setEnv() |
760
|
|
|
defer unsetEnv() |
761
|
|
|
|
762
|
|
|
type args struct { |
763
|
|
|
ctx context.Context |
764
|
|
|
customer string |
765
|
|
|
subscription string |
766
|
|
|
options *ListSubscriptionsOptions |
767
|
|
|
} |
768
|
|
|
cases := []struct { |
769
|
|
|
name string |
770
|
|
|
args args |
771
|
|
|
wantErr bool |
772
|
|
|
err error |
773
|
|
|
pre func() |
774
|
|
|
handler http.HandlerFunc |
775
|
|
|
}{ |
776
|
|
|
{ |
777
|
|
|
"get subscription payments works as expected.", |
778
|
|
|
args{ |
779
|
|
|
context.Background(), |
780
|
|
|
"cst_stTC2WHAuS", |
781
|
|
|
"sub_rVKGtNd6s3", |
782
|
|
|
nil, |
783
|
|
|
}, |
784
|
|
|
false, |
785
|
|
|
nil, |
786
|
|
|
noPre, |
787
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
788
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
789
|
|
|
testMethod(t, r, "GET") |
790
|
|
|
|
791
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
792
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
793
|
|
|
} |
794
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentsResponse)) |
795
|
|
|
}, |
796
|
|
|
}, |
797
|
|
|
{ |
798
|
|
|
"get subscription payments with options works as expected.", |
799
|
|
|
args{ |
800
|
|
|
context.Background(), |
801
|
|
|
"cst_stTC2WHAuS", |
802
|
|
|
"sub_rVKGtNd6s3", |
803
|
|
|
&ListSubscriptionsOptions{ |
804
|
|
|
Limit: 10, |
805
|
|
|
}, |
806
|
|
|
}, |
807
|
|
|
false, |
808
|
|
|
nil, |
809
|
|
|
noPre, |
810
|
|
|
func(w http.ResponseWriter, r *http.Request) { |
811
|
|
|
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
812
|
|
|
testMethod(t, r, "GET") |
813
|
|
|
testQuery(t, r, "limit=10") |
814
|
|
|
|
815
|
|
|
if _, ok := r.Header[AuthHeader]; !ok { |
816
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
817
|
|
|
} |
818
|
|
|
_, _ = w.Write([]byte(testdata.ListPaymentsResponse)) |
819
|
|
|
}, |
820
|
|
|
}, |
821
|
|
|
{ |
822
|
|
|
"get subscription payments, an error is returned from the server", |
823
|
|
|
args{ |
824
|
|
|
context.Background(), |
825
|
|
|
"cst_stTC2WHAuS", |
826
|
|
|
"sub_rVKGtNd6s3", |
827
|
|
|
nil, |
828
|
|
|
}, |
829
|
|
|
true, |
830
|
|
|
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
831
|
|
|
noPre, |
832
|
|
|
errorHandler, |
833
|
|
|
}, |
834
|
|
|
{ |
835
|
|
|
"get subscription payments, an error occurs when parsing json", |
836
|
|
|
args{ |
837
|
|
|
context.Background(), |
838
|
|
|
"cst_stTC2WHAuS", |
839
|
|
|
"sub_rVKGtNd6s3", |
840
|
|
|
nil, |
841
|
|
|
}, |
842
|
|
|
true, |
843
|
|
|
fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
844
|
|
|
noPre, |
845
|
|
|
encodingHandler, |
846
|
|
|
}, |
847
|
|
|
{ |
848
|
|
|
"get subscription payments, invalid url when building request", |
849
|
|
|
args{ |
850
|
|
|
context.Background(), |
851
|
|
|
"cst_stTC2WHAuS", |
852
|
|
|
"sub_rVKGtNd6s3", |
853
|
|
|
nil, |
854
|
|
|
}, |
855
|
|
|
true, |
856
|
|
|
errBadBaseURL, |
857
|
|
|
crashSrv, |
858
|
|
|
errorHandler, |
859
|
|
|
}, |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
for _, c := range cases { |
863
|
|
|
setup() |
864
|
|
|
defer teardown() |
865
|
|
|
|
866
|
|
|
t.Run(c.name, func(t *testing.T) { |
867
|
|
|
c.pre() |
868
|
|
|
tMux.HandleFunc(fmt.Sprintf("/v2/customers/%s/subscriptions/%s/payments", c.args.customer, c.args.subscription), c.handler) |
869
|
|
|
|
870
|
|
|
res, m, err := tClient.Subscriptions.ListPayments(c.args.ctx, c.args.customer, c.args.subscription, c.args.options) |
871
|
|
|
if c.wantErr { |
872
|
|
|
assert.NotNil(t, err) |
873
|
|
|
assert.EqualError(t, err, c.err.Error()) |
874
|
|
|
} else { |
875
|
|
|
assert.Nil(t, err) |
876
|
|
|
assert.IsType(t, &PaymentList{}, m) |
877
|
|
|
assert.IsType(t, &http.Response{}, res.Response) |
878
|
|
|
} |
879
|
|
|
}) |
880
|
|
|
} |
881
|
|
|
} |
882
|
|
|
|