Skip to content

Commit

Permalink
added and ordered tests in WebClientIntegrationTest to match article …
Browse files Browse the repository at this point in the history
…- test not passing (reusing specs)
  • Loading branch information
rozagerardo committed Jan 24, 2021
1 parent 1884aba commit 2359c53
Showing 1 changed file with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.baeldung.web.client;

import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
Expand All @@ -10,6 +10,7 @@
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
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.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -22,6 +23,10 @@
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
import org.springframework.web.reactive.function.client.WebClient.UriSpec;

import com.baeldung.web.reactive.client.WebClientApplication;

Expand All @@ -31,7 +36,7 @@
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

@SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class WebClientIntegrationTest {

@LocalServerPort
Expand All @@ -40,44 +45,73 @@ public class WebClientIntegrationTest {
@Test
public void demonstrateWebClient() {
// request
WebClient.UriSpec<WebClient.RequestBodySpec> request1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST);
WebClient.UriSpec<WebClient.RequestBodySpec> request2 = createWebClientWithServerURLAndDefaultValues().post();
UriSpec<WebClient.RequestBodySpec> requestPost1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST);
UriSpec<WebClient.RequestBodySpec> requestPost2 = createWebClientWithServerURLAndDefaultValues().post();
UriSpec<?> requestGet = createWebClientWithServerURLAndDefaultValues().get();

// request body specifications
WebClient.RequestBodySpec uri1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST)
.uri("/resource");
WebClient.RequestBodySpec uri2 = createWebClientWithServerURLAndDefaultValues().post()
.uri(URI.create("/resource"));
RequestBodySpec bodySpecPost = requestPost1.uri("/resource");
RequestBodySpec bodySpecPostMultipart = requestPost2.uri(uriBuilder -> uriBuilder.pathSegment("resource-multipart")
.build());
RequestBodySpec bodySpecOverridenBaseUri = requestPost2.uri(URI.create("/resource"));

// request header specification
WebClient.RequestHeadersSpec<?> requestSpec1 = uri1.body(BodyInserters.fromPublisher(Mono.just("data"), String.class));
WebClient.RequestHeadersSpec<?> requestSpec2 = uri2.body(BodyInserters.fromValue("data"));
String bodyValue = "bodyValue";
RequestHeadersSpec<?> headerSpecPost1 = bodySpecPost.body(BodyInserters.fromPublisher(Mono.just(bodyValue), String.class));
RequestHeadersSpec<?> headerSpecPost2 = bodySpecPost.body(BodyInserters.fromValue(bodyValue));
RequestHeadersSpec<?> headerSpecGet = requestGet.uri("/resource");

// inserters
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters.fromPublisher(Subscriber::onComplete, String.class);
// request header specification using inserters
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class);

LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("key1", "value1");
map.add("key2", "value2");
map.add("key1", "multipartValue1");
map.add("key2", "multipartValue2");

BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map);
BodyInserter<Object, ReactiveHttpOutputMessage> inserter3 = BodyInserters.fromValue(new Object());
BodyInserter<String, ReactiveHttpOutputMessage> inserter4 = BodyInserters.fromValue("body");
BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData(map);
BodyInserter<Object, ReactiveHttpOutputMessage> inserterObject = BodyInserters.fromValue(new Object());
BodyInserter<String, ReactiveHttpOutputMessage> inserterString = BodyInserters.fromValue(bodyValue);

RequestHeadersSpec<?> headerSpecInserterCompleteSuscriber = bodySpecPost.body(inserterCompleteSuscriber);
RequestHeadersSpec<?> headerSpecInserterMultipart = bodySpecPostMultipart.body(inserterMultipart);
RequestHeadersSpec<?> headerSpecInserterObject = bodySpecPost.body(inserterObject);
RequestHeadersSpec<?> headerSpecInserterString = bodySpecPost.body(inserterString);

// responses
WebClient.ResponseSpec response1 = uri1.body(inserter3)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
ResponseSpec responsePostObject = headerSpecInserterObject.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(Charset.forName("UTF-8"))
.acceptCharset(StandardCharsets.UTF_8)
.ifNoneMatch("*")
.ifModifiedSince(ZonedDateTime.now())
.retrieve();
String response2 = uri1.exchangeToMono(response -> response.bodyToMono(String.class))
String responsePostString = headerSpecInserterString.exchangeToMono(response -> response.bodyToMono(String.class))
.block();
String responsePostMultipart = headerSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.retrieve()
.bodyToMono(String.class)
.block();
String responsePostCompleteSubsciber = headerSpecInserterCompleteSuscriber.retrieve()
.bodyToMono(String.class)
.block();
String responsePostWithBody1 = headerSpecPost1.retrieve()
.bodyToMono(String.class)
.block();
String responsePostWithBody3 = headerSpecPost2.retrieve()
.bodyToMono(String.class)
.block();
String responseGet = headerSpecGet.retrieve()
.bodyToMono(String.class)
.block();
String response3 = uri2.retrieve()
String responsePostWithNoBody = bodySpecPost.retrieve()
.bodyToMono(String.class)
.block();
WebClient.ResponseSpec response4 = requestSpec2.retrieve();
try {
bodySpecOverridenBaseUri.retrieve()
.bodyToMono(String.class)
.block();
} catch (Exception ex) {
System.out.println(ex.getClass());
}
}

private WebClient createWebClient() {
Expand All @@ -101,7 +135,7 @@ private WebClient createWebClientConfiguringTimeout() {

private WebClient createWebClientWithServerURLAndDefaultValues() {
return WebClient.builder()
.baseUrl("http://localhost:8081")
.baseUrl("http://localhost:" + port)
.defaultCookie("cookieKey", "cookieValue")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
Expand Down

0 comments on commit 2359c53

Please sign in to comment.