Repository navigation
Expand file tree
/
Copy pathAbuseLimitHandlerTest.java
More file actions
629 lines (545 loc) · 31.7 KB
/
Copy pathAbuseLimitHandlerTest.java
File metadata and controls
629 lines (545 loc) · 31.7 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package org.kohsuke.github;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
// TODO: Auto-generated Javadoc
/**
* Test showing the behavior of OkHttpConnector with and without cache.
* <p>
* Key take aways:
*
* <ul>
* <li>These tests are artificial and intended to highlight the differences in behavior between scenarios. However, the
* differences they indicate are stark.</li>
* <li>Caching reduces rate limit consumption by at least a factor of two in even the simplest case.</li>
* <li>The OkHttp cache is pretty smart and will often connect read and write requests made on the same client and
* invalidate caches.</li>
* <li>Changes made outside the current client cause the OkHttp cache to return stale data. This is expected and correct
* behavior.</li>
* <li>"max-age=0" addresses the problem of external changes by revalidating caches for each request. This produces the
* same number of requests as OkHttp without caching, but those requests only count towards the GitHub rate limit if
* data has changes.</li>
* </ul>
*
* @author Liam Newman
*/
public class AbuseLimitHandlerTest extends AbstractGitHubWireMockTest {
/**
* This is making an assertion about the behaviour of the mock, so it's useful for making sure we're on the right
* mock, but should not be used to validate assumptions about the behaviour of the actual GitHub API.
*/
private static void checkErrorMessageMatches(GitHubConnectorResponse connectorResponse, String substring)
throws IOException {
try (InputStream errorStream = connectorResponse.bodyStream()) {
assertThat(errorStream, notNullValue());
String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
assertThat(errorString, containsString(substring));
}
}
/**
* Instantiates a new abuse limit handler test.
*/
public AbuseLimitHandlerTest() {
useDefaultGitHub = false;
}
/**
* Test handler fail.
*
* @throws Exception
* the exception
*/
@Test
public void testHandler_Fail() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
// Verify
// assertThat(GitHubClient.parseInstant(connectorResponse.header("Date")).toEpochMilli(),
// Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
assertThat(connectorResponse.header("Expires"), nullValue());
// assertThat(GitHubClient.parseInstant(connectorResponse.header("Last-Modified")).toEpochMilli(),
// equalTo(1581014017000L));
assertThat(connectorResponse.statusCode(), equalTo(403));
assertThat(connectorResponse.header("Status"), containsString("Forbidden"));
// assertThat(uc.getHeaderFieldInt("X-RateLimit-Limit", 10), equalTo(5000));
// assertThat(uc.getHeaderFieldInt("X-RateLimit-Remaining", 10), equalTo(4000));
// assertThat(uc.getHeaderFieldInt("X-Foo", 20), equalTo(20));
// assertThat(uc.getHeaderFieldLong("X-RateLimit-Limit", 15L), equalTo(5000L));
// assertThat(uc.getHeaderFieldLong("X-RateLimit-Remaining", 15L), equalTo(4000L));
// assertThat(uc.getHeaderFieldLong("X-Foo", 20L), equalTo(20L));
//
// assertThat(uc.getContentEncoding(), nullValue());
// assertThat(uc.getContentType(), equalTo("application/json; charset=utf-8"));
// assertThat(uc.getContentLength(), equalTo(-1));
//
// // getting an input stream in an error case should throw
// IOException ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());
//
// try (InputStream errorStream = uc.getErrorStream()) {
// assertThat(errorStream, notNullValue());
// String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
// assertThat(errorString, containsString("Must have push access to repository"));
// }
//
// // calling again should still error
// ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());
//
// // calling again on a GitHubConnectorResponse should yield the same value
// if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
// try (InputStream errorStream = uc.getErrorStream()) {
// assertThat(errorStream, notNullValue());
// String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
// assertThat(errorString, containsString("Must have push access to repository"));
// }
// } else {
// try (InputStream errorStream = uc.getErrorStream()) {
// assertThat(errorStream, notNullValue());
// String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
// fail();
// } catch (IOException ex) {
// assertThat(ex, notNullValue());
// assertThat(ex.getMessage(), containsString("stream is closed"));
// }
// }
assertThat(connectorResponse.allHeaders(), instanceOf(Map.class));
assertThat(connectorResponse.allHeaders().size(), Matchers.greaterThan(25));
assertThat(connectorResponse.header("Status"), equalTo("403 Forbidden"));
// assertThat(uc.getRequestProperty("Accept"), equalTo("application/vnd.github.v3+json"));
// checkErrorMessageMatches(uc, "Must have push access to repository");
// // calling again should still error
// ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream());
// // calling again on a GitHubConnectorResponse should yield the same value
// if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
// checkErrorMessageMatches(uc, "Must have push access to repository");
// } else {
// try (InputStream errorStream = uc.getErrorStream()) {
// assertThat(errorStream, notNullValue());
// String errorString = IOUtils.toString(errorStream, StandardCharsets.UTF_8);
// fail();
// } catch (IOException ex) {
// assertThat(ex, notNullValue());
// assertThat(ex.getMessage(), containsString("stream is closed"));
// }
// }
// assertThat(uc.getHeaderFields(), instanceOf(Map.class));
// assertThat(uc.getHeaderFields().size(), greaterThan(25));
// assertThat(uc.getHeaderField("Status"), equalTo("403 Forbidden"));
// String key = uc.getHeaderFieldKey(1);
// assertThat(key, notNullValue());
// assertThat(uc.getHeaderField(1), notNullValue());
// assertThat(uc.getHeaderField(1), equalTo(uc.getHeaderField(key)));
// assertThat(uc.getRequestProperty("Accept"), equalTo("application/vnd.github+json"));
// Assert.assertThrows(IllegalStateException.class, () -> uc.getRequestProperties());
// // Actions that are not allowed because connection already opened.
// Assert.assertThrows(IllegalStateException.class, () -> uc.addRequestProperty("bogus",
// "item"));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setAllowUserInteraction(true));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setChunkedStreamingMode(1));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setDoInput(true));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setDoOutput(true));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setFixedLengthStreamingMode(1));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setFixedLengthStreamingMode(1L));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setIfModifiedSince(1L));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setRequestProperty("bogus",
// "thing"));
// Assert.assertThrows(IllegalStateException.class, () -> uc.setUseCaches(true));
// if (uc.toString().contains("GitHubConnectorResponseHttpUrlConnectionAdapter")) {
// Assert.assertThrows(UnsupportedOperationException.class,
// () -> uc.getAllowUserInteraction());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getConnectTimeout());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getContent());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getContent(null));
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDefaultUseCaches());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDoInput());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDoOutput());
// Assert.assertThrows(UnsupportedOperationException.class,
// () -> uc.getInstanceFollowRedirects());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getOutputStream());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getPermission());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getReadTimeout());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getUseCaches());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.usingProxy());
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setConnectTimeout(10));
// Assert.assertThrows(UnsupportedOperationException.class,
// () -> uc.setDefaultUseCaches(true));
// Assert.assertThrows(UnsupportedOperationException.class,
// () -> uc.setInstanceFollowRedirects(true));
// Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setReadTimeout(10));
// Assert.assertThrows(ProtocolException.class, () -> uc.setRequestMethod("GET"));
// } else {
// uc.getDefaultUseCaches();
// assertThat(uc.getDoInput(), is(true));
// // Depending on the underlying implementation, this may throw or not
// // Assert.assertThrows(IllegalStateException.class, () -> uc.setRequestMethod("GET"));
// }
// // ignored
// uc.connect();
// // disconnect does nothing, never throws
// uc.disconnect();
// uc.disconnect();
// // ignored
// uc.connect();
GitHubAbuseLimitHandler.FAIL.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
try {
getTempRepository();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getMessage(), equalTo("Abuse limit reached"));
}
assertThat(mockGitHub.getRequestCount(), equalTo(2));
}
/**
* Test handler http status fail.
*
* @throws Exception
* the exception
*/
@Test
public void testHandler_HttpStatus_Fail() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(GitHubAbuseLimitHandler.FAIL)
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
try {
gitHub.createRequest()
.withUrlPath("/repos/" + GITHUB_API_TEST_ORG + "/temp-testHandler_Fail")
.fetchHttpStatusCode();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getMessage(), equalTo("Abuse limit reached"));
}
assertThat(mockGitHub.getRequestCount(), equalTo(2));
}
/**
* Test handler wait.
*
* @throws Exception
* the exception
*/
@Test
public void testHandler_Wait() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(GitHubAbuseLimitHandler.WAIT)
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Test handler wait stuck.
*
* @throws Exception
* the exception
*/
@Test
public void testHandler_WaitStuck() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
try {
getTempRepository();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHIOException.class));
}
assertThat(mockGitHub.getRequestCount(), equalTo(4));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler is set to WAIT then the handler waits
* appropriately when secondary rate limits are encountered.
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
// Verify
// assertThat(uc.getDate(), Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
// assertThat(uc.getExpiration(), equalTo(0L));
// assertThat(uc.getIfModifiedSince(), equalTo(0L));
// assertThat(uc.getLastModified(), equalTo(1581014017000L));
assertThat(connectorResponse.request().method(), equalTo("GET"));
assertThat(connectorResponse.statusCode(), equalTo(403));
// assertThat(uc.getResponseMessage(), containsString("Forbidden"));
assertThat(connectorResponse.request().url().toString(),
endsWith("/repos/hub4j-test-org/temp-testHandler_Wait_Secondary_Limits"));
assertThat(connectorResponse.header("X-RateLimit-Limit"), equalTo("5000"));
assertThat(connectorResponse.header("X-RateLimit-Remaining"), equalTo("4000"));
assertThat(connectorResponse.header("X-Foo"), is(nullValue())); // equalTo(20));
assertThat(connectorResponse.header("gh-limited-by"),
equalTo("search-elapsed-time-shared-grouped"));
// assertThat(uc.getContentEncoding(), nullValue());
// assertThat(uc.getContentType(), equalTo("application/json; charset=utf-8"));
// assertThat(uc.getContentLength(), equalTo(-1));
assertThat(connectorResponse.allHeaders(), instanceOf(Map.class));
assertThat(connectorResponse.allHeaders().size(), greaterThan(25));
assertThat(GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS, equalTo(61 * 1000l));
GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS = 3210l;
long waitTime = parseWaitTime(connectorResponse);
assertThat(waitTime, equalTo(GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS));
assertThat(connectorResponse.header("Status"), equalTo("403 Forbidden"));
checkErrorMessageMatches(connectorResponse,
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again");
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler encounters a 403 response that contains
* the secondary rate limit marker in the body, but neither the {@code Retry-After} nor the {@code gh-limited-by}
* header. Without body-based detection, this case slips through {@code isError()} and surfaces as a generic
* {@code HttpException} (see hub4j/github-api#2009).
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits_Body_Marker_Only() throws Exception {
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests.
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
assertThat(connectorResponse.request().method(), equalTo("GET"));
assertThat(connectorResponse.statusCode(), equalTo(403));
assertThat(connectorResponse.request().url().toString(),
endsWith(
"/repos/hub4j-test-org/temp-testHandler_Wait_Secondary_Limits_Body_Marker_Only"));
assertThat(connectorResponse.header("Retry-After"), nullValue());
assertThat(connectorResponse.header("gh-limited-by"), nullValue());
checkErrorMessageMatches(connectorResponse,
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again");
long waitTime = parseWaitTime(connectorResponse);
assertThat(waitTime, equalTo(GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS));
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler is set to WAIT then the handler waits
* appropriately when secondary rate limits are encountered.
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits_Too_Many_Requests() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
// Verify the test data is what we expected it to be for this test case
// assertThat(uc.getDate(), Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
// assertThat(uc.getExpiration(), equalTo(0L));
// assertThat(uc.getIfModifiedSince(), equalTo(0L));
// assertThat(uc.getLastModified(), equalTo(1581014017000L));
assertThat(connectorResponse.request().method(), equalTo("GET"));
assertThat(connectorResponse.statusCode(), equalTo(429));
assertThat(connectorResponse.request().url().toString(),
endsWith(
"/repos/hub4j-test-org/temp-testHandler_Wait_Secondary_Limits_Too_Many_Requests"));
assertThat(connectorResponse.allHeaders(), instanceOf(Map.class));
assertThat(connectorResponse.header("Status"), equalTo("429 Too Many Requests"));
assertThat(connectorResponse.header("Retry-After"), equalTo("8"));
checkErrorMessageMatches(connectorResponse,
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again");
long waitTime = parseWaitTime(connectorResponse);
assertThat(waitTime, equalTo(8 * 1000l));
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler with a date retry.
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits_Too_Many_Requests_Date_Retry_After() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
// Verify the test data is what we expected it to be for this test case
assertThat(connectorResponse.request().method(), equalTo("GET"));
assertThat(connectorResponse.statusCode(), equalTo(429));
assertThat(connectorResponse.request().url().toString(),
endsWith(
"/repos/hub4j-test-org/temp-testHandler_Wait_Secondary_Limits_Too_Many_Requests_Date_Retry_After"));
assertThat(connectorResponse.header("Status"), equalTo("429 Too Many Requests"));
assertThat(connectorResponse.header("Retry-After"), containsString("GMT"));
checkErrorMessageMatches(connectorResponse,
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again");
long waitTime = parseWaitTime(connectorResponse);
assertThat(waitTime, Matchers.lessThan(GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS));
assertThat(waitTime, equalTo(8 * 1000l));
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler with a date retry, when the response is
* missing the main "date" header.
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits_Too_Many_Requests_Date_Retry_After_Missing_Date_Header()
throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
long waitTime = parseWaitTime(connectorResponse);
// This will now use system time, so might not be exactly 8s
assertThat(waitTime, Matchers.greaterThan((8 - 1) * 1000l));
assertThat(waitTime, Matchers.lessThan((8 + 1) * 1000l));
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Tests the behavior of the GitHub API client when the abuse limit handler with a no retry after header.
*
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void testHandler_Wait_Secondary_Limits_Too_Many_Requests_No_Retry_After() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
/**
* Overriding method because the actual method will wait for one minute causing slowness in unit
* tests
*/
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
// Verify the test data is what we expected it to be for this test case
assertThat(connectorResponse.request().method(), equalTo("GET"));
assertThat(connectorResponse.statusCode(), equalTo(429));
assertThat(connectorResponse.request().url().toString(),
endsWith(
"/repos/hub4j-test-org/temp-testHandler_Wait_Secondary_Limits_Too_Many_Requests_No_Retry_After"));
// assertThat(uc.getContentEncoding(), nullValue());
// assertThat(uc.getContentType(), equalTo("application/json; charset=utf-8"));
assertThat(connectorResponse.allHeaders(), instanceOf(Map.class));
assertThat(connectorResponse.header("Status"), equalTo("429 Too Many Requests"));
assertThat(connectorResponse.header("Retry-After"), nullValue());
checkErrorMessageMatches(connectorResponse,
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again");
GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS = 3210l;
long waitTime = parseWaitTime(connectorResponse);
assertThat(waitTime, equalTo(GitHubAbuseLimitHandler.DEFAULT_WAIT_MILLIS));
GitHubAbuseLimitHandler.WAIT.onError(connectorResponse);
}
})
.build();
gitHub.getMyself();
assertThat(mockGitHub.getRequestCount(), equalTo(1));
getTempRepository();
assertThat(mockGitHub.getRequestCount(), equalTo(3));
}
/**
* Gets the wire mock options.
*
* @return the wire mock options
*/
@Override
protected WireMockConfiguration getWireMockOptions() {
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
}
}