fixed rest of tests

This commit is contained in:
Gerardo Roza 2021-01-25 11:37:12 -03:00
parent 84737e1056
commit cfe47f772a
3 changed files with 25 additions and 13 deletions

View File

@ -4,6 +4,10 @@ public class Foo {
private String name; private String name;
public Foo() {
super();
}
public Foo(String name) { public Foo(String name) {
super(); super();
this.name = name; this.name = name;

View File

@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController @RestController
public class WebClientController { public class WebClientController {
@ -24,13 +26,13 @@ public class WebClientController {
} }
@PostMapping("/resource") @PostMapping("/resource")
public String postResource(@RequestBody String bodyString) { public Mono<String> postStringResource(@RequestBody Mono<String> bodyString) {
return "processed-" + bodyString; return bodyString.map(body -> "processed-" + body);
} }
@PostMapping("/resource-foo") @PostMapping("/resource-foo")
public String postResource(@RequestBody Foo bodyFoo) { public Mono<String> postFooResource(@RequestBody Mono<Foo> bodyFoo) {
return "processedFoo-" + bodyFoo.getName(); return bodyFoo.map(foo -> "processedFoo-" + foo.getName());
} }
@PostMapping(value = "/resource-multipart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/resource-multipart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

View File

@ -17,8 +17,10 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.CodecException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.client.reactive.ClientHttpRequest;
@ -72,8 +74,8 @@ public class WebClientIntegrationTest {
RequestBodySpec bodySpecPost = uriSpecPost1.uri("http://localhost:" + port + "/resource"); RequestBodySpec bodySpecPost = uriSpecPost1.uri("http://localhost:" + port + "/resource");
RequestBodySpec bodySpecPostMultipart = uriSpecPost2.uri(uriBuilder -> uriBuilder.pathSegment("resource-multipart") RequestBodySpec bodySpecPostMultipart = uriSpecPost2.uri(uriBuilder -> uriBuilder.pathSegment("resource-multipart")
.build()); .build());
RequestBodySpec fooBodySpecPost = createDefaultPostRequest().uri("/resource-foo");
RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource")); RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
RequestBodySpec fooBodySpecPost = createDefaultPostRequest().uri(URI.create("/resource-foo"));
// request body specifications // request body specifications
String bodyValue = "bodyValue"; String bodyValue = "bodyValue";
@ -124,23 +126,27 @@ public class WebClientIntegrationTest {
Map<String, String> responseGet = headerSpecGet.retrieve() Map<String, String> responseGet = headerSpecGet.retrieve()
.bodyToMono(ref) .bodyToMono(ref)
.block(); .block();
String responsePostWithNoBody = bodySpecPost.retrieve() Map<String, String> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
.bodyToMono(String.class) assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
return responseHandler.bodyToMono(ref);
})
.block(); .block();
// response assertions // response assertions
assertThat(responsePostString).isEqualTo("processed-bodyValue"); assertThat(responsePostString).isEqualTo("processed-bodyValue");
assertThat(responsePostMultipart).isEqualTo("processed-multipartValue1-multipartValue2"); assertThat(responsePostMultipart).isEqualTo("processed-multipartValue1-multipartValue2");
assertThat(responsePostWithBody1).isEqualTo("processed-"); assertThat(responsePostWithBody1).isEqualTo("processed-bodyValue");
assertThat(responsePostWithBody3).isEqualTo("processed-"); assertThat(responsePostWithBody3).isEqualTo("processed-bodyValue");
assertThat(responseGet).containsEntry("field", "value"); assertThat(responseGet).containsEntry("field", "value");
assertThat(responsePostWithNoBody).isEqualTo("processed-"); assertThat(responsePostFoo).isEqualTo("processedFoo-fooName");
assertThat(responsePostFoo).isEqualTo("processed-fooName"); assertThat(responsePostWithNoBody).containsEntry("error", "Bad Request");
assertThrows(WebClientRequestException.class, () -> { // assert sending plain `new Object()` as request body
String responsePostObject = headerSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class)) assertThrows(CodecException.class, () -> {
headerSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class))
.block(); .block();
}); });
// assert sending request overriding base uri
assertThrows(WebClientRequestException.class, () -> { assertThrows(WebClientRequestException.class, () -> {
bodySpecOverridenBaseUri.retrieve() bodySpecOverridenBaseUri.retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)