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