24
24
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
25
25
26
26
import static org .assertj .core .api .Assertions .assertThat ;
27
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
28
+ import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
27
29
28
30
/**
29
31
* Tests for {@link ServletRequestPathUtils}.
30
32
*
31
33
* @author Rossen Stoyanchev
34
+ * @author Stephane Nicoll
32
35
*/
33
36
class ServletRequestPathUtilsTests {
34
37
@@ -47,19 +50,63 @@ void parseAndCache() {
47
50
testParseAndCache ("/app/servlet/a//" , "/app" , "/servlet" , "/a//" );
48
51
}
49
52
53
+ @ Test
54
+ void modifyPathContextWithExistingContextPath () {
55
+ RequestPath requestPath = createRequestPath ("/app/api/persons/42" , "/app" , "/api" , "/persons/42" );
56
+ assertThatIllegalStateException ().isThrownBy (() -> requestPath .modifyContextPath ("/persons" ))
57
+ .withMessage ("Could not change context path to '/api/persons': a context path is already specified" );
58
+ }
59
+
60
+ @ Test
61
+ void modifyPathContextWhenContextPathIsNotInThePath () {
62
+ RequestPath requestPath = createRequestPath ("/api/persons/42" , "" , "/api" , "/persons/42" );
63
+ assertThatIllegalArgumentException ().isThrownBy (() -> requestPath .modifyContextPath ("/something" ))
64
+ .withMessage ("Invalid contextPath '/api/something': " +
65
+ "must match the start of requestPath: '/api/persons/42'" );
66
+ }
67
+
68
+ @ Test
69
+ void modifyPathContextReplacesServletPath () {
70
+ RequestPath requestPath = createRequestPath ("/api/persons/42" , "" , "/api" , "/persons/42" );
71
+ RequestPath updatedRequestPath = requestPath .modifyContextPath ("/persons" );
72
+ assertThat (updatedRequestPath .contextPath ().value ()).isEqualTo ("/api/persons" );
73
+ assertThat (updatedRequestPath .pathWithinApplication ().value ()).isEqualTo ("/42" );
74
+ assertThat (updatedRequestPath .value ()).isEqualTo ("/api/persons/42" );
75
+ }
76
+
77
+ @ Test
78
+ void modifyPathContextWithContextPathNotStartingWithSlash () {
79
+ RequestPath requestPath = createRequestPath ("/api/persons/42" , "" , "/api" , "/persons/42" );
80
+ assertThatIllegalArgumentException ().isThrownBy (() -> requestPath .modifyContextPath ("persons" ))
81
+ .withMessage ("Invalid contextPath 'persons': must start with '/' and not end with '/'" );
82
+ }
83
+
84
+ @ Test
85
+ void modifyPathContextWithContextPathEndingWithSlash () {
86
+ RequestPath requestPath = createRequestPath ("/api/persons/42" , "" , "/api" , "/persons/42" );
87
+ assertThatIllegalArgumentException ().isThrownBy (() -> requestPath .modifyContextPath ("/persons/" ))
88
+ .withMessage ("Invalid contextPath '/persons/': must start with '/' and not end with '/'" );
89
+ }
90
+
50
91
private void testParseAndCache (
51
92
String requestUri , String contextPath , String servletPath , String pathWithinApplication ) {
52
93
94
+ RequestPath requestPath = createRequestPath (requestUri , contextPath , servletPath , pathWithinApplication );
95
+
96
+ assertThat (requestPath .contextPath ().value ()).isEqualTo (contextPath );
97
+ assertThat (requestPath .pathWithinApplication ().value ()).isEqualTo (pathWithinApplication );
98
+ }
99
+
100
+ private static RequestPath createRequestPath (
101
+ String requestUri , String contextPath , String servletPath , String pathWithinApplication ) {
102
+
53
103
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , requestUri );
54
104
request .setContextPath (contextPath );
55
105
request .setServletPath (servletPath );
56
106
request .setHttpServletMapping (new MockHttpServletMapping (
57
107
pathWithinApplication , contextPath , "myServlet" , MappingMatch .PATH ));
58
108
59
- RequestPath requestPath = ServletRequestPathUtils .parseAndCache (request );
60
-
61
- assertThat (requestPath .contextPath ().value ()).isEqualTo (contextPath );
62
- assertThat (requestPath .pathWithinApplication ().value ()).isEqualTo (pathWithinApplication );
109
+ return ServletRequestPathUtils .parseAndCache (request );
63
110
}
64
111
65
112
}
0 commit comments