|
| 1 | +package org.springframework.http.client; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.http.HttpClient; |
| 11 | +import java.net.http.HttpRequest; |
| 12 | +import java.net.http.HttpResponse; |
| 13 | +import java.net.http.HttpTimeoutException; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.concurrent.*; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.http.HttpHeaders; |
| 21 | +import org.springframework.http.HttpMethod; |
| 22 | + |
| 23 | +class JdkClientHttpRequestTest { |
| 24 | + |
| 25 | + private HttpClient mockHttpClient; |
| 26 | + private URI uri = URI.create("http://example.com"); |
| 27 | + private HttpMethod method = HttpMethod.GET; |
| 28 | + |
| 29 | + private ExecutorService executor; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setup() { |
| 33 | + mockHttpClient = mock(HttpClient.class); |
| 34 | + executor = Executors.newSingleThreadExecutor(); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + void tearDown() { |
| 39 | + executor.shutdownNow(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void executeInternal_withTimeout_shouldThrowHttpTimeoutException() throws Exception { |
| 44 | + Duration timeout = Duration.ofMillis(10); |
| 45 | + |
| 46 | + JdkClientHttpRequest request = new JdkClientHttpRequest(mockHttpClient, uri, method, executor, timeout); |
| 47 | + |
| 48 | + CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>(); |
| 49 | + |
| 50 | + when(mockHttpClient.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) |
| 51 | + .thenReturn(future); |
| 52 | + |
| 53 | + HttpHeaders headers = new HttpHeaders(); |
| 54 | + |
| 55 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 56 | + |
| 57 | + // Cancellation thread waits for startLatch, then cancels the future after a delay |
| 58 | + Thread canceller = new Thread(() -> { |
| 59 | + try { |
| 60 | + startLatch.await(); |
| 61 | + Thread.sleep(500); |
| 62 | + future.cancel(true); |
| 63 | + } catch (InterruptedException ignored) { |
| 64 | + } |
| 65 | + }); |
| 66 | + canceller.start(); |
| 67 | + |
| 68 | + IOException ex = assertThrows(IOException.class, () -> { |
| 69 | + startLatch.countDown(); |
| 70 | + request.executeInternal(headers, null); |
| 71 | + }); |
| 72 | + |
| 73 | + assertThat(ex) |
| 74 | + .isInstanceOf(HttpTimeoutException.class) |
| 75 | + .hasMessage("Request timed out"); |
| 76 | + |
| 77 | + canceller.join(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void executeInternal_withTimeout_shouldThrowIOException() throws Exception { |
| 82 | + Duration timeout = Duration.ofMillis(500); |
| 83 | + |
| 84 | + JdkClientHttpRequest request = new JdkClientHttpRequest(mockHttpClient, uri, method, executor, timeout); |
| 85 | + |
| 86 | + CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>(); |
| 87 | + |
| 88 | + when(mockHttpClient.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) |
| 89 | + .thenReturn(future); |
| 90 | + |
| 91 | + HttpHeaders headers = new HttpHeaders(); |
| 92 | + |
| 93 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 94 | + |
| 95 | + Thread canceller = new Thread(() -> { |
| 96 | + try { |
| 97 | + startLatch.await(); |
| 98 | + Thread.sleep(10); |
| 99 | + future.cancel(true); |
| 100 | + } catch (InterruptedException ignored) { |
| 101 | + } |
| 102 | + }); |
| 103 | + canceller.start(); |
| 104 | + |
| 105 | + IOException ex = assertThrows(IOException.class, () -> { |
| 106 | + startLatch.countDown(); |
| 107 | + request.executeInternal(headers, null); |
| 108 | + }); |
| 109 | + |
| 110 | + assertThat(ex) |
| 111 | + .isInstanceOf(IOException.class) |
| 112 | + .hasMessage("Request was cancelled"); |
| 113 | + |
| 114 | + canceller.join(); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments