|
17 | 17 | package org.springframework.http.server.reactive;
|
18 | 18 |
|
19 | 19 | import java.io.ByteArrayInputStream;
|
| 20 | +import java.net.URI; |
20 | 21 | import java.util.Arrays;
|
21 | 22 | import java.util.Collections;
|
22 | 23 | import javax.servlet.AsyncContext;
|
23 | 24 | import javax.servlet.ReadListener;
|
24 | 25 | import javax.servlet.ServletInputStream;
|
25 |
| -import javax.servlet.http.HttpServletRequest; |
26 | 26 |
|
27 | 27 | import org.junit.Test;
|
28 | 28 |
|
29 | 29 | import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
| 30 | +import org.springframework.http.HttpMethod; |
30 | 31 | import org.springframework.mock.web.test.DelegatingServletInputStream;
|
31 | 32 | import org.springframework.mock.web.test.MockAsyncContext;
|
32 | 33 | import org.springframework.mock.web.test.MockHttpServletRequest;
|
33 | 34 | import org.springframework.mock.web.test.MockHttpServletResponse;
|
34 | 35 | import org.springframework.util.MultiValueMap;
|
35 | 36 |
|
36 | 37 | import static org.junit.Assert.*;
|
| 38 | +import static org.mockito.Mockito.*; |
37 | 39 |
|
38 | 40 | /**
|
39 | 41 | * Unit tests for {@link AbstractServerHttpRequest}.
|
@@ -84,33 +86,78 @@ public void queryParamsWithNoValue() throws Exception {
|
84 | 86 | assertEquals(Collections.singletonList(null), params.get("a"));
|
85 | 87 | }
|
86 | 88 |
|
| 89 | + @Test |
| 90 | + public void mutateRequest() throws Exception { |
| 91 | + |
| 92 | + SslInfo sslInfo = mock(SslInfo.class); |
| 93 | + ServerHttpRequest request = createHttpRequest("/").mutate().sslInfo(sslInfo).build(); |
| 94 | + assertSame(sslInfo, request.getSslInfo()); |
| 95 | + |
| 96 | + request = createHttpRequest("/").mutate().method(HttpMethod.DELETE).build(); |
| 97 | + assertEquals(HttpMethod.DELETE, request.getMethod()); |
| 98 | + |
| 99 | + String baseUri = "http://aaa.org:8080/a"; |
| 100 | + |
| 101 | + request = createHttpRequest(baseUri).mutate().uri(URI.create("http://bbb.org:9090/b")).build(); |
| 102 | + assertEquals("http://bbb.org:9090/b", request.getURI().toString()); |
| 103 | + |
| 104 | + request = createHttpRequest(baseUri).mutate().path("/b/c/d").build(); |
| 105 | + assertEquals("http://aaa.org:8080/b/c/d", request.getURI().toString()); |
| 106 | + |
| 107 | + request = createHttpRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build(); |
| 108 | + assertEquals("http://aaa.org:8080/app/b/c/d", request.getURI().toString()); |
| 109 | + assertEquals("/app", request.getPath().contextPath().value()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test(expected = IllegalArgumentException.class) |
| 113 | + public void mutateWithInvalidPath() throws Exception { |
| 114 | + createHttpRequest("/").mutate().path("foo-bar"); |
| 115 | + } |
| 116 | + |
87 | 117 | @Test // SPR-16434
|
88 | 118 | public void mutatePathWithEncodedQueryParams() throws Exception {
|
89 |
| - ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9") |
90 |
| - .mutate().path("/mutatedPath").build(); |
| 119 | + ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9"); |
| 120 | + request = request.mutate().path("/mutatedPath").build(); |
| 121 | + |
91 | 122 | assertEquals("/mutatedPath", request.getURI().getRawPath());
|
92 | 123 | assertEquals("name=%E6%89%8E%E6%A0%B9", request.getURI().getRawQuery());
|
93 | 124 | }
|
94 | 125 |
|
95 |
| - |
96 |
| - private ServerHttpRequest createHttpRequest(String path) throws Exception { |
97 |
| - HttpServletRequest request = createEmptyBodyHttpServletRequest(path); |
| 126 | + private ServerHttpRequest createHttpRequest(String uriString) throws Exception { |
| 127 | + URI uri = URI.create(uriString); |
| 128 | + MockHttpServletRequest request = new TestHttpServletRequest(uri); |
98 | 129 | AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
|
99 | 130 | return new ServletServerHttpRequest(request, asyncContext, "", new DefaultDataBufferFactory(), 1024);
|
100 | 131 | }
|
101 | 132 |
|
102 |
| - private HttpServletRequest createEmptyBodyHttpServletRequest(String path) { |
103 |
| - return new MockHttpServletRequest("GET", path) { |
| 133 | + |
| 134 | + private static class TestHttpServletRequest extends MockHttpServletRequest { |
| 135 | + |
| 136 | + TestHttpServletRequest(URI uri) { |
| 137 | + super("GET", uri.getRawPath()); |
| 138 | + if (uri.getScheme() != null) { |
| 139 | + setScheme(uri.getScheme()); |
| 140 | + } |
| 141 | + if (uri.getHost() != null) { |
| 142 | + setServerName(uri.getHost()); |
| 143 | + } |
| 144 | + if (uri.getPort() != -1) { |
| 145 | + setServerPort(uri.getPort()); |
| 146 | + } |
| 147 | + if (uri.getRawQuery() != null) { |
| 148 | + setQueryString(uri.getRawQuery()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public ServletInputStream getInputStream() { |
| 154 | + return new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])) { |
104 | 155 | @Override
|
105 |
| - public ServletInputStream getInputStream() { |
106 |
| - return new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])) { |
107 |
| - @Override |
108 |
| - public void setReadListener(ReadListener readListener) { |
109 |
| - // Ignore |
110 |
| - } |
111 |
| - }; |
| 156 | + public void setReadListener(ReadListener readListener) { |
| 157 | + // Ignore |
112 | 158 | }
|
113 | 159 | };
|
| 160 | + } |
114 | 161 | }
|
115 | 162 |
|
116 | 163 | }
|
0 commit comments