|
|
|
@ -17,16 +17,17 @@
@@ -17,16 +17,17 @@
|
|
|
|
|
package org.springframework.http.server.reactive; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import javax.servlet.AsyncContext; |
|
|
|
|
import javax.servlet.ReadListener; |
|
|
|
|
import javax.servlet.ServletInputStream; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
|
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
import org.springframework.mock.web.test.DelegatingServletInputStream; |
|
|
|
|
import org.springframework.mock.web.test.MockAsyncContext; |
|
|
|
|
import org.springframework.mock.web.test.MockHttpServletRequest; |
|
|
|
@ -34,6 +35,7 @@ import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -34,6 +35,7 @@ import org.springframework.mock.web.test.MockHttpServletResponse;
|
|
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unit tests for {@link AbstractServerHttpRequest}. |
|
|
|
@ -84,33 +86,78 @@ public class ServerHttpRequestTests {
@@ -84,33 +86,78 @@ public class ServerHttpRequestTests {
|
|
|
|
|
assertEquals(Collections.singletonList(null), params.get("a")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void mutateRequest() throws Exception { |
|
|
|
|
|
|
|
|
|
SslInfo sslInfo = mock(SslInfo.class); |
|
|
|
|
ServerHttpRequest request = createHttpRequest("/").mutate().sslInfo(sslInfo).build(); |
|
|
|
|
assertSame(sslInfo, request.getSslInfo()); |
|
|
|
|
|
|
|
|
|
request = createHttpRequest("/").mutate().method(HttpMethod.DELETE).build(); |
|
|
|
|
assertEquals(HttpMethod.DELETE, request.getMethod()); |
|
|
|
|
|
|
|
|
|
String baseUri = "http://aaa.org:8080/a"; |
|
|
|
|
|
|
|
|
|
request = createHttpRequest(baseUri).mutate().uri(URI.create("http://bbb.org:9090/b")).build(); |
|
|
|
|
assertEquals("http://bbb.org:9090/b", request.getURI().toString()); |
|
|
|
|
|
|
|
|
|
request = createHttpRequest(baseUri).mutate().path("/b/c/d").build(); |
|
|
|
|
assertEquals("http://aaa.org:8080/b/c/d", request.getURI().toString()); |
|
|
|
|
|
|
|
|
|
request = createHttpRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build(); |
|
|
|
|
assertEquals("http://aaa.org:8080/app/b/c/d", request.getURI().toString()); |
|
|
|
|
assertEquals("/app", request.getPath().contextPath().value()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void mutateWithInvalidPath() throws Exception { |
|
|
|
|
createHttpRequest("/").mutate().path("foo-bar"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // SPR-16434
|
|
|
|
|
public void mutatePathWithEncodedQueryParams() throws Exception { |
|
|
|
|
ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9") |
|
|
|
|
.mutate().path("/mutatedPath").build(); |
|
|
|
|
ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9"); |
|
|
|
|
request = request.mutate().path("/mutatedPath").build(); |
|
|
|
|
|
|
|
|
|
assertEquals("/mutatedPath", request.getURI().getRawPath()); |
|
|
|
|
assertEquals("name=%E6%89%8E%E6%A0%B9", request.getURI().getRawQuery()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ServerHttpRequest createHttpRequest(String path) throws Exception { |
|
|
|
|
HttpServletRequest request = createEmptyBodyHttpServletRequest(path); |
|
|
|
|
private ServerHttpRequest createHttpRequest(String uriString) throws Exception { |
|
|
|
|
URI uri = URI.create(uriString); |
|
|
|
|
MockHttpServletRequest request = new TestHttpServletRequest(uri); |
|
|
|
|
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse()); |
|
|
|
|
return new ServletServerHttpRequest(request, asyncContext, "", new DefaultDataBufferFactory(), 1024); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private HttpServletRequest createEmptyBodyHttpServletRequest(String path) { |
|
|
|
|
return new MockHttpServletRequest("GET", path) { |
|
|
|
|
|
|
|
|
|
private static class TestHttpServletRequest extends MockHttpServletRequest { |
|
|
|
|
|
|
|
|
|
TestHttpServletRequest(URI uri) { |
|
|
|
|
super("GET", uri.getRawPath()); |
|
|
|
|
if (uri.getScheme() != null) { |
|
|
|
|
setScheme(uri.getScheme()); |
|
|
|
|
} |
|
|
|
|
if (uri.getHost() != null) { |
|
|
|
|
setServerName(uri.getHost()); |
|
|
|
|
} |
|
|
|
|
if (uri.getPort() != -1) { |
|
|
|
|
setServerPort(uri.getPort()); |
|
|
|
|
} |
|
|
|
|
if (uri.getRawQuery() != null) { |
|
|
|
|
setQueryString(uri.getRawQuery()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public ServletInputStream getInputStream() { |
|
|
|
|
return new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])) { |
|
|
|
|
@Override |
|
|
|
|
public ServletInputStream getInputStream() { |
|
|
|
|
return new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])) { |
|
|
|
|
@Override |
|
|
|
|
public void setReadListener(ReadListener readListener) { |
|
|
|
|
// Ignore
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
public void setReadListener(ReadListener readListener) { |
|
|
|
|
// Ignore
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|