Merge pull request #6804 from CROSP/webclient-requests
BAEL-2714 - WebClient request with parameters
This commit is contained in:
commit
e094bfc434
|
@ -2,3 +2,4 @@
|
||||||
|
|
||||||
- [Spring Boot Reactor Netty Configuration](https://www.baeldung.com/spring-boot-reactor-netty)
|
- [Spring Boot Reactor Netty Configuration](https://www.baeldung.com/spring-boot-reactor-netty)
|
||||||
- [How to Return 404 with Spring WebFlux](https://www.baeldung.com/spring-webflux-404)
|
- [How to Return 404 with Spring WebFlux](https://www.baeldung.com/spring-webflux-404)
|
||||||
|
- [WebClient request with parameters](https://www.baeldung.com/webclient-request-with-parameters)
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.spring.webclientrequests;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringWebClientRequestsApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringWebClientRequestsApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,164 @@
|
||||||
|
package com.baeldung.spring.webclientrequests;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@WebFluxTest
|
||||||
|
public class WebClientRequestsUnitTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "https://example.com";
|
||||||
|
|
||||||
|
private WebClient webClient;
|
||||||
|
|
||||||
|
@Captor
|
||||||
|
private ArgumentCaptor<ClientRequest> argumentCaptor;
|
||||||
|
|
||||||
|
private ExchangeFunction exchangeFunction;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
this.exchangeFunction = mock(ExchangeFunction.class);
|
||||||
|
ClientResponse mockResponse = mock(ClientResponse.class);
|
||||||
|
when(this.exchangeFunction.exchange(this.argumentCaptor.capture())).thenReturn(Mono.just(mockResponse));
|
||||||
|
this.webClient = WebClient
|
||||||
|
.builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.exchangeFunction(exchangeFunction)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallSimpleURI_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri("/products")
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallSinglePathSegmentUri_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/{id}")
|
||||||
|
.build(2))
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallMultiplePathSegmentsUri_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/{id}/attributes/{attributeId}")
|
||||||
|
.build(2, 13))
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/2/attributes/13");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallSingleQueryParams_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("name", "AndroidPhone")
|
||||||
|
.queryParam("color", "black")
|
||||||
|
.queryParam("deliveryDate", "13/04/2019")
|
||||||
|
.build())
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallSingleQueryParamsPlaceholders_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("name", "{title}")
|
||||||
|
.queryParam("color", "{authorId}")
|
||||||
|
.queryParam("deliveryDate", "{date}")
|
||||||
|
.build("AndroidPhone", "black", "13/04/2019"))
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallArrayQueryParamsBrackets_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("tag[]", "Snapdragon", "NFC")
|
||||||
|
.build())
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallArrayQueryParams_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("category", "Phones", "Tablets")
|
||||||
|
.build())
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?category=Phones&category=Tablets");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallArrayQueryParamsComma_thenURIMatched() {
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("category", String.join(",", "Phones", "Tablets"))
|
||||||
|
.build())
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?category=Phones,Tablets");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUriComponentEncoding_thenQueryParamsNotEscaped() {
|
||||||
|
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL);
|
||||||
|
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
|
||||||
|
this.webClient = WebClient
|
||||||
|
.builder()
|
||||||
|
.uriBuilderFactory(factory)
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.exchangeFunction(exchangeFunction)
|
||||||
|
.build();
|
||||||
|
this.webClient.get()
|
||||||
|
.uri(uriBuilder -> uriBuilder
|
||||||
|
.path("/products/")
|
||||||
|
.queryParam("name", "AndroidPhone")
|
||||||
|
.queryParam("color", "black")
|
||||||
|
.queryParam("deliveryDate", "13/04/2019")
|
||||||
|
.build())
|
||||||
|
.retrieve();
|
||||||
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyCalledUrl(String relativeUrl) {
|
||||||
|
ClientRequest request = this.argumentCaptor.getValue();
|
||||||
|
Assert.assertEquals(String.format("%s%s", BASE_URL, relativeUrl), request.url().toString());
|
||||||
|
Mockito.verify(this.exchangeFunction).exchange(request);
|
||||||
|
verifyNoMoreInteractions(this.exchangeFunction);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue