|
|
|
@ -47,6 +47,7 @@ import static org.mockito.Mockito.mock;
@@ -47,6 +47,7 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
import static org.mockito.Mockito.verifyNoInteractions; |
|
|
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
|
|
|
|
import static org.mockito.Mockito.when; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unit tests for {@link DefaultWebClient}. |
|
|
|
@ -69,6 +70,7 @@ public class DefaultWebClientTests {
@@ -69,6 +70,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
@BeforeEach |
|
|
|
|
public void setup() { |
|
|
|
|
ClientResponse mockResponse = mock(ClientResponse.class); |
|
|
|
|
when(mockResponse.bodyToMono(Void.class)).thenReturn(Mono.empty()); |
|
|
|
|
given(this.exchangeFunction.exchange(this.captor.capture())).willReturn(Mono.just(mockResponse)); |
|
|
|
|
this.builder = WebClient.builder().baseUrl("/base").exchangeFunction(this.exchangeFunction); |
|
|
|
|
} |
|
|
|
@ -77,7 +79,7 @@ public class DefaultWebClientTests {
@@ -77,7 +79,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
@Test |
|
|
|
|
public void basic() { |
|
|
|
|
this.builder.build().get().uri("/path") |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.url().toString()).isEqualTo("/base/path"); |
|
|
|
@ -89,7 +91,7 @@ public class DefaultWebClientTests {
@@ -89,7 +91,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
public void uriBuilder() { |
|
|
|
|
this.builder.build().get() |
|
|
|
|
.uri(builder -> builder.path("/path").queryParam("q", "12").build()) |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.url().toString()).isEqualTo("/base/path?q=12"); |
|
|
|
@ -98,8 +100,8 @@ public class DefaultWebClientTests {
@@ -98,8 +100,8 @@ public class DefaultWebClientTests {
|
|
|
|
|
@Test // gh-22705
|
|
|
|
|
public void uriBuilderWithUriTemplate() { |
|
|
|
|
this.builder.build().get() |
|
|
|
|
.uri("/path/{id}", builder -> builder.queryParam("q", "12").build("identifier")) |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.uri("/path/{id}", builder -> builder.queryParam("q", "12").build("identifier")) |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.url().toString()).isEqualTo("/base/path/identifier?q=12"); |
|
|
|
@ -110,7 +112,7 @@ public class DefaultWebClientTests {
@@ -110,7 +112,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
public void uriBuilderWithPathOverride() { |
|
|
|
|
this.builder.build().get() |
|
|
|
|
.uri(builder -> builder.replacePath("/path").build()) |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.url().toString()).isEqualTo("/path"); |
|
|
|
@ -120,7 +122,7 @@ public class DefaultWebClientTests {
@@ -120,7 +122,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
public void requestHeaderAndCookie() { |
|
|
|
|
this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON) |
|
|
|
|
.cookies(cookies -> cookies.add("id", "123")) // SPR-16178
|
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/json"); |
|
|
|
@ -131,7 +133,7 @@ public class DefaultWebClientTests {
@@ -131,7 +133,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
public void httpRequest() { |
|
|
|
|
this.builder.build().get().uri("/path") |
|
|
|
|
.httpRequest(httpRequest -> {}) |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.httpRequest()).isNotNull(); |
|
|
|
@ -143,7 +145,8 @@ public class DefaultWebClientTests {
@@ -143,7 +145,8 @@ public class DefaultWebClientTests {
|
|
|
|
|
.defaultHeader("Accept", "application/json").defaultCookie("id", "123") |
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
client.get().uri("/path").exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
client.get().uri("/path") |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/json"); |
|
|
|
@ -160,7 +163,7 @@ public class DefaultWebClientTests {
@@ -160,7 +163,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
client.get().uri("/path") |
|
|
|
|
.header("Accept", "application/xml") |
|
|
|
|
.cookie("id", "456") |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/xml"); |
|
|
|
@ -185,7 +188,7 @@ public class DefaultWebClientTests {
@@ -185,7 +188,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
try { |
|
|
|
|
context.set("bar"); |
|
|
|
|
client.get().uri("/path").attribute("foo", "bar") |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
} |
|
|
|
|
finally { |
|
|
|
|
context.remove(); |
|
|
|
@ -271,7 +274,7 @@ public class DefaultWebClientTests {
@@ -271,7 +274,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
this.builder.filter(filter).build() |
|
|
|
|
.get().uri("/path") |
|
|
|
|
.attribute("foo", "bar") |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
assertThat(actual.get("foo")).isEqualTo("bar"); |
|
|
|
|
|
|
|
|
@ -290,7 +293,7 @@ public class DefaultWebClientTests {
@@ -290,7 +293,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
this.builder.filter(filter).build() |
|
|
|
|
.get().uri("/path") |
|
|
|
|
.attribute("foo", null) |
|
|
|
|
.exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
assertThat(actual.get("foo")).isNull(); |
|
|
|
|
|
|
|
|
@ -306,7 +309,7 @@ public class DefaultWebClientTests {
@@ -306,7 +309,7 @@ public class DefaultWebClientTests {
|
|
|
|
|
.defaultCookie("id", "123")) |
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
client.get().uri("/path").exchange().block(Duration.ofSeconds(10)); |
|
|
|
|
client.get().uri("/path").retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10)); |
|
|
|
|
|
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/json"); |
|
|
|
@ -317,8 +320,8 @@ public class DefaultWebClientTests {
@@ -317,8 +320,8 @@ public class DefaultWebClientTests {
|
|
|
|
|
public void switchToErrorOnEmptyClientResponseMono() { |
|
|
|
|
ExchangeFunction exchangeFunction = mock(ExchangeFunction.class); |
|
|
|
|
given(exchangeFunction.exchange(any())).willReturn(Mono.empty()); |
|
|
|
|
WebClient.Builder builder = WebClient.builder().baseUrl("/base").exchangeFunction(exchangeFunction); |
|
|
|
|
StepVerifier.create(builder.build().get().uri("/path").exchange()) |
|
|
|
|
WebClient client = WebClient.builder().baseUrl("/base").exchangeFunction(exchangeFunction).build(); |
|
|
|
|
StepVerifier.create(client.get().uri("/path").retrieve().bodyToMono(Void.class)) |
|
|
|
|
.expectErrorMessage("The underlying HTTP client completed without emitting a response.") |
|
|
|
|
.verify(Duration.ofSeconds(5)); |
|
|
|
|
} |
|
|
|
@ -333,9 +336,11 @@ public class DefaultWebClientTests {
@@ -333,9 +336,11 @@ public class DefaultWebClientTests {
|
|
|
|
|
.build()) |
|
|
|
|
) |
|
|
|
|
.build(); |
|
|
|
|
Mono<ClientResponse> exchange = client.get().uri("/path").exchange(); |
|
|
|
|
|
|
|
|
|
Mono<Void> result = client.get().uri("/path").retrieve().bodyToMono(Void.class); |
|
|
|
|
|
|
|
|
|
verifyNoInteractions(this.exchangeFunction); |
|
|
|
|
exchange.block(Duration.ofSeconds(10)); |
|
|
|
|
result.block(Duration.ofSeconds(10)); |
|
|
|
|
ClientRequest request = verifyAndGetRequest(); |
|
|
|
|
assertThat(request.headers().getFirst("Custom")).isEqualTo("value"); |
|
|
|
|
} |
|
|
|
|