diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index ddd36837e1..fde5f96f69 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -19,13 +19,13 @@ package org.springframework.web.reactive.function.client; import java.time.Duration; import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; import org.hamcrest.Matchers; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Flux; @@ -39,7 +39,9 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.codec.Pojo; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; /** * Integration tests using a {@link ExchangeFunction} through {@link WebClient}. @@ -56,8 +58,7 @@ public class WebClientIntegrationTests { @Before public void setup() { this.server = new MockWebServer(); - String baseUrl = this.server.url("/").toString(); - this.webClient = WebClient.create(baseUrl); + this.webClient = WebClient.create(this.server.url("/").toString()); } @After @@ -65,17 +66,12 @@ public class WebClientIntegrationTests { this.server.shutdown(); } - @Test - public void headers() throws Exception { - this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); + public void shouldReceiveResponseHeaders() throws Exception { + prepareResponse(response -> response + .setHeader("Content-Type", "text/plain") + .setBody("Hello Spring!")); - this.webClient.get().uri("/test") - .exchange() - .map(response -> { - response.close(); - return response.statusCode(); - }); Mono result = this.webClient.get() .uri("/greeting?name=Spring") .exchange() @@ -87,18 +83,18 @@ public class WebClientIntegrationTests { assertEquals(MediaType.TEXT_PLAIN, httpHeaders.getContentType()); assertEquals(13L, httpHeaders.getContentLength()); }) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void plainText() throws Exception { - this.server.enqueue(new MockResponse().setBody("Hello Spring!")); + public void shouldReceivePlainText() throws Exception { + prepareResponse(response -> response.setBody("Hello Spring!")); Mono result = this.webClient.get() .uri("/greeting?name=Spring") @@ -108,94 +104,69 @@ public class WebClientIntegrationTests { StepVerifier.create(result) .expectNext("Hello Spring!") - .expectComplete() - .verify(Duration.ofSeconds(3)); - - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("testvalue", recordedRequest.getHeader("X-Test-Header")); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); - } - - @Test - public void jsonString() throws Exception { - String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); - - Mono result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) - .exchange() - .flatMap(response -> response.bodyToMono(String.class)); - - StepVerifier.create(result) - .expectNext(content) - .expectComplete() - .verify(Duration.ofSeconds(3)); - - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + .expectComplete().verify(Duration.ofSeconds(3)); + + expectRequestCount(1); + expectRequest(request -> { + assertEquals("testvalue", request.getHeader("X-Test-Header")); + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void jsonStringRetrieveMono() throws Exception { + public void shouldReceiveJsonAsString() throws Exception { String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); + prepareResponse(response -> response + .setHeader("Content-Type", "application/json").setBody(content)); Mono result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) + .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(String.class); StepVerifier.create(result) .expectNext(content) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonStringRetrieveMonoTypeReference() throws Exception { + public void shouldReceiveJsonAsTypeReferenceString() throws Exception { String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); + prepareResponse(response -> response + .setHeader("Content-Type", "application/json").setBody(content)); Mono result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) + .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() - .bodyToMono(new ParameterizedTypeReference() {}); + .bodyToMono(new ParameterizedTypeReference() { + }); StepVerifier.create(result) .expectNext(content) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonStringExchangeEntity() throws Exception { + public void shouldReceiveJsonAsResponseEntityString() throws Exception { String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); + prepareResponse(response -> response + .setHeader("Content-Type", "application/json").setBody(content)); Mono> result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) + .uri("/json").accept(MediaType.APPLICATION_JSON) .exchange() .flatMap(response -> response.toEntity(String.class)); @@ -206,23 +177,23 @@ public class WebClientIntegrationTests { assertEquals(31, entity.getHeaders().getContentLength()); assertEquals(content, entity.getBody()); }) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonStringExchangeEntityList() throws Exception { + public void shouldReceiveJsonAsResponseEntityList() throws Exception { String content = "[{\"bar\":\"bar1\",\"foo\":\"foo1\"}, {\"bar\":\"bar2\",\"foo\":\"foo2\"}]"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody(content)); + prepareResponse(response -> response + .setHeader("Content-Type", "application/json").setBody(content)); Mono>> result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) + .uri("/json").accept(MediaType.APPLICATION_JSON) .exchange() .flatMap(response -> response.toEntityList(Pojo.class)); @@ -235,64 +206,41 @@ public class WebClientIntegrationTests { Pojo pojo2 = new Pojo("foo2", "bar2"); assertEquals(Arrays.asList(pojo1, pojo2), entity.getBody()); }) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonStringRetrieveFlux() throws Exception { + public void shouldReceiveJsonAsFluxString() throws Exception { String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); + prepareResponse(response -> response + .setHeader("Content-Type", "application/json").setBody(content)); Flux result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) + .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToFlux(String.class); StepVerifier.create(result) .expectNext(content) - .expectComplete() - .verify(Duration.ofSeconds(3)); - - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - } - - @Test - public void jsonStringRetrieveFluxTypeReference() throws Exception { - String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") - .setBody(content)); - - Flux result = this.webClient.get() - .uri("/json") - .accept(MediaType.APPLICATION_JSON) - .retrieve() - .bodyToFlux(new ParameterizedTypeReference() {}); - - StepVerifier.create(result) - .expectNext(content) - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/json", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/json", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonPojoMono() throws Exception { - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") + public void shouldReceiveJsonAsPojo() throws Exception { + prepareResponse(response -> response + .setHeader("Content-Type", "application/json") .setBody("{\"bar\":\"barbar\",\"foo\":\"foofoo\"}")); Mono result = this.webClient.get() @@ -306,15 +254,17 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/pojo", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/pojo", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void jsonPojoFlux() throws Exception { - this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json") + public void shouldReceiveJsonAsFluxPojo() throws Exception { + prepareResponse(response -> response + .setHeader("Content-Type", "application/json") .setBody("[{\"bar\":\"bar1\",\"foo\":\"foo1\"},{\"bar\":\"bar2\",\"foo\":\"foo2\"}]")); Flux result = this.webClient.get() @@ -329,16 +279,16 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/pojos", recordedRequest.getPath()); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/pojos", request.getPath()); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + }); } @Test - public void postJsonPojo() throws Exception { - this.server.enqueue(new MockResponse() - .setHeader("Content-Type", "application/json") + public void shouldSendPojoAsJson() throws Exception { + prepareResponse(response -> response.setHeader("Content-Type", "application/json") .setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}")); Mono result = this.webClient.post() @@ -354,18 +304,19 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/pojo/capitalize", recordedRequest.getPath()); - Assert.assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", recordedRequest.getBody().readUtf8()); - Assert.assertEquals("chunked", recordedRequest.getHeader(HttpHeaders.TRANSFER_ENCODING)); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/pojo/capitalize", request.getPath()); + assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", request.getBody().readUtf8()); + assertEquals("chunked", request.getHeader(HttpHeaders.TRANSFER_ENCODING)); + assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("application/json", request.getHeader(HttpHeaders.CONTENT_TYPE)); + }); } @Test - public void cookies() throws Exception { - this.server.enqueue(new MockResponse() + public void shouldSendCookies() throws Exception { + prepareResponse(response -> response .setHeader("Content-Type", "text/plain").setBody("test")); Mono result = this.webClient.get() @@ -379,15 +330,16 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("/test", recordedRequest.getPath()); - Assert.assertEquals("testkey=testvalue", recordedRequest.getHeader(HttpHeaders.COOKIE)); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("/test", request.getPath()); + assertEquals("testkey=testvalue", request.getHeader(HttpHeaders.COOKIE)); + }); } @Test - public void exchangeNotFound() throws Exception { - this.server.enqueue(new MockResponse().setResponseCode(404) + public void shouldReceive404Response() throws Exception { + prepareResponse(response -> response.setResponseCode(404) .setHeader("Content-Type", "text/plain").setBody("Not Found")); Mono result = this.webClient.get().uri("/greeting?name=Spring").exchange(); @@ -397,15 +349,16 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void retrieveBodyToMonoNotFound() throws Exception { - this.server.enqueue(new MockResponse().setResponseCode(404) + public void shouldGetErrorSignalOn404() throws Exception { + prepareResponse(response -> response.setResponseCode(404) .setHeader("Content-Type", "text/plain").setBody("Not Found")); Mono result = this.webClient.get() @@ -417,16 +370,17 @@ public class WebClientIntegrationTests { .expectError(WebClientException.class) .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void retrieveBodyToMonoInternalServerError() throws Exception { + public void shouldGetInternalServerErrorSignal() throws Exception { String errorMessage = "Internal Server error"; - this.server.enqueue(new MockResponse().setResponseCode(500) + prepareResponse(response -> response.setResponseCode(500) .setHeader("Content-Type", "text/plain").setBody(errorMessage)); Mono result = this.webClient.get() @@ -438,22 +392,22 @@ public class WebClientIntegrationTests { .expectErrorSatisfies(throwable -> { assertTrue(throwable instanceof WebClientResponseException); WebClientResponseException ex = (WebClientResponseException) throwable; - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, ex.getHeaders().getContentType()); assertEquals(errorMessage, ex.getResponseBodyAsString()); }) .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void retrieveBodyToCustomStatusHandler() throws Exception { - this.server.enqueue(new MockResponse().setResponseCode(500) + public void shouldApplyCustomStatusHandler() throws Exception { + prepareResponse(response -> response.setResponseCode(500) .setHeader("Content-Type", "text/plain").setBody("Internal Server error")); Mono result = this.webClient.get() @@ -466,15 +420,16 @@ public class WebClientIntegrationTests { .expectError(MyException.class) .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void exchangeToEntityNotFound() throws Exception { - this.server.enqueue(new MockResponse().setResponseCode(404) + public void shouldReceiveNotFoundEntity() throws Exception { + prepareResponse(response -> response.setResponseCode(404) .setHeader("Content-Type", "text/plain").setBody("Not Found")); Mono> result = this.webClient.get() @@ -487,15 +442,16 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT)); - Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath()); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); } @Test - public void filter() throws Exception { - this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain") + public void shouldApplyExchangeFilter() throws Exception { + prepareResponse(response -> response.setHeader("Content-Type", "text/plain") .setBody("Hello Spring!")); WebClient filteredClient = this.webClient.mutate() @@ -516,29 +472,29 @@ public class WebClientIntegrationTests { .expectComplete() .verify(Duration.ofSeconds(3)); - RecordedRequest recordedRequest = server.takeRequest(); - Assert.assertEquals(1, server.getRequestCount()); - Assert.assertEquals("bar", recordedRequest.getHeader("foo")); + expectRequestCount(1); + expectRequest(request -> { + assertEquals("bar", request.getHeader("foo")); + }); } @Test - public void errorHandlingFilter() throws Exception { + public void shouldApplyErrorHandlingFilter() throws Exception { ExchangeFilterFunction filter = ExchangeFilterFunction.ofResponseProcessor( - clientResponse -> { - List headerValues = clientResponse.headers().header("Foo"); - return headerValues.isEmpty() ? Mono.error( - new MyException("Response does not contain Foo header")) : - Mono.just(clientResponse); - } - ); + clientResponse -> { + List headerValues = clientResponse.headers().header("Foo"); + return headerValues.isEmpty() ? Mono.error( + new MyException("Response does not contain Foo header")) : + Mono.just(clientResponse); + } + ); - WebClient filteredClient = this.webClient.mutate() - .filter(filter) - .build(); + WebClient filteredClient = this.webClient.mutate().filter(filter).build(); // header not present - this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); + prepareResponse(response -> response + .setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); Mono result = filteredClient.get() .uri("/greeting?name=Spring") @@ -546,31 +502,28 @@ public class WebClientIntegrationTests { .bodyToMono(String.class); StepVerifier.create(result) - .expectError(MyException.class) - .verify(Duration.ofSeconds(3)); + .expectError(MyException.class).verify(Duration.ofSeconds(3)); // header present - this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain") + prepareResponse(response -> response.setHeader("Content-Type", "text/plain") .setHeader("Foo", "Bar") .setBody("Hello Spring!")); result = filteredClient.get() .uri("/greeting?name=Spring") - .retrieve() - .bodyToMono(String.class); + .retrieve().bodyToMono(String.class); StepVerifier.create(result) .expectNext("Hello Spring!") - .expectComplete() - .verify(Duration.ofSeconds(3)); + .expectComplete().verify(Duration.ofSeconds(3)); - Assert.assertEquals(2, server.getRequestCount()); + expectRequestCount(2); } @Test - public void exchangeNoContent() throws Exception { - this.server.enqueue(new MockResponse().setHeader("Content-Length", "0")); + public void shouldReceiveEmptyResponse() throws Exception { + prepareResponse(response -> response.setHeader("Content-Length", "0")); Mono result = this.webClient.get() .uri("/noContent") @@ -583,7 +536,7 @@ public class WebClientIntegrationTests { } @Test // SPR-15782 - public void absoluteUri() throws Exception { + public void shouldFailWithRelativeUrls() throws Exception { String uri = "/api/v4/groups/1"; Mono responseMono = WebClient.builder().build().get().uri(uri).exchange(); @@ -592,6 +545,20 @@ public class WebClientIntegrationTests { .verify(Duration.ofSeconds(5)); } + private void prepareResponse(Consumer consumer) { + MockResponse response = new MockResponse(); + consumer.accept(response); + this.server.enqueue(response); + } + + private void expectRequest(Consumer consumer) throws InterruptedException { + consumer.accept(this.server.takeRequest()); + } + + private void expectRequestCount(int count) { + assertEquals(count, this.server.getRequestCount()); + } + @SuppressWarnings("serial") private static class MyException extends RuntimeException {