renamed headersSpec to match article

This commit is contained in:
Gerardo Roza 2021-01-26 17:53:48 -03:00
parent 32f3528575
commit 0f668a63b9
1 changed files with 19 additions and 19 deletions

View File

@ -79,11 +79,11 @@ public class WebClientIntegrationTest {
// request body specifications // request body specifications
String bodyValue = "bodyValue"; String bodyValue = "bodyValue";
RequestHeadersSpec<?> headerSpecPost1 = bodySpecPost.body(BodyInserters.fromPublisher(Mono.just(bodyValue), String.class)); RequestHeadersSpec<?> headersSpecPost1 = bodySpecPost.body(BodyInserters.fromPublisher(Mono.just(bodyValue), String.class));
RequestHeadersSpec<?> headerSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(bodyValue)); RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(bodyValue));
RequestHeadersSpec<?> headerSpecPost3 = createDefaultPostResourceRequest().bodyValue(bodyValue); RequestHeadersSpec<?> headersSpecPost3 = createDefaultPostResourceRequest().bodyValue(bodyValue);
RequestHeadersSpec<?> headerSpecFooPost = fooBodySpecPost.body(Mono.just(new Foo("fooName")), Foo.class); RequestHeadersSpec<?> headersSpecFooPost = fooBodySpecPost.body(Mono.just(new Foo("fooName")), Foo.class);
RequestHeadersSpec<?> headerSpecGet = requestGet.uri("/resource"); RequestHeadersSpec<?> headersSpecGet = requestGet.uri("/resource");
// request body specifications - using inserters // request body specifications - using inserters
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
@ -94,28 +94,28 @@ public class WebClientIntegrationTest {
BodyInserter<Object, ReactiveHttpOutputMessage> inserterObject = BodyInserters.fromValue(new Object()); BodyInserter<Object, ReactiveHttpOutputMessage> inserterObject = BodyInserters.fromValue(new Object());
BodyInserter<String, ReactiveHttpOutputMessage> inserterString = BodyInserters.fromValue(bodyValue); BodyInserter<String, ReactiveHttpOutputMessage> inserterString = BodyInserters.fromValue(bodyValue);
RequestHeadersSpec<?> headerSpecInserterMultipart = bodySpecPostMultipart.body(inserterMultipart); RequestHeadersSpec<?> headersSpecInserterMultipart = bodySpecPostMultipart.body(inserterMultipart);
RequestHeadersSpec<?> headerSpecInserterObject = createDefaultPostResourceRequest().body(inserterObject); RequestHeadersSpec<?> headersSpecInserterObject = createDefaultPostResourceRequest().body(inserterObject);
RequestHeadersSpec<?> headerSpecInserterString = createDefaultPostResourceRequest().body(inserterString); RequestHeadersSpec<?> headersSpecInserterString = createDefaultPostResourceRequest().body(inserterString);
// request header specification // request header specification
RequestHeadersSpec<?> headerSpecInserterStringWithHeaders = headerSpecInserterString.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = headersSpecInserterString.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(StandardCharsets.UTF_8) .acceptCharset(StandardCharsets.UTF_8)
.ifNoneMatch("*") .ifNoneMatch("*")
.ifModifiedSince(ZonedDateTime.now()); .ifModifiedSince(ZonedDateTime.now());
// request // request
ResponseSpec responseSpecPostString = headerSpecInserterStringWithHeaders.retrieve(); ResponseSpec responseSpecPostString = headersSpecInserterStringWithHeaders.retrieve();
Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class); Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
Mono<String> responsePostMultipart = headerSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) Mono<String> responsePostMultipart = headersSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.retrieve() .retrieve()
.bodyToMono(String.class); .bodyToMono(String.class);
Mono<String> responsePostWithBody1 = headerSpecPost1.retrieve() Mono<String> responsePostWithBody1 = headersSpecPost1.retrieve()
.bodyToMono(String.class); .bodyToMono(String.class);
Mono<String> responsePostWithBody2 = headerSpecPost2.retrieve() Mono<String> responsePostWithBody2 = headersSpecPost2.retrieve()
.bodyToMono(String.class); .bodyToMono(String.class);
Mono<String> responsePostWithBody3 = headerSpecPost3.exchangeToMono(response -> { Mono<String> responsePostWithBody3 = headersSpecPost3.exchangeToMono(response -> {
if (response.statusCode() if (response.statusCode()
.equals(HttpStatus.OK)) { .equals(HttpStatus.OK)) {
return response.bodyToMono(String.class); return response.bodyToMono(String.class);
@ -127,11 +127,11 @@ public class WebClientIntegrationTest {
.flatMap(Mono::error); .flatMap(Mono::error);
} }
}); });
Mono<String> responsePostFoo = headerSpecFooPost.retrieve() Mono<String> responsePostFoo = headersSpecFooPost.retrieve()
.bodyToMono(String.class); .bodyToMono(String.class);
ParameterizedTypeReference<Map<String, String>> ref = new ParameterizedTypeReference<Map<String, String>>() { ParameterizedTypeReference<Map<String, String>> ref = new ParameterizedTypeReference<Map<String, String>>() {
}; };
Mono<Map<String, String>> responseGet = headerSpecGet.retrieve() Mono<Map<String, String>> responseGet = headersSpecGet.retrieve()
.bodyToMono(ref); .bodyToMono(ref);
Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> { Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST); assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
@ -173,7 +173,7 @@ public class WebClientIntegrationTest {
.contains("Connection refused")) .contains("Connection refused"))
.verify(); .verify();
// assert error plain `new Object()` as request body // assert error plain `new Object()` as request body
StepVerifier.create(headerSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class))) StepVerifier.create(headersSpecInserterObject.exchangeToMono(response -> response.bodyToMono(String.class)))
.expectError(CodecException.class) .expectError(CodecException.class)
.verify(); .verify();
} }
@ -191,11 +191,11 @@ public class WebClientIntegrationTest {
.build(); .build();
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class); BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class);
RequestHeadersSpec<?> headerSpecInserterCompleteSuscriber = timeoutClient.post() RequestHeadersSpec<?> headersSpecInserterCompleteSuscriber = timeoutClient.post()
.uri("/resource") .uri("/resource")
.body(inserterCompleteSuscriber); .body(inserterCompleteSuscriber);
StepVerifier.create(headerSpecInserterCompleteSuscriber.retrieve() StepVerifier.create(headersSpecInserterCompleteSuscriber.retrieve()
.bodyToMono(String.class)) .bodyToMono(String.class))
.expectTimeout(Duration.ofMillis(2000)) .expectTimeout(Duration.ofMillis(2000))
.verify(); .verify();