Copy default headers, cookies in WebClient builder

This commit makes copies of the default headers and cookies when a
WebClient is built, so that subsequent changes to these do not affect
previously built clients.

Closes: gh-25992
This commit is contained in:
Arjen Poutsma
2020-10-29 12:51:47 +01:00
parent a9dec6a4af
commit 0acb1e5513
2 changed files with 62 additions and 3 deletions

View File

@@ -130,7 +130,8 @@ public class DefaultWebClientTests {
@Test
public void defaultHeaderAndCookie() {
WebClient client = this.builder
.defaultHeader("Accept", "application/json").defaultCookie("id", "123")
.defaultHeader("Accept", "application/json")
.defaultCookie("id", "123")
.build();
client.get().uri("/path").exchange().block(Duration.ofSeconds(10));
@@ -157,6 +158,35 @@ public class DefaultWebClientTests {
assertThat(request.cookies().getFirst("id")).isEqualTo("456");
}
@Test
public void defaultHeaderAndCookieCopies() {
WebClient client1 = this.builder
.defaultHeader("Accept", "application/json")
.defaultCookie("id", "123")
.build();
WebClient client2 = this.builder
.defaultHeader("Accept", "application/xml")
.defaultCookies(cookies -> cookies.set("id", "456"))
.build();
client1.get().uri("/path")
.exchange().block(Duration.ofSeconds(10));
ClientRequest request = verifyAndGetRequest();
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/json");
assertThat(request.cookies().getFirst("id")).isEqualTo("123");
client2.get().uri("/path")
.exchange().block(Duration.ofSeconds(10));
request = verifyAndGetRequest();
assertThat(request.headers().getFirst("Accept")).isEqualTo("application/xml");
assertThat(request.cookies().getFirst("id")).isEqualTo("456");
}
@Test
public void defaultRequest() {
ThreadLocal<String> context = new NamedThreadLocal<>("foo");