|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.http.HttpClient; |
| 23 | +import java.net.http.HttpRequest; |
| 24 | +import java.net.http.HttpResponse; |
| 25 | +import java.net.http.HttpTimeoutException; |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import org.springframework.http.HttpHeaders; |
| 36 | +import org.springframework.http.HttpMethod; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 39 | +import static org.mockito.Mockito.any; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | + |
| 43 | +/** |
| 44 | + * Unit tests for {@link JdkClientHttpRequest}. |
| 45 | + */ |
| 46 | +class JdkClientHttpRequestTests { |
| 47 | + |
| 48 | + private final HttpClient client = mock(HttpClient.class); |
| 49 | + |
| 50 | + private ExecutorService executor; |
| 51 | + |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setup() { |
| 55 | + executor = Executors.newSingleThreadExecutor(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void tearDown() { |
| 60 | + executor.shutdownNow(); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Test |
| 65 | + void futureCancelledAfterTimeout() { |
| 66 | + CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>(); |
| 67 | + when(client.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(future); |
| 68 | + |
| 69 | + assertThatThrownBy(() -> createRequest(Duration.ofMillis(10)).executeInternal(new HttpHeaders(), null)) |
| 70 | + .isExactlyInstanceOf(HttpTimeoutException.class); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void futureCancelled() { |
| 75 | + CompletableFuture<HttpResponse<InputStream>> future = new CompletableFuture<>(); |
| 76 | + future.cancel(true); |
| 77 | + when(client.sendAsync(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(future); |
| 78 | + |
| 79 | + assertThatThrownBy(() -> createRequest(null).executeInternal(new HttpHeaders(), null)) |
| 80 | + .isExactlyInstanceOf(IOException.class); |
| 81 | + } |
| 82 | + |
| 83 | + private JdkClientHttpRequest createRequest(Duration timeout) { |
| 84 | + return new JdkClientHttpRequest(client, URI.create("http://abc.com"), HttpMethod.GET, executor, timeout); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments