|
28 | 28 | import java.util.concurrent.CompletableFuture; |
29 | 29 | import java.util.concurrent.CountDownLatch; |
30 | 30 | import java.util.concurrent.ExecutionException; |
31 | | -import java.util.concurrent.Executors; |
32 | 31 | import java.util.concurrent.atomic.AtomicReference; |
33 | 32 |
|
34 | 33 | @Timeout(value = 3) |
| 34 | +@SuppressWarnings("resource") |
35 | 35 | public class RedirectTargetTest { |
36 | 36 |
|
| 37 | + @Test |
| 38 | + @DisplayName("start() doesn't accept a relative path") |
| 39 | + public void testStartWithRelativePath() { |
| 40 | + Assertions.assertThrows(IllegalArgumentException.class, () -> RedirectTarget.start("hello")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("setSuccessResponse() fails on null") |
| 45 | + public void testSetSuccessNull() throws IOException { |
| 46 | + try (var target = RedirectTarget.start("/")) { |
| 47 | + Assertions.assertThrows(NullPointerException.class, () -> target.setSuccessResponse(null)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("setSuccessResponse() succeeds on non-null") |
| 53 | + public void testSetSuccessNonNull() throws IOException { |
| 54 | + try (var target = RedirectTarget.start("/")) { |
| 55 | + Assertions.assertDoesNotThrow(() -> target.setSuccessResponse(Response.empty(Response.Status.OK))); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("setErrorResponse() fails on null") |
| 61 | + public void testSetErrorNull() throws IOException { |
| 62 | + try (var target = RedirectTarget.start("/")) { |
| 63 | + Assertions.assertThrows(NullPointerException.class, () -> target.setErrorResponse(null)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("setErrorResponse() succeeds on non-null") |
| 69 | + public void testSetErrorNonNull() throws IOException { |
| 70 | + try (var target = RedirectTarget.start("/")) { |
| 71 | + Assertions.assertDoesNotThrow(() -> target.setErrorResponse(Response.empty(Response.Status.OK))); |
| 72 | + } |
| 73 | + } |
| 74 | + |
37 | 75 | @Test |
38 | 76 | @DisplayName("start() doesn't leak resource on error") |
39 | 77 | public void testStartExceptionally() throws IOException { |
@@ -134,9 +172,11 @@ public void testRedirectError() throws IOException, InterruptedException, URISyn |
134 | 172 |
|
135 | 173 | @Test |
136 | 174 | @DisplayName("http response 400 for missing code") |
137 | | - public void testRedirectMissingCode() throws IOException, InterruptedException { |
| 175 | + public void testRedirectMissingCode() throws IOException, InterruptedException, URISyntaxException { |
138 | 176 | try (var redirect = RedirectTarget.start("/")) { |
139 | | - var uri = redirect.getRedirectUri(); |
| 177 | + var baseUri = redirect.getRedirectUri(); |
| 178 | + var query = "state=" + redirect.getCsrfToken(); |
| 179 | + var uri = new URI(baseUri.getScheme(), baseUri.getAuthority(), baseUri.getPath(), query, baseUri.getFragment()); |
140 | 180 |
|
141 | 181 | var accessToken = receiveAsync(redirect); |
142 | 182 | var request = HttpRequest.newBuilder(uri).GET().build(); |
@@ -211,20 +251,21 @@ public void testInterrupt() throws IOException, InterruptedException { |
211 | 251 | var threadStarted = new CountDownLatch(1); |
212 | 252 | var threadExited = new CountDownLatch(1); |
213 | 253 | var exception = new AtomicReference<Exception>(); |
214 | | - var future = Executors.newSingleThreadExecutor().submit(() -> { |
| 254 | + var thread = new Thread(() -> { |
215 | 255 | try { |
216 | 256 | threadStarted.countDown(); |
217 | | - return redirect.receive(); |
| 257 | + redirect.receive(); |
218 | 258 | } catch (IOException e) { |
219 | 259 | exception.set(e); |
220 | 260 | throw new UncheckedIOException(e); |
221 | 261 | } finally { |
222 | 262 | threadExited.countDown(); |
223 | 263 | } |
224 | 264 | }); |
| 265 | + thread.start(); |
225 | 266 |
|
226 | 267 | threadStarted.await(); |
227 | | - future.cancel(true); |
| 268 | + thread.interrupt(); |
228 | 269 | threadExited.await(); |
229 | 270 |
|
230 | 271 | Assertions.assertInstanceOf(ClosedByInterruptException.class, exception.get()); |
|
0 commit comments