Add WebClient.filter method

This commit introduces a new method on WebClient: filter, which takes an
filter function, and returns a (filtered) WebClient.

Issue: SPR-14961
This commit is contained in:
Arjen Poutsma
2016-12-06 13:03:49 +01:00
parent 079eca9f63
commit 9e72033036
3 changed files with 47 additions and 1 deletions

View File

@@ -268,7 +268,7 @@ public class WebClientIntegrationTests {
}
@Test
public void filter() throws Exception {
public void buildFilter() throws Exception {
HttpUrl baseUrl = server.url("/greeting?name=Spring");
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
@@ -296,6 +296,35 @@ public class WebClientIntegrationTests {
}
@Test
public void filter() throws Exception {
HttpUrl baseUrl = server.url("/greeting?name=Spring");
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
ExchangeFilterFunction filter = (request, next) -> {
ClientRequest<?> filteredRequest = ClientRequest.from(request)
.header("foo", "bar").build();
return next.exchange(filteredRequest);
};
WebClient client = WebClient.create(new ReactorClientHttpConnector());
WebClient filteredClient = client.filter(filter);
ClientRequest<Void> request = ClientRequest.GET(baseUrl.toString()).build();
Mono<String> result = filteredClient.exchange(request)
.then(response -> response.body(toMono(String.class)));
StepVerifier.create(result)
.expectNext("Hello Spring!")
.expectComplete()
.verify();
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(1, server.getRequestCount());
assertEquals("bar", recordedRequest.getHeader("foo"));
}
@After
public void tearDown() throws Exception {
this.server.shutdown();