diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index a3cc4c2890..0b46ac391f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -370,8 +370,8 @@ class DefaultWebTestClient implements WebTestClient { private ClientRequest.Builder initRequestBuilder() { ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) - .headers(headers -> headers.addAll(initHeaders())) - .cookies(cookies -> cookies.addAll(initCookies())) + .headers(this::initHeaders) + .cookies(this::initCookies) .attributes(attributes -> attributes.putAll(this.attributes)); if (this.httpRequestConsumer != null) { builder.httpRequest(this.httpRequestConsumer); @@ -383,28 +383,21 @@ class DefaultWebTestClient implements WebTestClient { return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); } - private HttpHeaders initHeaders() { - if (CollectionUtils.isEmpty(defaultHeaders)) { - return this.headers; + private void initHeaders(HttpHeaders out) { + if (!CollectionUtils.isEmpty(defaultHeaders)) { + out.putAll(defaultHeaders); + } + if (!CollectionUtils.isEmpty(this.headers)) { + out.putAll(this.headers); } - HttpHeaders result = new HttpHeaders(); - result.putAll(defaultHeaders); - result.putAll(this.headers); - return result; } - private MultiValueMap initCookies() { - if (CollectionUtils.isEmpty(this.cookies)) { - return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>()); + private void initCookies(MultiValueMap out) { + if (!CollectionUtils.isEmpty(defaultCookies)) { + out.putAll(defaultCookies); } - else if (CollectionUtils.isEmpty(defaultCookies)) { - return this.cookies; - } - else { - MultiValueMap result = new LinkedMultiValueMap<>(); - result.putAll(defaultCookies); - result.putAll(this.cookies); - return result; + if (!CollectionUtils.isEmpty(this.cookies)) { + out.putAll(this.cookies); } } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 5d4f6f27ab..d79b513080 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -401,8 +401,6 @@ class DefaultWebClient implements WebClient { return new HttpRequest() { private final URI uri = initUri(); - private final HttpHeaders headers = initHeaders(); - @Override public HttpMethod getMethod() { return httpMethod; @@ -415,7 +413,9 @@ class DefaultWebClient implements WebClient { @Override public HttpHeaders getHeaders() { - return this.headers; + HttpHeaders headers = new HttpHeaders(); + initHeaders(headers); + return headers; } }; } @@ -488,8 +488,8 @@ class DefaultWebClient implements WebClient { defaultRequest.accept(this); } ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) - .headers(headers -> headers.addAll(initHeaders())) - .cookies(cookies -> cookies.addAll(initCookies())) + .headers(this::initHeaders) + .cookies(this::initCookies) .attributes(attributes -> attributes.putAll(this.attributes)); if (this.httpRequestConsumer != null) { builder.httpRequest(this.httpRequestConsumer); @@ -501,33 +501,21 @@ class DefaultWebClient implements WebClient { return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); } - private HttpHeaders initHeaders() { - if (CollectionUtils.isEmpty(this.headers)) { - return (defaultHeaders != null ? defaultHeaders : new HttpHeaders()); - } - else if (CollectionUtils.isEmpty(defaultHeaders)) { - return this.headers; + private void initHeaders(HttpHeaders out) { + if (!CollectionUtils.isEmpty(defaultHeaders)) { + out.putAll(defaultHeaders); } - else { - HttpHeaders result = new HttpHeaders(); - result.putAll(defaultHeaders); - result.putAll(this.headers); - return result; + if (!CollectionUtils.isEmpty(this.headers)) { + out.putAll(this.headers); } } - private MultiValueMap initCookies() { - if (CollectionUtils.isEmpty(this.cookies)) { - return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>()); - } - else if (CollectionUtils.isEmpty(defaultCookies)) { - return this.cookies; + private void initCookies(MultiValueMap out) { + if (!CollectionUtils.isEmpty(defaultCookies)) { + out.putAll(defaultCookies); } - else { - MultiValueMap result = new LinkedMultiValueMap<>(); - result.putAll(defaultCookies); - result.putAll(this.cookies); - return result; + if (!CollectionUtils.isEmpty(this.cookies)) { + out.putAll(this.cookies); } } }