16
16
17
17
package org .springframework .web .servlet .handler ;
18
18
19
- import static org .junit .Assert .*;
20
-
21
19
import java .io .IOException ;
22
20
import java .util .Collections ;
23
-
24
21
import javax .servlet .ServletException ;
25
22
import javax .servlet .http .HttpServletRequest ;
26
23
import javax .servlet .http .HttpServletResponse ;
32
29
import org .springframework .http .HttpHeaders ;
33
30
import org .springframework .http .HttpStatus ;
34
31
import org .springframework .mock .web .test .MockHttpServletRequest ;
32
+ import org .springframework .util .ObjectUtils ;
35
33
import org .springframework .web .HttpRequestHandler ;
36
34
import org .springframework .web .bind .annotation .RequestMethod ;
37
35
import org .springframework .web .context .support .StaticWebApplicationContext ;
41
39
import org .springframework .web .servlet .HandlerInterceptor ;
42
40
import org .springframework .web .servlet .support .WebContentGenerator ;
43
41
42
+ import static org .junit .Assert .*;
43
+ import static org .mockito .Mockito .*;
44
+
44
45
/**
45
46
* Unit tests for CORS-related handling in {@link AbstractHandlerMapping}.
46
47
* @author Sebastien Deleuze
@@ -57,6 +58,7 @@ public class CorsAbstractHandlerMappingTests {
57
58
public void setup () {
58
59
StaticWebApplicationContext context = new StaticWebApplicationContext ();
59
60
this .handlerMapping = new TestHandlerMapping ();
61
+ this .handlerMapping .setInterceptors (mock (HandlerInterceptor .class ));
60
62
this .handlerMapping .setApplicationContext (context );
61
63
this .request = new MockHttpServletRequest ();
62
64
this .request .setRemoteHost ("domain1.com" );
@@ -69,6 +71,7 @@ public void actualRequestWithoutCorsConfigurationProvider() throws Exception {
69
71
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
70
72
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
71
73
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
74
+
72
75
assertNotNull (chain );
73
76
assertTrue (chain .getHandler () instanceof SimpleHandler );
74
77
}
@@ -80,6 +83,7 @@ public void preflightRequestWithoutCorsConfigurationProvider() throws Exception
80
83
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
81
84
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
82
85
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
86
+
83
87
assertNotNull (chain );
84
88
assertTrue (chain .getHandler () instanceof SimpleHandler );
85
89
}
@@ -91,11 +95,10 @@ public void actualRequestWithCorsConfigurationProvider() throws Exception {
91
95
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
92
96
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
93
97
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
98
+
94
99
assertNotNull (chain );
95
100
assertTrue (chain .getHandler () instanceof CorsAwareHandler );
96
- CorsConfiguration config = getCorsConfiguration (chain , false );
97
- assertNotNull (config );
98
- assertArrayEquals (config .getAllowedOrigins ().toArray (), new String []{"*" });
101
+ assertEquals (Collections .singletonList ("*" ), getRequiredCorsConfiguration (chain , false ).getAllowedOrigins ());
99
102
}
100
103
101
104
@ Test
@@ -105,12 +108,11 @@ public void preflightRequestWithCorsConfigurationProvider() throws Exception {
105
108
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
106
109
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
107
110
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
111
+
108
112
assertNotNull (chain );
109
113
assertNotNull (chain .getHandler ());
110
- assertTrue (chain .getHandler ().getClass ().getSimpleName ().equals ("PreFlightHandler" ));
111
- CorsConfiguration config = getCorsConfiguration (chain , true );
112
- assertNotNull (config );
113
- assertArrayEquals (config .getAllowedOrigins ().toArray (), new String []{"*" });
114
+ assertEquals ("PreFlightHandler" , chain .getHandler ().getClass ().getSimpleName ());
115
+ assertEquals (Collections .singletonList ("*" ), getRequiredCorsConfiguration (chain , true ).getAllowedOrigins ());
114
116
}
115
117
116
118
@ Test
@@ -123,11 +125,10 @@ public void actualRequestWithMappedCorsConfiguration() throws Exception {
123
125
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
124
126
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
125
127
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
128
+
126
129
assertNotNull (chain );
127
130
assertTrue (chain .getHandler () instanceof SimpleHandler );
128
- config = getCorsConfiguration (chain , false );
129
- assertNotNull (config );
130
- assertArrayEquals (config .getAllowedOrigins ().toArray (), new String []{"*" });
131
+ assertEquals (Collections .singletonList ("*" ), getRequiredCorsConfiguration (chain , false ).getAllowedOrigins ());
131
132
}
132
133
133
134
@ Test
@@ -140,12 +141,11 @@ public void preflightRequestWithMappedCorsConfiguration() throws Exception {
140
141
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
141
142
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
142
143
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
144
+
143
145
assertNotNull (chain );
144
146
assertNotNull (chain .getHandler ());
145
- assertTrue (chain .getHandler ().getClass ().getSimpleName ().equals ("PreFlightHandler" ));
146
- config = getCorsConfiguration (chain , true );
147
- assertNotNull (config );
148
- assertArrayEquals (config .getAllowedOrigins ().toArray (), new String []{"*" });
147
+ assertEquals ("PreFlightHandler" , chain .getHandler ().getClass ().getSimpleName ());
148
+ assertEquals (Collections .singletonList ("*" ), getRequiredCorsConfiguration (chain , true ).getAllowedOrigins ());
149
149
}
150
150
151
151
@ Test
@@ -156,11 +156,12 @@ public void actualRequestWithCorsConfigurationSource() throws Exception {
156
156
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
157
157
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
158
158
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
159
+
159
160
assertNotNull (chain );
160
161
assertTrue (chain .getHandler () instanceof SimpleHandler );
161
- CorsConfiguration config = getCorsConfiguration (chain , false );
162
+ CorsConfiguration config = getRequiredCorsConfiguration (chain , false );
162
163
assertNotNull (config );
163
- assertArrayEquals ( new String []{ "*" } , config .getAllowedOrigins (). toArray ());
164
+ assertEquals ( Collections . singletonList ( "*" ) , config .getAllowedOrigins ());
164
165
assertEquals (true , config .getAllowCredentials ());
165
166
}
166
167
@@ -172,35 +173,35 @@ public void preflightRequestWithCorsConfigurationSource() throws Exception {
172
173
this .request .addHeader (HttpHeaders .ORIGIN , "https://domain2.com" );
173
174
this .request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
174
175
HandlerExecutionChain chain = handlerMapping .getHandler (this .request );
176
+
175
177
assertNotNull (chain );
176
178
assertNotNull (chain .getHandler ());
177
- assertTrue ( chain .getHandler ().getClass ().getSimpleName (). equals ( "PreFlightHandler" ));
178
- CorsConfiguration config = getCorsConfiguration (chain , true );
179
+ assertEquals ( "PreFlightHandler" , chain .getHandler ().getClass ().getSimpleName ());
180
+ CorsConfiguration config = getRequiredCorsConfiguration (chain , true );
179
181
assertNotNull (config );
180
- assertArrayEquals ( new String []{ "*" } , config .getAllowedOrigins (). toArray ());
182
+ assertEquals ( Collections . singletonList ( "*" ) , config .getAllowedOrigins ());
181
183
assertEquals (true , config .getAllowCredentials ());
182
184
}
183
185
184
186
185
- private CorsConfiguration getCorsConfiguration (HandlerExecutionChain chain , boolean isPreFlightRequest ) {
187
+ @ SuppressWarnings ("ConstantConditions" )
188
+ private CorsConfiguration getRequiredCorsConfiguration (HandlerExecutionChain chain , boolean isPreFlightRequest ) {
189
+ CorsConfiguration corsConfig = null ;
186
190
if (isPreFlightRequest ) {
187
191
Object handler = chain .getHandler ();
188
- assertTrue ( handler .getClass ().getSimpleName (). equals ( "PreFlightHandler" ));
192
+ assertEquals ( "PreFlightHandler" , handler .getClass ().getSimpleName ());
189
193
DirectFieldAccessor accessor = new DirectFieldAccessor (handler );
190
- return (CorsConfiguration )accessor .getPropertyValue ("config" );
194
+ corsConfig = (CorsConfiguration ) accessor .getPropertyValue ("config" );
191
195
}
192
196
else {
193
197
HandlerInterceptor [] interceptors = chain .getInterceptors ();
194
- if (interceptors != null ) {
195
- for (HandlerInterceptor interceptor : interceptors ) {
196
- if (interceptor .getClass ().getSimpleName ().equals ("CorsInterceptor" )) {
197
- DirectFieldAccessor accessor = new DirectFieldAccessor (interceptor );
198
- return (CorsConfiguration ) accessor .getPropertyValue ("config" );
199
- }
200
- }
198
+ if (!ObjectUtils .isEmpty (interceptors )) {
199
+ DirectFieldAccessor accessor = new DirectFieldAccessor (interceptors [0 ]);
200
+ corsConfig = (CorsConfiguration ) accessor .getPropertyValue ("config" );
201
201
}
202
202
}
203
- return null ;
203
+ assertNotNull (corsConfig );
204
+ return corsConfig ;
204
205
}
205
206
206
207
public class TestHandlerMapping extends AbstractHandlerMapping {
0 commit comments