|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
52 | 52 | import jakarta.servlet.ServletRequest;
|
53 | 53 | import jakarta.servlet.ServletResponse;
|
54 | 54 | import jakarta.servlet.http.Cookie;
|
| 55 | +import jakarta.servlet.http.HttpServletMapping; |
55 | 56 | import jakarta.servlet.http.HttpServletRequest;
|
56 | 57 | import jakarta.servlet.http.HttpServletResponse;
|
57 | 58 | import jakarta.servlet.http.HttpSession;
|
58 | 59 | import jakarta.servlet.http.HttpUpgradeHandler;
|
| 60 | +import jakarta.servlet.http.MappingMatch; |
59 | 61 | import jakarta.servlet.http.Part;
|
60 | 62 |
|
61 | 63 | import org.springframework.http.HttpHeaders;
|
|
69 | 71 | import org.springframework.util.ObjectUtils;
|
70 | 72 | import org.springframework.util.StreamUtils;
|
71 | 73 | import org.springframework.util.StringUtils;
|
| 74 | +import org.springframework.web.util.UrlPathHelper; |
72 | 75 |
|
73 | 76 | /**
|
74 | 77 | * Mock implementation of the {@link jakarta.servlet.http.HttpServletRequest} interface.
|
@@ -274,6 +277,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
274 | 277 |
|
275 | 278 | private final MultiValueMap<String, Part> parts = new LinkedMultiValueMap<>();
|
276 | 279 |
|
| 280 | + @Nullable |
| 281 | + private HttpServletMapping httpServletMapping; |
| 282 | + |
277 | 283 |
|
278 | 284 | // ---------------------------------------------------------------------
|
279 | 285 | // Constructors
|
@@ -1389,6 +1395,33 @@ public Collection<Part> getParts() throws IOException, ServletException {
|
1389 | 1395 | return result;
|
1390 | 1396 | }
|
1391 | 1397 |
|
| 1398 | + public void setHttpServletMapping(@Nullable HttpServletMapping httpServletMapping) { |
| 1399 | + this.httpServletMapping = httpServletMapping; |
| 1400 | + } |
| 1401 | + |
| 1402 | + @Override |
| 1403 | + public HttpServletMapping getHttpServletMapping() { |
| 1404 | + return (this.httpServletMapping == null ? |
| 1405 | + new MockHttpServletMapping("", "", "", determineMappingMatch()) : |
| 1406 | + this.httpServletMapping); |
| 1407 | + } |
| 1408 | + |
| 1409 | + /** |
| 1410 | + * Best effort to detect a Servlet path mapping, e.g. {@code "/foo/*"}, by |
| 1411 | + * checking whether the length of requestURI > contextPath + servletPath. |
| 1412 | + * This helps {@link org.springframework.web.util.ServletRequestPathUtils} |
| 1413 | + * to take into account the Servlet path when parsing the requestURI. |
| 1414 | + */ |
| 1415 | + @Nullable |
| 1416 | + private MappingMatch determineMappingMatch() { |
| 1417 | + if (StringUtils.hasText(this.requestURI) && StringUtils.hasText(this.servletPath)) { |
| 1418 | + String path = UrlPathHelper.defaultInstance.getRequestUri(this); |
| 1419 | + String prefix = this.contextPath + this.servletPath; |
| 1420 | + return (path.startsWith(prefix) && (path.length() > prefix.length()) ? MappingMatch.PATH : null); |
| 1421 | + } |
| 1422 | + return null; |
| 1423 | + } |
| 1424 | + |
1392 | 1425 | @Override
|
1393 | 1426 | public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
|
1394 | 1427 | throw new UnsupportedOperationException();
|
|
0 commit comments