|
22 | 22 |
|
23 | 23 | import static com.spotify.github.v3.clients.ChecksClientTest.loadResource;
|
24 | 24 | import static org.hamcrest.CoreMatchers.hasItem;
|
| 25 | +import static org.hamcrest.CoreMatchers.instanceOf; |
25 | 26 | import static org.hamcrest.CoreMatchers.is;
|
| 27 | +import static org.hamcrest.CoreMatchers.notNullValue; |
26 | 28 | import static org.hamcrest.CoreMatchers.startsWith;
|
27 | 29 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 30 | +import static org.junit.Assert.assertThrows; |
28 | 31 |
|
29 | 32 | import com.fasterxml.jackson.core.JsonProcessingException;
|
30 | 33 | import com.spotify.github.jackson.Json;
|
|
33 | 36 | import java.io.File;
|
34 | 37 | import java.io.IOException;
|
35 | 38 | import java.net.URI;
|
| 39 | +import java.time.Duration; |
36 | 40 | import java.time.ZonedDateTime;
|
37 | 41 | import java.util.Objects;
|
| 42 | +import java.util.concurrent.TimeUnit; |
38 | 43 | import okhttp3.OkHttpClient;
|
39 | 44 | import okhttp3.mockwebserver.MockResponse;
|
40 | 45 | import okhttp3.mockwebserver.MockWebServer;
|
@@ -72,7 +77,12 @@ public GitHubAuthTest() throws JsonProcessingException {}
|
72 | 77 |
|
73 | 78 | @Before
|
74 | 79 | public void setUp() throws IOException {
|
75 |
| - client = new OkHttpClient(); |
| 80 | + client = |
| 81 | + new OkHttpClient.Builder() |
| 82 | + .connectTimeout(Duration.ofSeconds(1)) |
| 83 | + .readTimeout(Duration.ofSeconds(1)) |
| 84 | + .build(); |
| 85 | + |
76 | 86 | mockServer.start();
|
77 | 87 | url = mockServer.url("/").uri();
|
78 | 88 | checksClient =
|
@@ -139,10 +149,20 @@ public void fetchesANewInstallationTokenIfExpired() throws Exception {
|
139 | 149 | assertThat(mockServer.takeRequest().getPath(), is("/repos/foo/bar/check-runs/123"));
|
140 | 150 | }
|
141 | 151 |
|
142 |
| - @Test(expected = Exception.class) |
143 |
| - public void throwsIfFetchingInstallationTokenRequestIsUnsuccessful() { |
| 152 | + @Test |
| 153 | + public void throwsIfFetchingInstallationTokenRequestIsUnsuccessful() throws Exception { |
144 | 154 | mockServer.enqueue(new MockResponse().setResponseCode(500));
|
145 |
| - checksClient.getCheckRun(123).join(); |
| 155 | + RuntimeException ex = |
| 156 | + assertThrows(RuntimeException.class, () -> checksClient.getCheckRun(123).join()); |
| 157 | + |
| 158 | + assertThat(ex.getMessage(), is("Could not generate access token for github app")); |
| 159 | + |
| 160 | + assertThat(ex.getCause(), is(notNullValue())); |
| 161 | + assertThat(ex.getCause().getMessage(), startsWith("Got non-2xx status 500 when getting an access token from GitHub")); |
| 162 | + |
| 163 | + RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 164 | + // make sure it was the expected request that threw |
| 165 | + assertThat(recordedRequest.getRequestUrl().encodedPath(), is("/app/installations/1/access_tokens")); |
146 | 166 | }
|
147 | 167 |
|
148 | 168 | @Test
|
@@ -178,16 +198,24 @@ public void assertChecksApiContainsCorrectHeader() throws Exception {
|
178 | 198 | assertThat(request2.getMethod(), is("PATCH"));
|
179 | 199 | }
|
180 | 200 |
|
181 |
| - @Test(expected = Exception.class) |
| 201 | + @Test |
182 | 202 | public void assertInstallationEndpointWithoutInstallationThrows() {
|
183 | 203 | final GitHubClient github = GitHubClient.create(client, url, key, 123);
|
184 |
| - github.createRepositoryClient("foo", "bar").createChecksApiClient().getCheckRun(123).join(); |
| 204 | + final RuntimeException ex = assertThrows(RuntimeException.class, |
| 205 | + () -> github.createRepositoryClient("foo", "bar").createChecksApiClient().getCheckRun(123) |
| 206 | + .join()); |
| 207 | + assertThat(ex.getMessage(), is("This endpoint needs a client with an installation ID")); |
185 | 208 | }
|
186 | 209 |
|
187 |
| - @Test(expected = Exception.class) |
| 210 | + @Test |
188 | 211 | public void assertJwtEndpointWithNoKeyThrows() {
|
189 | 212 | final GitHubClient github = GitHubClient.create(client, url, "a-token");
|
190 |
| - github.createRepositoryClient("foo", "bar").createGithubAppClient().getInstallations().join(); |
| 213 | + |
| 214 | + final IllegalStateException ex = assertThrows(IllegalStateException.class, |
| 215 | + () -> github.createRepositoryClient("foo", "bar").createGithubAppClient().getInstallations() |
| 216 | + .join()); |
| 217 | + |
| 218 | + assertThat(ex.getMessage(), is("This endpoint needs a client with a private key for an App")); |
191 | 219 | }
|
192 | 220 |
|
193 | 221 | @Test
|
|
0 commit comments