Avoid parse cookies when mutating request or response
When mutating a ServerHttpRequest or ClientResponse, the respective builders no longer access cookies automatically which causes them to be parsed and does so only if necessary. Likewise re-applying the read-only HttpHeaders wrapper is avoided. See gh-24680
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,13 +49,13 @@ public class ServerHttpRequestTests {
|
||||
|
||||
@Test
|
||||
public void queryParamsNone() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParams() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path?a=A&b=B").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path?a=A&b=B").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(2);
|
||||
assertThat(params.get("a")).isEqualTo(Collections.singletonList("A"));
|
||||
assertThat(params.get("b")).isEqualTo(Collections.singletonList("B"));
|
||||
@@ -63,84 +63,87 @@ public class ServerHttpRequestTests {
|
||||
|
||||
@Test
|
||||
public void queryParamsWithMultipleValues() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path?a=1&a=2").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path?a=1&a=2").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(1);
|
||||
assertThat(params.get("a")).isEqualTo(Arrays.asList("1", "2"));
|
||||
}
|
||||
|
||||
@Test // SPR-15140
|
||||
public void queryParamsWithEncodedValue() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path?a=%20%2B+%C3%A0").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path?a=%20%2B+%C3%A0").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(1);
|
||||
assertThat(params.get("a")).isEqualTo(Collections.singletonList(" + \u00e0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParamsWithEmptyValue() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path?a=").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path?a=").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(1);
|
||||
assertThat(params.get("a")).isEqualTo(Collections.singletonList(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParamsWithNoValue() throws Exception {
|
||||
MultiValueMap<String, String> params = createHttpRequest("/path?a").getQueryParams();
|
||||
MultiValueMap<String, String> params = createRequest("/path?a").getQueryParams();
|
||||
assertThat(params.size()).isEqualTo(1);
|
||||
assertThat(params.get("a")).isEqualTo(Collections.singletonList(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateRequest() throws Exception {
|
||||
SslInfo sslInfo = mock(SslInfo.class);
|
||||
ServerHttpRequest request = createHttpRequest("/").mutate().sslInfo(sslInfo).build();
|
||||
assertThat(request.getSslInfo()).isSameAs(sslInfo);
|
||||
|
||||
request = createHttpRequest("/").mutate().method(HttpMethod.DELETE).build();
|
||||
public void mutateRequestMethod() throws Exception {
|
||||
ServerHttpRequest request = createRequest("/").mutate().method(HttpMethod.DELETE).build();
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.DELETE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateSslInfo() throws Exception {
|
||||
SslInfo sslInfo = mock(SslInfo.class);
|
||||
ServerHttpRequest request = createRequest("/").mutate().sslInfo(sslInfo).build();
|
||||
assertThat(request.getSslInfo()).isSameAs(sslInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateUriAndPath() throws Exception {
|
||||
String baseUri = "https://aaa.org:8080/a";
|
||||
|
||||
request = createHttpRequest(baseUri).mutate().uri(URI.create("https://bbb.org:9090/b")).build();
|
||||
ServerHttpRequest request = createRequest(baseUri).mutate().uri(URI.create("https://bbb.org:9090/b")).build();
|
||||
assertThat(request.getURI().toString()).isEqualTo("https://bbb.org:9090/b");
|
||||
|
||||
request = createHttpRequest(baseUri).mutate().path("/b/c/d").build();
|
||||
request = createRequest(baseUri).mutate().path("/b/c/d").build();
|
||||
assertThat(request.getURI().toString()).isEqualTo("https://aaa.org:8080/b/c/d");
|
||||
|
||||
request = createHttpRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build();
|
||||
request = createRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build();
|
||||
assertThat(request.getURI().toString()).isEqualTo("https://aaa.org:8080/app/b/c/d");
|
||||
assertThat(request.getPath().contextPath().value()).isEqualTo("/app");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateWithInvalidPath() throws Exception {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
createHttpRequest("/").mutate().path("foo-bar"));
|
||||
}
|
||||
|
||||
@Test // SPR-16434
|
||||
public void mutatePathWithEncodedQueryParams() throws Exception {
|
||||
ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9");
|
||||
ServerHttpRequest request = createRequest("/path?name=%E6%89%8E%E6%A0%B9");
|
||||
request = request.mutate().path("/mutatedPath").build();
|
||||
|
||||
assertThat(request.getURI().getRawPath()).isEqualTo("/mutatedPath");
|
||||
assertThat(request.getURI().getRawQuery()).isEqualTo("name=%E6%89%8E%E6%A0%B9");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateWithInvalidPath() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> createRequest("/").mutate().path("foo-bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mutateHeadersViaConsumer() throws Exception {
|
||||
String headerName = "key";
|
||||
String headerValue1 = "value1";
|
||||
String headerValue2 = "value2";
|
||||
|
||||
ServerHttpRequest request = createHttpRequest("/path");
|
||||
ServerHttpRequest request = createRequest("/path");
|
||||
assertThat(request.getHeaders().get(headerName)).isNull();
|
||||
|
||||
request = request.mutate().headers(headers -> headers.add(headerName, headerValue1)).build();
|
||||
|
||||
assertThat(request.getHeaders().get(headerName)).containsExactly(headerValue1);
|
||||
|
||||
request = request.mutate().headers(headers -> headers.add(headerName, headerValue2)).build();
|
||||
|
||||
assertThat(request.getHeaders().get(headerName)).containsExactly(headerValue1, headerValue2);
|
||||
}
|
||||
|
||||
@@ -151,19 +154,17 @@ public class ServerHttpRequestTests {
|
||||
String headerValue2 = "value2";
|
||||
String headerValue3 = "value3";
|
||||
|
||||
ServerHttpRequest request = createHttpRequest("/path");
|
||||
ServerHttpRequest request = createRequest("/path");
|
||||
assertThat(request.getHeaders().get(headerName)).isNull();
|
||||
|
||||
request = request.mutate().header(headerName, headerValue1, headerValue2).build();
|
||||
|
||||
assertThat(request.getHeaders().get(headerName)).containsExactly(headerValue1, headerValue2);
|
||||
|
||||
request = request.mutate().header(headerName, headerValue3).build();
|
||||
|
||||
assertThat(request.getHeaders().get(headerName)).containsExactly(headerValue3);
|
||||
}
|
||||
|
||||
private ServerHttpRequest createHttpRequest(String uriString) throws Exception {
|
||||
private ServerHttpRequest createRequest(String uriString) throws Exception {
|
||||
URI uri = URI.create(uriString);
|
||||
MockHttpServletRequest request = new TestHttpServletRequest(uri);
|
||||
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
|
||||
|
||||
Reference in New Issue
Block a user