Skip to content

Commit f22e2ac

Browse files
committed
Add HttpServletMapping support to MockHttpServletRequest
See gh-26428
1 parent 355aca7 commit f22e2ac

File tree

4 files changed

+186
-2
lines changed

4 files changed

+186
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.web;
17+
18+
import javax.servlet.http.HttpServletMapping;
19+
import javax.servlet.http.MappingMatch;
20+
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Mock implementation of {@link HttpServletMapping}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 5.3.4
28+
*/
29+
public class MockHttpServletMapping implements HttpServletMapping {
30+
31+
private final String matchValue;
32+
33+
private final String pattern;
34+
35+
private final String servletName;
36+
37+
@Nullable
38+
private final MappingMatch mappingMatch;
39+
40+
41+
public MockHttpServletMapping(
42+
String matchValue, String pattern, String servletName, @Nullable MappingMatch match) {
43+
44+
this.matchValue = matchValue;
45+
this.pattern = pattern;
46+
this.servletName = servletName;
47+
this.mappingMatch = match;
48+
}
49+
50+
51+
@Override
52+
public String getMatchValue() {
53+
return this.matchValue;
54+
}
55+
56+
@Override
57+
public String getPattern() {
58+
return this.pattern;
59+
}
60+
61+
@Override
62+
public String getServletName() {
63+
return this.servletName;
64+
}
65+
66+
@Override
67+
@Nullable
68+
public MappingMatch getMappingMatch() {
69+
return this.mappingMatch;
70+
}
71+
72+
73+
@Override
74+
public String toString() {
75+
return "MockHttpServletMapping [matchValue=\"" + matchValue + "\", " +
76+
"pattern=\"" + pattern + "\", servletName=\"" + servletName + "\", " +
77+
"mappingMatch=" + mappingMatch + "]";
78+
}
79+
80+
}

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@
5252
import javax.servlet.ServletRequest;
5353
import javax.servlet.ServletResponse;
5454
import javax.servlet.http.Cookie;
55+
import javax.servlet.http.HttpServletMapping;
5556
import javax.servlet.http.HttpServletRequest;
5657
import javax.servlet.http.HttpServletResponse;
5758
import javax.servlet.http.HttpSession;
@@ -274,6 +275,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
274275

275276
private final MultiValueMap<String, Part> parts = new LinkedMultiValueMap<>();
276277

278+
private HttpServletMapping httpServletMapping = new MockHttpServletMapping("", "", "", null);
279+
277280

278281
// ---------------------------------------------------------------------
279282
// Constructors
@@ -1390,6 +1393,15 @@ public Collection<Part> getParts() throws IOException, ServletException {
13901393
return result;
13911394
}
13921395

1396+
public void setHttpServletMapping(HttpServletMapping httpServletMapping) {
1397+
this.httpServletMapping = httpServletMapping;
1398+
}
1399+
1400+
@Override
1401+
public HttpServletMapping getHttpServletMapping() {
1402+
return this.httpServletMapping;
1403+
}
1404+
13931405
@Override
13941406
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
13951407
throw new UnsupportedOperationException();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.testfixture.servlet;
17+
18+
import javax.servlet.http.HttpServletMapping;
19+
import javax.servlet.http.MappingMatch;
20+
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Mock implementation of {@link HttpServletMapping}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 5.3.4
28+
*/
29+
public class MockHttpServletMapping implements HttpServletMapping {
30+
31+
private final String matchValue;
32+
33+
private final String pattern;
34+
35+
private final String servletName;
36+
37+
@Nullable
38+
private final MappingMatch mappingMatch;
39+
40+
41+
public MockHttpServletMapping(
42+
String matchValue, String pattern, String servletName, @Nullable MappingMatch match) {
43+
44+
this.matchValue = matchValue;
45+
this.pattern = pattern;
46+
this.servletName = servletName;
47+
this.mappingMatch = match;
48+
}
49+
50+
51+
@Override
52+
public String getMatchValue() {
53+
return this.matchValue;
54+
}
55+
56+
@Override
57+
public String getPattern() {
58+
return this.pattern;
59+
}
60+
61+
@Override
62+
public String getServletName() {
63+
return this.servletName;
64+
}
65+
66+
@Override
67+
@Nullable
68+
public MappingMatch getMappingMatch() {
69+
return this.mappingMatch;
70+
}
71+
72+
73+
@Override
74+
public String toString() {
75+
return "MockHttpServletMapping [matchValue=\"" + matchValue + "\", " +
76+
"pattern=\"" + pattern + "\", servletName=\"" + servletName + "\", " +
77+
"mappingMatch=" + mappingMatch + "]";
78+
}
79+
80+
}

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@
5252
import javax.servlet.ServletRequest;
5353
import javax.servlet.ServletResponse;
5454
import javax.servlet.http.Cookie;
55+
import javax.servlet.http.HttpServletMapping;
5556
import javax.servlet.http.HttpServletRequest;
5657
import javax.servlet.http.HttpServletResponse;
5758
import javax.servlet.http.HttpSession;
@@ -274,6 +275,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
274275

275276
private final MultiValueMap<String, Part> parts = new LinkedMultiValueMap<>();
276277

278+
private HttpServletMapping httpServletMapping = new MockHttpServletMapping("", "", "", null);
279+
277280

278281
// ---------------------------------------------------------------------
279282
// Constructors
@@ -1390,6 +1393,15 @@ public Collection<Part> getParts() throws IOException, ServletException {
13901393
return result;
13911394
}
13921395

1396+
public void setHttpServletMapping(HttpServletMapping httpServletMapping) {
1397+
this.httpServletMapping = httpServletMapping;
1398+
}
1399+
1400+
@Override
1401+
public HttpServletMapping getHttpServletMapping() {
1402+
return this.httpServletMapping;
1403+
}
1404+
13931405
@Override
13941406
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
13951407
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)