|
37 | 37 |
|
38 | 38 | import static org.assertj.core.api.Assertions.assertThat;
|
39 | 39 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 40 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
40 | 41 | import static org.mockito.Mockito.mock;
|
41 | 42 |
|
42 | 43 | /**
|
@@ -164,14 +165,42 @@ public void mutateHeaderBySettingHeaderValues() throws Exception {
|
164 | 165 | assertThat(request.getHeaders().get(headerName)).containsExactly(headerValue3);
|
165 | 166 | }
|
166 | 167 |
|
| 168 | + @Test |
| 169 | + void mutateWithExistingContextPath() throws Exception { |
| 170 | + ServerHttpRequest request = createRequest("/context/path", "/context"); |
| 171 | + |
| 172 | + ServerHttpRequest mutated = request.mutate().build(); |
| 173 | + assertThat(mutated.getPath().contextPath().value()).isEqualTo("/context"); |
| 174 | + assertThat(mutated.getPath().pathWithinApplication().value()).isEqualTo("/path"); |
| 175 | + assertThat(mutated.getURI().getRawPath()).isEqualTo("/context/path"); |
| 176 | + |
| 177 | + mutated = request.mutate().contextPath("/other").path("/other/path").build(); |
| 178 | + assertThat(mutated.getPath().contextPath().value()).isEqualTo("/other"); |
| 179 | + assertThat(mutated.getPath().pathWithinApplication().value()).isEqualTo("/path"); |
| 180 | + assertThat(mutated.getURI().getRawPath()).isEqualTo("/other/path"); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + void mutateContextPathWithoutUpdatingPathShouldFail() throws Exception { |
| 185 | + ServerHttpRequest request = createRequest("/context/path", "/context"); |
| 186 | + |
| 187 | + assertThatThrownBy(() -> request.mutate().path("/fail").build()) |
| 188 | + .isInstanceOf(IllegalArgumentException.class) |
| 189 | + .hasMessage("Invalid contextPath '/context': must match the start of requestPath: '/fail'"); |
| 190 | + } |
| 191 | + |
167 | 192 | private ServerHttpRequest createRequest(String uriString) throws Exception {
|
| 193 | + return createRequest(uriString, ""); |
| 194 | + } |
| 195 | + |
| 196 | + private ServerHttpRequest createRequest(String uriString, String contextPath) throws Exception { |
168 | 197 | URI uri = URI.create(uriString);
|
169 | 198 | MockHttpServletRequest request = new TestHttpServletRequest(uri);
|
| 199 | + request.setContextPath(contextPath); |
170 | 200 | AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
|
171 | 201 | return new ServletServerHttpRequest(request, asyncContext, "", new DefaultDataBufferFactory(), 1024);
|
172 | 202 | }
|
173 | 203 |
|
174 |
| - |
175 | 204 | private static class TestHttpServletRequest extends MockHttpServletRequest {
|
176 | 205 |
|
177 | 206 | TestHttpServletRequest(URI uri) {
|
|
0 commit comments