From cfe47f772a434eb3b1c462144c9a2301e3418cf4 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Mon, 25 Jan 2021 11:37:12 -0300 Subject: [PATCH] fixed rest of tests --- .../com/baeldung/web/reactive/client/Foo.java | 4 ++++ .../reactive/client/WebClientController.java | 10 ++++---- .../web/client/WebClientIntegrationTest.java | 24 ++++++++++++------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/Foo.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/Foo.java index 1fc4834cfa..c6e3678832 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/Foo.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/Foo.java @@ -4,6 +4,10 @@ public class Foo { private String name; + public Foo() { + super(); + } + public Foo(String name) { super(); this.name = name; diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 4c7941ac35..1a91001807 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + @RestController public class WebClientController { @@ -24,13 +26,13 @@ public class WebClientController { } @PostMapping("/resource") - public String postResource(@RequestBody String bodyString) { - return "processed-" + bodyString; + public Mono postStringResource(@RequestBody Mono bodyString) { + return bodyString.map(body -> "processed-" + body); } @PostMapping("/resource-foo") - public String postResource(@RequestBody Foo bodyFoo) { - return "processedFoo-" + bodyFoo.getName(); + public Mono postFooResource(@RequestBody Mono bodyFoo) { + return bodyFoo.map(foo -> "processedFoo-" + foo.getName()); } @PostMapping(value = "/resource-multipart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) diff --git a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java index b49aff9575..04a4031c62 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java @@ -17,8 +17,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.core.codec.CodecException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.client.reactive.ClientHttpRequest; @@ -72,8 +74,8 @@ public class WebClientIntegrationTest { RequestBodySpec bodySpecPost = uriSpecPost1.uri("http://localhost:" + port + "/resource"); RequestBodySpec bodySpecPostMultipart = uriSpecPost2.uri(uriBuilder -> uriBuilder.pathSegment("resource-multipart") .build()); + RequestBodySpec fooBodySpecPost = createDefaultPostRequest().uri("/resource-foo"); RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource")); - RequestBodySpec fooBodySpecPost = createDefaultPostRequest().uri(URI.create("/resource-foo")); // request body specifications String bodyValue = "bodyValue"; @@ -124,23 +126,27 @@ public class WebClientIntegrationTest { Map responseGet = headerSpecGet.retrieve() .bodyToMono(ref) .block(); - String responsePostWithNoBody = bodySpecPost.retrieve() - .bodyToMono(String.class) + Map responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> { + assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + return responseHandler.bodyToMono(ref); + }) .block(); // response assertions assertThat(responsePostString).isEqualTo("processed-bodyValue"); assertThat(responsePostMultipart).isEqualTo("processed-multipartValue1-multipartValue2"); - assertThat(responsePostWithBody1).isEqualTo("processed-"); - assertThat(responsePostWithBody3).isEqualTo("processed-"); + assertThat(responsePostWithBody1).isEqualTo("processed-bodyValue"); + assertThat(responsePostWithBody3).isEqualTo("processed-bodyValue"); assertThat(responseGet).containsEntry("field", "value"); - assertThat(responsePostWithNoBody).isEqualTo("processed-"); - assertThat(responsePostFoo).isEqualTo("processed-fooName"); + assertThat(responsePostFoo).isEqualTo("processedFoo-fooName"); + assertThat(responsePostWithNoBody).containsEntry("error", "Bad Request"); - assertThrows(WebClientRequestException.class, () -> { - String responsePostObject = headerSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class)) + // assert sending plain `new Object()` as request body + assertThrows(CodecException.class, () -> { + headerSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class)) .block(); }); + // assert sending request overriding base uri assertThrows(WebClientRequestException.class, () -> { bodySpecOverridenBaseUri.retrieve() .bodyToMono(String.class)