|
1 | 1 | package org.kohsuke.github; |
2 | 2 |
|
3 | 3 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.hamcrest.Matchers; |
| 6 | +import org.junit.Assert; |
4 | 7 | import org.junit.Test; |
5 | 8 |
|
6 | 9 | import java.io.IOException; |
| 10 | +import java.io.InputStream; |
7 | 11 | import java.net.HttpURLConnection; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Date; |
| 14 | +import java.util.Map; |
8 | 15 |
|
9 | 16 | import static org.hamcrest.CoreMatchers.*; |
10 | 17 | import static org.hamcrest.core.IsInstanceOf.instanceOf; |
@@ -46,7 +53,96 @@ public void testHandler_Fail() throws Exception { |
46 | 53 | snapshotNotAllowed(); |
47 | 54 |
|
48 | 55 | gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()) |
49 | | - .withRateLimitHandler(RateLimitHandler.FAIL) |
| 56 | + .withRateLimitHandler(new RateLimitHandler() { |
| 57 | + @Override |
| 58 | + public void onError(IOException e, HttpURLConnection uc) throws IOException { |
| 59 | + // Verify |
| 60 | + assertThat(uc.getDate(), Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000)); |
| 61 | + assertThat(uc.getExpiration(), equalTo(0L)); |
| 62 | + assertThat(uc.getIfModifiedSince(), equalTo(0L)); |
| 63 | + assertThat(uc.getLastModified(), equalTo(1581014017000L)); |
| 64 | + assertThat(uc.getRequestMethod(), equalTo("GET")); |
| 65 | + assertThat(uc.getResponseCode(), equalTo(403)); |
| 66 | + assertThat(uc.getResponseMessage(), equalTo("403 Forbidden")); |
| 67 | + assertThat(uc.getURL().toString(), endsWith("/repos/hub4j-test-org/temp-testHandler_Fail")); |
| 68 | + assertThat(uc.getHeaderFieldInt("X-RateLimit-Limit", 10), equalTo(5000)); |
| 69 | + assertThat(uc.getHeaderFieldInt("X-RateLimit-Remaining", 10), equalTo(0)); |
| 70 | + assertThat(uc.getHeaderFieldInt("X-Foo", 20), equalTo(20)); |
| 71 | + assertThat(uc.getHeaderFieldLong("X-RateLimit-Limit", 15L), equalTo(5000L)); |
| 72 | + assertThat(uc.getHeaderFieldLong("X-RateLimit-Remaining", 15L), equalTo(0L)); |
| 73 | + assertThat(uc.getHeaderFieldLong("X-Foo", 20L), equalTo(20L)); |
| 74 | + |
| 75 | + assertThat(uc.getContentEncoding(), nullValue()); |
| 76 | + assertThat(uc.getContentType(), equalTo("application/json; charset=utf-8")); |
| 77 | + assertThat(uc.getContentLength(), equalTo(-1)); |
| 78 | + |
| 79 | + // getting an input stream in an error case should throw |
| 80 | + IOException ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream()); |
| 81 | + |
| 82 | + InputStream errorStream = uc.getErrorStream(); |
| 83 | + assertThat(errorStream, notNullValue()); |
| 84 | + String error = IOUtils.toString(errorStream, StandardCharsets.UTF_8); |
| 85 | + assertThat(error, containsString("Must have push access to repository")); |
| 86 | + |
| 87 | + // calling again should still error |
| 88 | + ioEx = Assert.assertThrows(IOException.class, () -> uc.getInputStream()); |
| 89 | + |
| 90 | + assertThat(uc.getHeaderFields(), instanceOf(Map.class)); |
| 91 | + assertThat(uc.getHeaderFields().size(), equalTo(27)); |
| 92 | + assertThat(uc.getHeaderField("Status"), equalTo("403 Forbidden")); |
| 93 | + assertThat(uc.getHeaderField(0), equalTo("HTTP/1.1 403 Forbidden")); |
| 94 | + assertThat(uc.getHeaderFieldKey(0), nullValue()); |
| 95 | + assertThat(uc.getHeaderFieldKey(1), equalTo("Access-Control-Allow-Origin")); |
| 96 | + |
| 97 | + assertThat(uc.getRequestProperties(), instanceOf(Map.class)); |
| 98 | + assertThat(uc.getRequestProperties().size(), equalTo(3)); |
| 99 | + assertThat(uc.getRequestProperty("Accept"), equalTo("application/vnd.github.v3+json")); |
| 100 | + |
| 101 | + // disconnect does nothing, never throws |
| 102 | + uc.disconnect(); |
| 103 | + uc.disconnect(); |
| 104 | + |
| 105 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.connect()); |
| 106 | + |
| 107 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getAllowUserInteraction()); |
| 108 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getConnectTimeout()); |
| 109 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getContent()); |
| 110 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getContent(null)); |
| 111 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDefaultUseCaches()); |
| 112 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDoInput()); |
| 113 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getDoOutput()); |
| 114 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getInstanceFollowRedirects()); |
| 115 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getOutputStream()); |
| 116 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getPermission()); |
| 117 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getReadTimeout()); |
| 118 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.getUseCaches()); |
| 119 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.usingProxy()); |
| 120 | + |
| 121 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 122 | + () -> uc.addRequestProperty("bogus", "item")); |
| 123 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 124 | + () -> uc.setAllowUserInteraction(true)); |
| 125 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setChunkedStreamingMode(1)); |
| 126 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setConnectTimeout(10)); |
| 127 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setDefaultUseCaches(true)); |
| 128 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setDoInput(true)); |
| 129 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setDoOutput(true)); |
| 130 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 131 | + () -> uc.setFixedLengthStreamingMode(1)); |
| 132 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 133 | + () -> uc.setFixedLengthStreamingMode(1L)); |
| 134 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setIfModifiedSince(1L)); |
| 135 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 136 | + () -> uc.setInstanceFollowRedirects(true)); |
| 137 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setReadTimeout(10)); |
| 138 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setRequestMethod("GET")); |
| 139 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 140 | + () -> uc.setRequestProperty("bogus", "thing")); |
| 141 | + Assert.assertThrows(UnsupportedOperationException.class, () -> uc.setUseCaches(true)); |
| 142 | + |
| 143 | + RateLimitHandler.FAIL.onError(e, uc); |
| 144 | + } |
| 145 | + }) |
50 | 146 | .build(); |
51 | 147 |
|
52 | 148 | gitHub.getMyself(); |
|
0 commit comments