15
15
*/
16
16
package org .springframework .web .filter ;
17
17
18
+ import java .io .IOException ;
18
19
import javax .servlet .ServletException ;
19
20
import javax .servlet .http .HttpServlet ;
20
21
import javax .servlet .http .HttpServletRequest ;
21
22
23
+ import org .junit .Before ;
22
24
import org .junit .Test ;
23
25
24
26
import org .springframework .mock .web .test .MockFilterChain ;
@@ -38,6 +40,98 @@ public class ForwardedHeaderFilterTests {
38
40
39
41
private final ForwardedHeaderFilter filter = new ForwardedHeaderFilter ();
40
42
43
+ private MockHttpServletRequest request ;
44
+
45
+ private MockFilterChain filterChain ;
46
+
47
+
48
+ @ Before
49
+ public void setUp () throws Exception {
50
+ this .request = new MockHttpServletRequest ();
51
+ this .request .setScheme ("http" );
52
+ this .request .setServerName ("localhost" );
53
+ this .request .setServerPort (80 );
54
+ this .filterChain = new MockFilterChain (new HttpServlet () {});
55
+ }
56
+
57
+
58
+ @ Test (expected = IllegalArgumentException .class )
59
+ public void contextPathNull () {
60
+ this .filter .setContextPath (null );
61
+ }
62
+
63
+ @ Test
64
+ public void contextPathEmpty () throws Exception {
65
+ this .filter .setContextPath ("" );
66
+ assertEquals ("" , filterAndGetContextPath ());
67
+ }
68
+
69
+ @ Test
70
+ public void contextPathWithExtraSpaces () throws Exception {
71
+ this .filter .setContextPath (" /foo " );
72
+ assertEquals ("/foo" , filterAndGetContextPath ());
73
+ }
74
+
75
+ @ Test
76
+ public void contextPathWithNoLeadingSlash () throws Exception {
77
+ this .filter .setContextPath ("foo" );
78
+ assertEquals ("/foo" , filterAndGetContextPath ());
79
+ }
80
+
81
+ @ Test
82
+ public void contextPathWithTrailingSlash () throws Exception {
83
+ this .filter .setContextPath ("/foo/bar/" );
84
+ assertEquals ("/foo/bar" , filterAndGetContextPath ());
85
+ }
86
+
87
+ @ Test
88
+ public void contextPathWithTrailingSlashes () throws Exception {
89
+ this .filter .setContextPath ("/foo/bar/baz///" );
90
+ assertEquals ("/foo/bar/baz" , filterAndGetContextPath ());
91
+ }
92
+
93
+ @ Test
94
+ public void requestUri () throws Exception {
95
+ this .filter .setContextPath ("/" );
96
+ this .request .setContextPath ("/app" );
97
+ this .request .setRequestURI ("/app/path" );
98
+ HttpServletRequest actual = filterAndGetWrappedRequest ();
99
+
100
+ assertEquals ("/" , actual .getContextPath ());
101
+ assertEquals ("/path" , actual .getRequestURI ());
102
+ }
103
+
104
+ @ Test
105
+ public void requestUriWithTrailingSlash () throws Exception {
106
+ this .filter .setContextPath ("/" );
107
+ this .request .setContextPath ("/app" );
108
+ this .request .setRequestURI ("/app/path/" );
109
+ HttpServletRequest actual = filterAndGetWrappedRequest ();
110
+
111
+ assertEquals ("/" , actual .getContextPath ());
112
+ assertEquals ("/path/" , actual .getRequestURI ());
113
+ }
114
+ @ Test
115
+ public void requestUriEqualsContextPath () throws Exception {
116
+ this .filter .setContextPath ("/" );
117
+ this .request .setContextPath ("/app" );
118
+ this .request .setRequestURI ("/app" );
119
+ HttpServletRequest actual = filterAndGetWrappedRequest ();
120
+
121
+ assertEquals ("/" , actual .getContextPath ());
122
+ assertEquals ("/" , actual .getRequestURI ());
123
+ }
124
+
125
+ @ Test
126
+ public void requestUriRootUrl () throws Exception {
127
+ this .filter .setContextPath ("/" );
128
+ this .request .setContextPath ("/app" );
129
+ this .request .setRequestURI ("/app/" );
130
+ HttpServletRequest actual = filterAndGetWrappedRequest ();
131
+
132
+ assertEquals ("/" , actual .getContextPath ());
133
+ assertEquals ("/" , actual .getRequestURI ());
134
+ }
41
135
42
136
@ Test
43
137
public void shouldFilter () throws Exception {
@@ -54,19 +148,14 @@ public void shouldNotFilter() throws Exception {
54
148
55
149
@ Test
56
150
public void forwardedRequest () throws Exception {
57
- MockHttpServletRequest request = new MockHttpServletRequest ();
58
- request .setScheme ("http" );
59
- request .setServerName ("localhost" );
60
- request .setServerPort (80 );
61
- request .setRequestURI ("/mvc-showcase" );
62
- request .addHeader ("X-Forwarded-Proto" , "https" );
63
- request .addHeader ("X-Forwarded-Host" , "84.198.58.199" );
64
- request .addHeader ("X-Forwarded-Port" , "443" );
65
- request .addHeader ("foo" , "bar" );
66
-
67
- MockFilterChain chain = new MockFilterChain (new HttpServlet () {});
68
- this .filter .doFilter (request , new MockHttpServletResponse (), chain );
69
- HttpServletRequest actual = (HttpServletRequest ) chain .getRequest ();
151
+ this .request .setRequestURI ("/mvc-showcase" );
152
+ this .request .addHeader ("X-Forwarded-Proto" , "https" );
153
+ this .request .addHeader ("X-Forwarded-Host" , "84.198.58.199" );
154
+ this .request .addHeader ("X-Forwarded-Port" , "443" );
155
+ this .request .addHeader ("foo" , "bar" );
156
+
157
+ this .filter .doFilter (this .request , new MockHttpServletResponse (), this .filterChain );
158
+ HttpServletRequest actual = (HttpServletRequest ) this .filterChain .getRequest ();
70
159
71
160
assertEquals ("https://84.198.58.199/mvc-showcase" , actual .getRequestURL ().toString ());
72
161
assertEquals ("https" , actual .getScheme ());
@@ -81,11 +170,20 @@ public void forwardedRequest() throws Exception {
81
170
}
82
171
83
172
173
+ private String filterAndGetContextPath () throws ServletException , IOException {
174
+ return filterAndGetWrappedRequest ().getContextPath ();
175
+ }
176
+
177
+ private HttpServletRequest filterAndGetWrappedRequest () throws ServletException , IOException {
178
+ MockHttpServletResponse response = new MockHttpServletResponse ();
179
+ this .filter .doFilterInternal (this .request , response , this .filterChain );
180
+ return (HttpServletRequest ) this .filterChain .getRequest ();
181
+ }
182
+
84
183
private void testShouldFilter (String headerName ) throws ServletException {
85
184
MockHttpServletRequest request = new MockHttpServletRequest ();
86
185
request .addHeader (headerName , "1" );
87
186
assertFalse (this .filter .shouldNotFilter (request ));
88
187
}
89
188
90
-
91
189
}
0 commit comments