[JAVA-12608] Fix WebClient integration tests (#12597)
This commit is contained in:
parent
f242abf1a1
commit
c8bb6d7603
|
@ -29,6 +29,11 @@ public class WebClientController {
|
||||||
return bodyString.map(body -> "processed-" + body);
|
return bodyString.map(body -> "processed-" + body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/resource-override")
|
||||||
|
public Mono<String> postStringResourceOverride(@RequestBody Mono<String> bodyString) {
|
||||||
|
return bodyString.map(body -> "override-processed-" + body);
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/resource-foo")
|
@PostMapping("/resource-foo")
|
||||||
public Mono<String> postFooResource(@RequestBody Mono<Foo> bodyFoo) {
|
public Mono<String> postFooResource(@RequestBody Mono<Foo> bodyFoo) {
|
||||||
return bodyFoo.map(foo -> "processedFoo-" + foo.getName());
|
return bodyFoo.map(foo -> "processedFoo-" + foo.getName());
|
||||||
|
|
|
@ -4,7 +4,6 @@ import io.netty.channel.ChannelOption;
|
||||||
import io.netty.handler.timeout.ReadTimeoutException;
|
import io.netty.handler.timeout.ReadTimeoutException;
|
||||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
@ -44,7 +43,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
@Slf4j
|
|
||||||
class WebClientIntegrationTest {
|
class WebClientIntegrationTest {
|
||||||
|
|
||||||
private static final String BODY_VALUE = "bodyValue";
|
private static final String BODY_VALUE = "bodyValue";
|
||||||
|
@ -56,11 +54,18 @@ class WebClientIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
|
||||||
log.info("Executing test using several webclient configs ....");
|
|
||||||
// WebClient creation
|
|
||||||
WebClient client1 = WebClient.builder().clientConnector(httpConnector()).build();
|
WebClient client1 = WebClient.builder().clientConnector(httpConnector()).build();
|
||||||
|
StepVerifier.create(retrieveResponse(client1.post()
|
||||||
|
.uri("http://localhost:" + port + "/resource")))
|
||||||
|
.expectNext("processed-bodyValue")
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
WebClient client2 = WebClient.builder().baseUrl("http://localhost:" + port)
|
WebClient client2 = WebClient.builder().baseUrl("http://localhost:" + port)
|
||||||
.clientConnector(httpConnector()).build();
|
.clientConnector(httpConnector()).build();
|
||||||
|
StepVerifier.create(retrieveResponse(client2))
|
||||||
|
.expectNext("processed-bodyValue")
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
WebClient client3 = WebClient.builder()
|
WebClient client3 = WebClient.builder()
|
||||||
.baseUrl("http://localhost:" + port)
|
.baseUrl("http://localhost:" + port)
|
||||||
.clientConnector(httpConnector())
|
.clientConnector(httpConnector())
|
||||||
|
@ -68,58 +73,25 @@ class WebClientIntegrationTest {
|
||||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||||
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
|
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// response assertions
|
|
||||||
StepVerifier.create(retrieveResponse(client1.post()
|
|
||||||
.uri("http://localhost:" + port + "/resource")))
|
|
||||||
.expectNext("processed-bodyValue")
|
|
||||||
.verifyComplete();
|
|
||||||
StepVerifier.create(retrieveResponse(client2))
|
|
||||||
.expectNext("processed-bodyValue")
|
|
||||||
.verifyComplete();
|
|
||||||
StepVerifier.create(retrieveResponse(client3))
|
StepVerifier.create(retrieveResponse(client3))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
// assert response without specifying URI
|
|
||||||
log.info("Obtaining error response using default client (without url) ....");
|
|
||||||
StepVerifier.create(retrieveResponse(client1))
|
|
||||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
|
||||||
.contains("Connection refused"))
|
|
||||||
.log()
|
|
||||||
.verify();
|
|
||||||
log.info("error response processed as expected");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void givenWebClientCreationWithoutUri_whenUsed_thenObtainExpectedResponse() {
|
|
||||||
log.info("Executing test for connection refused....");
|
|
||||||
WebClient client = WebClient.builder()
|
|
||||||
.clientConnector(httpConnector())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
StepVerifier.create(retrieveResponse(client))
|
|
||||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
|
||||||
.contains("Connection refused"))
|
|
||||||
.log()
|
|
||||||
.verify();
|
|
||||||
log.info("test completed....");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
// request specification
|
|
||||||
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
|
|
||||||
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
|
|
||||||
RequestHeadersUriSpec<?> requestGet = createDefaultClient().get();
|
|
||||||
|
|
||||||
// response assertions
|
RequestBodyUriSpec uriSpecPost1 = createDefaultClient().method(HttpMethod.POST);
|
||||||
StepVerifier.create(retrieveResponse(uriSpecPost1))
|
StepVerifier.create(retrieveResponse(uriSpecPost1))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
|
RequestBodyUriSpec uriSpecPost2 = createDefaultClient().post();
|
||||||
StepVerifier.create(retrieveResponse(uriSpecPost2))
|
StepVerifier.create(retrieveResponse(uriSpecPost2))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
|
RequestHeadersUriSpec<?> requestGet = createDefaultClient().get();
|
||||||
StepVerifier.create(retrieveGetResponse(requestGet))
|
StepVerifier.create(retrieveGetResponse(requestGet))
|
||||||
.expectNextMatches(nextMap -> nextMap.get("field")
|
.expectNextMatches(nextMap -> nextMap.get("field")
|
||||||
.equals("value"))
|
.equals("value"))
|
||||||
|
@ -128,21 +100,21 @@ class WebClientIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
// uri specification
|
|
||||||
RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
|
|
||||||
RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(
|
|
||||||
uriBuilder -> uriBuilder.pathSegment("resource")
|
|
||||||
.build());
|
|
||||||
RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(
|
|
||||||
URI.create("http://localhost:" + port + "/resource"));
|
|
||||||
|
|
||||||
// response assertions
|
RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource");
|
||||||
StepVerifier.create(retrieveResponse(bodySpecUsingString))
|
StepVerifier.create(retrieveResponse(bodySpecUsingString))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
|
RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(
|
||||||
|
uriBuilder -> uriBuilder.pathSegment("resource")
|
||||||
|
.build());
|
||||||
StepVerifier.create(retrieveResponse(bodySpecUsingUriBuilder))
|
StepVerifier.create(retrieveResponse(bodySpecUsingUriBuilder))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
|
RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(
|
||||||
|
URI.create("http://localhost:" + port + "/resource"));
|
||||||
StepVerifier.create(retrieveResponse(bodySpecusingURI))
|
StepVerifier.create(retrieveResponse(bodySpecusingURI))
|
||||||
.expectNext("processed-bodyValue")
|
.expectNext("processed-bodyValue")
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
@ -150,24 +122,23 @@ class WebClientIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() {
|
||||||
RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
|
RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest()
|
||||||
|
.uri(URI.create("http://localhost:" + port + "/resource-override"));
|
||||||
|
|
||||||
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri))
|
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri))
|
||||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
.expectNext("override-processed-bodyValue")
|
||||||
.contains("Connection refused"))
|
.verifyComplete();
|
||||||
.verify();
|
|
||||||
|
|
||||||
RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder()
|
RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder()
|
||||||
.clientConnector(httpConnector())
|
.clientConnector(httpConnector())
|
||||||
.baseUrl("http://localhost:" + port)
|
.baseUrl("http://localhost:" + port)
|
||||||
.build()
|
.build()
|
||||||
.post()
|
.post()
|
||||||
.uri(URI.create("/resource"));
|
.uri(URI.create("http://localhost:" + port + "/resource-override"));
|
||||||
|
|
||||||
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2))
|
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2))
|
||||||
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
|
.expectNext("override-processed-bodyValue")
|
||||||
.contains("Connection refused"))
|
.verifyComplete();
|
||||||
.verify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -212,7 +183,7 @@ class WebClientIntegrationTest {
|
||||||
StepVerifier.create(retrieveResponse(headersSpecPlainObject))
|
StepVerifier.create(retrieveResponse(headersSpecPlainObject))
|
||||||
.expectError(CodecException.class)
|
.expectError(CodecException.class)
|
||||||
.verify();
|
.verify();
|
||||||
// assert response for request with no body
|
// assert response for request without body
|
||||||
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(
|
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(
|
||||||
responseHandler -> {
|
responseHandler -> {
|
||||||
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||||
|
@ -342,35 +313,30 @@ class WebClientIntegrationTest {
|
||||||
.uri("/resource")
|
.uri("/resource")
|
||||||
.bodyValue(BODY_VALUE)
|
.bodyValue(BODY_VALUE)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class);
|
||||||
.log();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<String> retrieveResponse(RequestBodyUriSpec spec) {
|
private Mono<String> retrieveResponse(RequestBodyUriSpec spec) {
|
||||||
return spec.uri("/resource")
|
return spec.uri("/resource")
|
||||||
.bodyValue(BODY_VALUE)
|
.bodyValue(BODY_VALUE)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class);
|
||||||
.log();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) {
|
private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) {
|
||||||
return spec.uri("/resource")
|
return spec.uri("/resource")
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(MAP_RESPONSE_REF)
|
.bodyToMono(MAP_RESPONSE_REF);
|
||||||
.log();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<String> retrieveResponse(RequestBodySpec spec) {
|
private Mono<String> retrieveResponse(RequestBodySpec spec) {
|
||||||
return spec.bodyValue(BODY_VALUE)
|
return spec.bodyValue(BODY_VALUE)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class);
|
||||||
.log();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) {
|
private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) {
|
||||||
return spec.retrieve()
|
return spec.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class);
|
||||||
.log();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<appender-ref ref="LISTAPPENDER" />
|
<appender-ref ref="LISTAPPENDER" />
|
||||||
</logger>
|
</logger>
|
||||||
|
|
||||||
<logger name="reactor.netty.http.client" level="DEBUG"/>
|
<logger name="reactor.netty.http.client" level="INFO"/>
|
||||||
|
|
||||||
<root level="info">
|
<root level="info">
|
||||||
<appender-ref ref="CONSOLE" />
|
<appender-ref ref="CONSOLE" />
|
||||||
|
|
Loading…
Reference in New Issue