Browse Source

Remove extra copy in WebClient headers/cookies

See gh-30092
pull/30120/head
James Yuzawa 2 years ago committed by rstoyanchev
parent
commit
cd8955fa72
  1. 33
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java
  2. 42
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

33
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() { private ClientRequest.Builder initRequestBuilder() {
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders())) .headers(this::initHeaders)
.cookies(cookies -> cookies.addAll(initCookies())) .cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes)); .attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) { if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer); builder.httpRequest(this.httpRequestConsumer);
@ -383,28 +383,21 @@ class DefaultWebTestClient implements WebTestClient {
return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
} }
private HttpHeaders initHeaders() { private void initHeaders(HttpHeaders out) {
if (CollectionUtils.isEmpty(defaultHeaders)) { if (!CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers; 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<String, String> initCookies() { private void initCookies(MultiValueMap<String, String> out) {
if (CollectionUtils.isEmpty(this.cookies)) { if (!CollectionUtils.isEmpty(defaultCookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>()); out.putAll(defaultCookies);
} }
else if (CollectionUtils.isEmpty(defaultCookies)) { if (!CollectionUtils.isEmpty(this.cookies)) {
return this.cookies; out.putAll(this.cookies);
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
} }
} }
} }

42
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -401,8 +401,6 @@ class DefaultWebClient implements WebClient {
return new HttpRequest() { return new HttpRequest() {
private final URI uri = initUri(); private final URI uri = initUri();
private final HttpHeaders headers = initHeaders();
@Override @Override
public HttpMethod getMethod() { public HttpMethod getMethod() {
return httpMethod; return httpMethod;
@ -415,7 +413,9 @@ class DefaultWebClient implements WebClient {
@Override @Override
public HttpHeaders getHeaders() { 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); defaultRequest.accept(this);
} }
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders())) .headers(this::initHeaders)
.cookies(cookies -> cookies.addAll(initCookies())) .cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes)); .attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) { if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer); builder.httpRequest(this.httpRequestConsumer);
@ -501,33 +501,21 @@ class DefaultWebClient implements WebClient {
return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
} }
private HttpHeaders initHeaders() { private void initHeaders(HttpHeaders out) {
if (CollectionUtils.isEmpty(this.headers)) { if (!CollectionUtils.isEmpty(defaultHeaders)) {
return (defaultHeaders != null ? defaultHeaders : new HttpHeaders()); out.putAll(defaultHeaders);
}
else if (CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers;
} }
else { if (!CollectionUtils.isEmpty(this.headers)) {
HttpHeaders result = new HttpHeaders(); out.putAll(this.headers);
result.putAll(defaultHeaders);
result.putAll(this.headers);
return result;
} }
} }
private MultiValueMap<String, String> initCookies() { private void initCookies(MultiValueMap<String, String> out) {
if (CollectionUtils.isEmpty(this.cookies)) { if (!CollectionUtils.isEmpty(defaultCookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>()); out.putAll(defaultCookies);
}
else if (CollectionUtils.isEmpty(defaultCookies)) {
return this.cookies;
} }
else { if (!CollectionUtils.isEmpty(this.cookies)) {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(); out.putAll(this.cookies);
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
} }
} }
} }

Loading…
Cancel
Save