Replace WebClient.filter with Builder.filter
This commit replaces the WebClient.filter method with WebClient.Builder.filter. The reason for this change is that filters added via WebClient.filter would be applied in the opposite order of their declaration, due to the compositional nature of the method, combined with the immutable nature of the WebClient. WebClient.Builder.filter does keep the order of the filters, as registered. Furthermore, this commit introduces a WebClient.mutate() method, returning a WebClient.Builder. This method allow to add/remove filters and other defaults from a given WebClient. Issue: SPR-15657 Add WebClient.Builder.addFilter Add Consumer-based headers and cookies methods to builders. Add WebClient.mutate
This commit is contained in:
@@ -124,6 +124,28 @@ public class DefaultWebClientTests {
|
||||
client.post().uri("http://example.com").syncBody(mono);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateDoesCopy() throws Exception {
|
||||
|
||||
WebClient.Builder builder = WebClient.builder();
|
||||
builder.filter((request, next) -> next.exchange(request));
|
||||
builder.defaultHeader("foo", "bar");
|
||||
builder.defaultCookie("foo", "bar");
|
||||
WebClient client1 = builder.build();
|
||||
builder.filter((request, next) -> next.exchange(request));
|
||||
builder.defaultHeader("baz", "qux");
|
||||
builder.defaultCookie("baz", "qux");
|
||||
WebClient client2 = builder.build();
|
||||
|
||||
client1.mutate().filters(filters -> assertEquals(1, filters.size()));
|
||||
client1.mutate().defaultHeaders(headers -> assertEquals(1, headers.size()));
|
||||
client1.mutate().defaultCookies(cookies -> assertEquals(1, cookies.size()));
|
||||
client2.mutate().filters(filters -> assertEquals(2, filters.size()));
|
||||
client2.mutate().defaultHeaders(headers -> assertEquals(2, headers.size()));
|
||||
client2.mutate().defaultCookies(cookies -> assertEquals(2, cookies.size()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().baseUrl("/base").exchangeFunction(this.exchangeFunction);
|
||||
|
||||
@@ -52,11 +52,12 @@ public class WebClientIntegrationTests {
|
||||
|
||||
private WebClient webClient;
|
||||
|
||||
private String baseUrl;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.server = new MockWebServer();
|
||||
String baseUrl = this.server.url("/").toString();
|
||||
baseUrl = this.server.url("/").toString();
|
||||
this.webClient = WebClient.create(baseUrl);
|
||||
}
|
||||
|
||||
@@ -394,13 +395,16 @@ public class WebClientIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void filter() throws Exception {
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain")
|
||||
.setBody("Hello Spring!"));
|
||||
|
||||
WebClient filteredClient = this.webClient.filter(
|
||||
(request, next) -> {
|
||||
ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
|
||||
WebClient filteredClient = this.webClient.mutate()
|
||||
.filter((request, next) -> {
|
||||
ClientRequest filteredRequest =
|
||||
ClientRequest.from(request).header("foo", "bar").build();
|
||||
return next.exchange(filteredRequest);
|
||||
});
|
||||
})
|
||||
.build();
|
||||
|
||||
Mono<String> result = filteredClient.get()
|
||||
.uri("/greeting?name=Spring")
|
||||
@@ -429,7 +433,9 @@ public class WebClientIntegrationTests {
|
||||
}
|
||||
);
|
||||
|
||||
WebClient filteredClient = this.webClient.filter(filter);
|
||||
WebClient filteredClient = this.webClient.mutate()
|
||||
.filter(filter)
|
||||
.build();
|
||||
|
||||
// header not present
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
|
||||
@@ -462,6 +468,7 @@ public class WebClientIntegrationTests {
|
||||
Assert.assertEquals(2, server.getRequestCount());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class MyException extends RuntimeException {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user