added and ordered tests in WebClientIntegrationTest to match article - test not passing (reusing specs)
This commit is contained in:
parent
725465b0b6
commit
2bc7dbc708
|
@ -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;
|
||||
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
|
|||
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;
|
||||
|
@ -22,6 +23,10 @@ import org.springframework.util.MultiValueMap;
|
|||
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;
|
||||
|
||||
|
@ -31,7 +36,7 @@ import io.netty.handler.timeout.WriteTimeoutHandler;
|
|||
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
|
||||
|
@ -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 response3 = uri2.retrieve()
|
||||
String responsePostMultipart = headerSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
WebClient.ResponseSpec response4 = requestSpec2.retrieve();
|
||||
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 responsePostWithNoBody = bodySpecPost.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
try {
|
||||
bodySpecOverridenBaseUri.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
private WebClient createWebClient() {
|
||||
|
@ -101,7 +135,7 @@ public class WebClientIntegrationTest {
|
|||
|
||||
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"))
|
||||
|
|
Loading…
Reference in New Issue