40
40
public class UrlHandlerFilterTests {
41
41
42
42
@ Test
43
- void trailingSlashWithRequestWrapping () throws Exception {
44
- testTrailingSlashWithRequestWrapping ("/path/**" , "/path/123" , null );
45
- testTrailingSlashWithRequestWrapping ("/path/*" , "/path" , "/123" );
46
- testTrailingSlashWithRequestWrapping ("/path/*" , "" , "/path/123" );
43
+ void requestWrapping () throws Exception {
44
+ testRequestWrapping ("/path/**" , "/path/123" , null );
45
+ testRequestWrapping ("/path/*" , "/path" , "/123" );
46
+ testRequestWrapping ("/path/*" , "" , "/path/123" );
47
47
}
48
48
49
- void testTrailingSlashWithRequestWrapping (
50
- String pattern , String servletPath , @ Nullable String pathInfo ) throws Exception {
49
+ void testRequestWrapping (String pattern , String servletPath , @ Nullable String pathInfo ) throws Exception {
51
50
52
51
UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler (pattern ).wrapRequest ().build ();
53
52
@@ -70,31 +69,32 @@ void testTrailingSlashWithRequestWrapping(
70
69
}
71
70
72
71
@ Test
73
- void shouldNotSkipTrailingSlashForRootPath () throws Exception {
74
- UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/**" ).wrapRequest ().build ();
75
- MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/" );
76
- request .setPathInfo ("/" );
72
+ void redirect () throws Exception {
73
+ HttpStatus status = HttpStatus .PERMANENT_REDIRECT ;
74
+ UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/*" ).redirect (status ).build ();
75
+
76
+ String path = "/path/123" ;
77
+ MockHttpServletResponse response = new MockHttpServletResponse ();
77
78
78
79
MockFilterChain chain = new MockFilterChain ();
79
- filter .doFilterInternal (request , new MockHttpServletResponse () , chain );
80
+ filter .doFilterInternal (new MockHttpServletRequest ( "GET" , path + "/" ), response , chain );
80
81
81
- HttpServletRequest actual = (HttpServletRequest ) chain .getRequest ();
82
- assertThat (actual ).isNotNull ().isSameAs (request );
83
- assertThat (actual .getRequestURI ()).isEqualTo ("/" );
84
- assertThat (actual .getRequestURL ().toString ()).isEqualTo ("http://localhost/" );
85
- assertThat (actual .getServletPath ()).isEqualTo ("" );
86
- assertThat (actual .getPathInfo ()).isEqualTo ("/" );
82
+ assertThat (chain .getRequest ()).isNull ();
83
+ assertThat (response .getStatus ()).isEqualTo (status .value ());
84
+ assertThat (response .getHeader (HttpHeaders .LOCATION )).isEqualTo (path );
85
+ assertThat (response .isCommitted ()).isTrue ();
87
86
}
88
87
89
88
@ Test
90
- void noTrailingSlashWithRequestWrapping () throws Exception {
91
- testNoTrailingSlashWithRequestWrapping ("/path/**" , "/path/123" );
92
- testNoTrailingSlashWithRequestWrapping ("/path/*" , "/path/123" );
89
+ void noUrlHandling () throws Exception {
90
+ testNoUrlHandling ("/path/**" , "/path/123" );
91
+ testNoUrlHandling ("/path/*" , "/path/123" );
92
+ testNoUrlHandling ("/**" , "/" ); // gh-33444
93
93
}
94
94
95
- private static void testNoTrailingSlashWithRequestWrapping (
96
- String pattern , String requestURI ) throws ServletException , IOException {
95
+ private static void testNoUrlHandling (String pattern , String requestURI ) throws ServletException , IOException {
97
96
97
+ // No request wrapping
98
98
UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler (pattern ).wrapRequest ().build ();
99
99
100
100
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , requestURI );
@@ -103,34 +103,15 @@ private static void testNoTrailingSlashWithRequestWrapping(
103
103
104
104
HttpServletRequest actual = (HttpServletRequest ) chain .getRequest ();
105
105
assertThat (actual ).as ("Request should not be wrapped" ).isSameAs (request );
106
- }
107
-
108
- @ Test
109
- void trailingSlashHandlerWithRedirect () throws Exception {
110
- HttpStatus status = HttpStatus .PERMANENT_REDIRECT ;
111
- UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/*" ).redirect (status ).build ();
112
-
113
- String path = "/path/123" ;
114
- MockHttpServletResponse response = new MockHttpServletResponse ();
115
-
116
- MockFilterChain chain = new MockFilterChain ();
117
- filter .doFilterInternal (new MockHttpServletRequest ("GET" , path + "/" ), response , chain );
118
-
119
- assertThat (chain .getRequest ()).isNull ();
120
- assertThat (response .getStatus ()).isEqualTo (status .value ());
121
- assertThat (response .getHeader (HttpHeaders .LOCATION )).isEqualTo (path );
122
- assertThat (response .isCommitted ()).isTrue ();
123
- }
124
106
125
- @ Test
126
- void noTrailingSlashWithRedirect () throws Exception {
107
+ // No redirect
127
108
HttpStatus status = HttpStatus .PERMANENT_REDIRECT ;
128
- UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/*" ).redirect (status ).build ();
109
+ filter = UrlHandlerFilter .trailingSlashHandler (pattern ).redirect (status ).build ();
129
110
130
- MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/path/123" );
111
+ request = new MockHttpServletRequest ("GET" , requestURI );
131
112
MockHttpServletResponse response = new MockHttpServletResponse ();
132
113
133
- MockFilterChain chain = new MockFilterChain ();
114
+ chain = new MockFilterChain ();
134
115
filter .doFilterInternal (request , response , chain );
135
116
136
117
assertThat (chain .getRequest ()).isSameAs (request );
0 commit comments