1
2
3
4
5 package com.jcabi.github.wire;
6
7 import com.jcabi.http.request.FakeRequest;
8 import java.io.IOException;
9 import java.net.HttpURLConnection;
10 import java.util.concurrent.TimeUnit;
11 import org.hamcrest.MatcherAssert;
12 import org.hamcrest.Matchers;
13 import org.junit.jupiter.api.Test;
14
15
16
17
18
19 final class CarefulWireTest {
20
21
22
23 private static final String OK = "OK";
24
25
26
27
28 private static final String REMAINING_HEADER = "X-RateLimit-Remaining";
29
30
31
32
33
34 @Test
35 void waitUntilReset() throws IOException {
36 final int threshold = 10;
37
38 final long reset = TimeUnit.MILLISECONDS
39 .toSeconds(System.currentTimeMillis()) + 5L;
40 new FakeRequest()
41 .withStatus(HttpURLConnection.HTTP_OK)
42 .withReason(CarefulWireTest.OK)
43 .withHeader(CarefulWireTest.REMAINING_HEADER, "9")
44 .withHeader("X-RateLimit-Reset", String.valueOf(reset))
45 .through(CarefulWire.class, threshold)
46 .fetch();
47 final long now = TimeUnit.MILLISECONDS
48 .toSeconds(System.currentTimeMillis());
49 MatcherAssert.assertThat(
50 "Value is not greater than expected",
51 now,
52 Matchers.greaterThanOrEqualTo(reset)
53 );
54 }
55
56
57
58
59
60 @Test
61 void tolerateMissingRateLimitRemainingHeader() throws IOException {
62 final int threshold = 10;
63
64 new FakeRequest()
65 .withStatus(HttpURLConnection.HTTP_OK)
66 .withReason(CarefulWireTest.OK)
67 .through(CarefulWire.class, threshold)
68 .fetch();
69 MatcherAssert.assertThat(
70 "Did not crash when X-RateLimit-Remaining header was absent",
71 true,
72 Matchers.is(true)
73 );
74 }
75
76
77
78
79
80 @Test
81 void tolerateMissingRateLimitResetHeader() throws IOException {
82 final int threshold = 8;
83
84 new FakeRequest()
85 .withStatus(HttpURLConnection.HTTP_OK)
86 .withReason(CarefulWireTest.OK)
87 .withHeader(CarefulWireTest.REMAINING_HEADER, "7")
88 .through(CarefulWire.class, threshold)
89 .fetch();
90 MatcherAssert.assertThat(
91 "Did not crash when X-RateLimit-Reset header was absent",
92 true,
93 Matchers.is(true)
94 );
95 }
96 }