View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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   * Test case for {@link CarefulWire}.
17   * @since 0.1
18   */
19  final class CarefulWireTest {
20      /**
21       * HTTP 200 status reason.
22       */
23      private static final String OK = "OK";
24  
25      /**
26       * Name of GitHub's number-of-requests-remaining rate limit header.
27       */
28      private static final String REMAINING_HEADER = "X-RateLimit-Remaining";
29  
30      /**
31       * CarefulWire can wait until the limit reset.
32       * @throws IOException If some problem inside
33       */
34      @Test
35      void waitUntilReset() throws IOException {
36          final int threshold = 10;
37          // @checkstyle MagicNumber (2 lines)
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       * CarefulWire can tolerate the lack the X-RateLimit-Remaining header.
58       * @throws IOException If some problem inside
59       */
60      @Test
61      void tolerateMissingRateLimitRemainingHeader() throws IOException {
62          final int threshold = 10;
63          // @checkstyle MagicNumber (1 lines)
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       * CarefulWire can tolerate the lack the X-RateLimit-Reset header.
78       * @throws IOException If some problem inside
79       */
80      @Test
81      void tolerateMissingRateLimitResetHeader() throws IOException {
82          final int threshold = 8;
83          // @checkstyle MagicNumber (1 lines)
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  }