1 /** 2 * Copyright (c) 2013-2023, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.github.wire; 31 32 import com.jcabi.http.request.FakeRequest; 33 import java.io.IOException; 34 import java.net.HttpURLConnection; 35 import java.util.concurrent.TimeUnit; 36 import org.hamcrest.MatcherAssert; 37 import org.hamcrest.Matchers; 38 import org.junit.Test; 39 40 /** 41 * Test case for {@link CarefulWire}. 42 * 43 * @author Alexander Sinyagin (sinyagin.alexander@gmail.com) 44 * @version $Id: 03f0a7ceeafe1d6da3adaf524e7db6b1d7d9b288 $ 45 */ 46 public final class CarefulWireTest { 47 /** 48 * HTTP 200 status reason. 49 */ 50 private static final String OK = "OK"; 51 /** 52 * Name of GitHub's number-of-requests-remaining rate limit header. 53 */ 54 private static final String REMAINING_HEADER = "X-RateLimit-Remaining"; 55 56 /** 57 * CarefulWire can wait until the limit reset. 58 * @throws IOException If some problem inside 59 */ 60 @Test 61 public void waitUntilReset() throws IOException { 62 final int threshold = 10; 63 // @checkstyle MagicNumber (2 lines) 64 final long reset = TimeUnit.MILLISECONDS 65 .toSeconds(System.currentTimeMillis()) + 5L; 66 new FakeRequest() 67 .withStatus(HttpURLConnection.HTTP_OK) 68 .withReason(OK) 69 .withHeader(REMAINING_HEADER, "9") 70 .withHeader("X-RateLimit-Reset", String.valueOf(reset)) 71 .through(CarefulWire.class, threshold) 72 .fetch(); 73 final long now = TimeUnit.MILLISECONDS 74 .toSeconds(System.currentTimeMillis()); 75 MatcherAssert.assertThat(now, Matchers.greaterThanOrEqualTo(reset)); 76 } 77 78 /** 79 * CarefulWire can tolerate the lack the X-RateLimit-Remaining header. 80 * @throws IOException If some problem inside 81 */ 82 @Test 83 public void tolerateMissingRateLimitRemainingHeader() throws IOException { 84 final int threshold = 10; 85 // @checkstyle MagicNumber (1 lines) 86 new FakeRequest() 87 .withStatus(HttpURLConnection.HTTP_OK) 88 .withReason(OK) 89 .through(CarefulWire.class, threshold) 90 .fetch(); 91 MatcherAssert.assertThat( 92 "Did not crash when X-RateLimit-Remaining header was absent", 93 true 94 ); 95 } 96 97 /** 98 * CarefulWire can tolerate the lack the X-RateLimit-Reset header. 99 * @throws IOException If some problem inside 100 */ 101 @Test 102 public void tolerateMissingRateLimitResetHeader() throws IOException { 103 final int threshold = 8; 104 // @checkstyle MagicNumber (1 lines) 105 new FakeRequest() 106 .withStatus(HttpURLConnection.HTTP_OK) 107 .withReason(OK) 108 .withHeader(REMAINING_HEADER, "7") 109 .through(CarefulWire.class, threshold) 110 .fetch(); 111 MatcherAssert.assertThat( 112 "Did not crash when X-RateLimit-Reset header was absent", 113 true 114 ); 115 } 116 }