1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .lang .annotation .Target ;
23
23
import java .lang .reflect .Method ;
24
24
import java .util .Arrays ;
25
+ import java .util .Properties ;
25
26
26
27
import org .junit .Before ;
27
28
import org .junit .Rule ;
28
29
import org .junit .Test ;
29
30
import org .junit .rules .ExpectedException ;
30
31
31
32
import org .springframework .beans .DirectFieldAccessor ;
33
+ import org .springframework .context .support .PropertySourcesPlaceholderConfigurer ;
32
34
import org .springframework .core .annotation .AnnotatedElementUtils ;
33
35
import org .springframework .core .annotation .AnnotationUtils ;
36
+ import org .springframework .core .env .PropertiesPropertySource ;
34
37
import org .springframework .http .HttpHeaders ;
35
38
import org .springframework .mock .web .test .MockHttpServletRequest ;
36
39
import org .springframework .stereotype .Controller ;
@@ -72,9 +75,15 @@ public class CrossOriginTests {
72
75
73
76
@ Before
74
77
public void setUp () {
78
+ StaticWebApplicationContext wac = new StaticWebApplicationContext ();
79
+ Properties props = new Properties ();
80
+ props .setProperty ("myOrigin" , "http://example.com" );
81
+ wac .getEnvironment ().getPropertySources ().addFirst (new PropertiesPropertySource ("ps" , props ));
82
+ wac .registerSingleton ("ppc" , PropertySourcesPlaceholderConfigurer .class );
83
+ wac .refresh ();
84
+
75
85
this .handlerMapping .setRemoveSemicolonContent (false );
76
- this .handlerMapping .setApplicationContext (new StaticWebApplicationContext ());
77
- this .handlerMapping .afterPropertiesSet ();
86
+ wac .getAutowireCapableBeanFactory ().initializeBean (this .handlerMapping , "hm" );
78
87
79
88
this .request .setMethod ("GET" );
80
89
this .request .addHeader (HttpHeaders .ORIGIN , "http://domain.com/" );
@@ -112,10 +121,10 @@ public void defaultAnnotation() throws Exception {
112
121
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
113
122
CorsConfiguration config = getCorsConfiguration (chain , false );
114
123
assertNotNull (config );
115
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
116
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
124
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
125
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
117
126
assertTrue (config .getAllowCredentials ());
118
- assertArrayEquals (new String []{"*" }, config .getAllowedHeaders ().toArray ());
127
+ assertArrayEquals (new String [] {"*" }, config .getAllowedHeaders ().toArray ());
119
128
assertTrue (CollectionUtils .isEmpty (config .getExposedHeaders ()));
120
129
assertEquals (new Long (1800 ), config .getMaxAge ());
121
130
}
@@ -127,10 +136,10 @@ public void customized() throws Exception {
127
136
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
128
137
CorsConfiguration config = getCorsConfiguration (chain , false );
129
138
assertNotNull (config );
130
- assertArrayEquals (new String []{"DELETE" }, config .getAllowedMethods ().toArray ());
131
- assertArrayEquals (new String []{"http://site1.com" , "http://site2.com" }, config .getAllowedOrigins ().toArray ());
132
- assertArrayEquals (new String []{"header1" , "header2" }, config .getAllowedHeaders ().toArray ());
133
- assertArrayEquals (new String []{"header3" , "header4" }, config .getExposedHeaders ().toArray ());
139
+ assertArrayEquals (new String [] {"DELETE" }, config .getAllowedMethods ().toArray ());
140
+ assertArrayEquals (new String [] {"http://site1.com" , "http://site2.com" }, config .getAllowedOrigins ().toArray ());
141
+ assertArrayEquals (new String [] {"header1" , "header2" }, config .getAllowedHeaders ().toArray ());
142
+ assertArrayEquals (new String [] {"header3" , "header4" }, config .getExposedHeaders ().toArray ());
134
143
assertEquals (new Long (123 ), config .getMaxAge ());
135
144
assertFalse (config .getAllowCredentials ());
136
145
}
@@ -146,6 +155,17 @@ public void customOriginDefinedViaValueAttribute() throws Exception {
146
155
assertTrue (config .getAllowCredentials ());
147
156
}
148
157
158
+ @ Test
159
+ public void customOriginDefinedViaPlaceholder () throws Exception {
160
+ this .handlerMapping .registerHandler (new MethodLevelController ());
161
+ this .request .setRequestURI ("/someOrigin" );
162
+ HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
163
+ CorsConfiguration config = getCorsConfiguration (chain , false );
164
+ assertNotNull (config );
165
+ assertEquals (Arrays .asList ("http://example.com" ), config .getAllowedOrigins ());
166
+ assertTrue (config .getAllowCredentials ());
167
+ }
168
+
149
169
@ Test
150
170
public void bogusAllowCredentialsValue () throws Exception {
151
171
exception .expect (IllegalStateException .class );
@@ -162,24 +182,24 @@ public void classLevel() throws Exception {
162
182
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
163
183
CorsConfiguration config = getCorsConfiguration (chain , false );
164
184
assertNotNull (config );
165
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
166
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
185
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
186
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
167
187
assertFalse (config .getAllowCredentials ());
168
188
169
189
this .request .setRequestURI ("/bar" );
170
190
chain = this .handlerMapping .getHandler (request );
171
191
config = getCorsConfiguration (chain , false );
172
192
assertNotNull (config );
173
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
174
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
193
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
194
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
175
195
assertFalse (config .getAllowCredentials ());
176
196
177
197
this .request .setRequestURI ("/baz" );
178
198
chain = this .handlerMapping .getHandler (request );
179
199
config = getCorsConfiguration (chain , false );
180
200
assertNotNull (config );
181
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
182
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
201
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
202
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
183
203
assertTrue (config .getAllowCredentials ());
184
204
}
185
205
@@ -191,8 +211,8 @@ public void classLevelComposedAnnotation() throws Exception {
191
211
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
192
212
CorsConfiguration config = getCorsConfiguration (chain , false );
193
213
assertNotNull (config );
194
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
195
- assertArrayEquals (new String []{"http://foo.com" }, config .getAllowedOrigins ().toArray ());
214
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
215
+ assertArrayEquals (new String [] {"http://foo.com" }, config .getAllowedOrigins ().toArray ());
196
216
assertTrue (config .getAllowCredentials ());
197
217
}
198
218
@@ -204,8 +224,8 @@ public void methodLevelComposedAnnotation() throws Exception {
204
224
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
205
225
CorsConfiguration config = getCorsConfiguration (chain , false );
206
226
assertNotNull (config );
207
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
208
- assertArrayEquals (new String []{"http://foo.com" }, config .getAllowedOrigins ().toArray ());
227
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
228
+ assertArrayEquals (new String [] {"http://foo.com" }, config .getAllowedOrigins ().toArray ());
209
229
assertTrue (config .getAllowCredentials ());
210
230
}
211
231
@@ -218,10 +238,10 @@ public void preFlightRequest() throws Exception {
218
238
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
219
239
CorsConfiguration config = getCorsConfiguration (chain , true );
220
240
assertNotNull (config );
221
- assertArrayEquals (new String []{"GET" }, config .getAllowedMethods ().toArray ());
222
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
241
+ assertArrayEquals (new String [] {"GET" }, config .getAllowedMethods ().toArray ());
242
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
223
243
assertTrue (config .getAllowCredentials ());
224
- assertArrayEquals (new String []{"*" }, config .getAllowedHeaders ().toArray ());
244
+ assertArrayEquals (new String [] {"*" }, config .getAllowedHeaders ().toArray ());
225
245
assertTrue (CollectionUtils .isEmpty (config .getExposedHeaders ()));
226
246
assertEquals (new Long (1800 ), config .getMaxAge ());
227
247
}
@@ -236,9 +256,9 @@ public void ambiguousHeaderPreFlightRequest() throws Exception {
236
256
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
237
257
CorsConfiguration config = getCorsConfiguration (chain , true );
238
258
assertNotNull (config );
239
- assertArrayEquals (new String []{"*" }, config .getAllowedMethods ().toArray ());
240
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
241
- assertArrayEquals (new String []{"*" }, config .getAllowedHeaders ().toArray ());
259
+ assertArrayEquals (new String [] {"*" }, config .getAllowedMethods ().toArray ());
260
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
261
+ assertArrayEquals (new String [] {"*" }, config .getAllowedHeaders ().toArray ());
242
262
assertTrue (config .getAllowCredentials ());
243
263
assertTrue (CollectionUtils .isEmpty (config .getExposedHeaders ()));
244
264
assertNull (config .getMaxAge ());
@@ -253,9 +273,9 @@ public void ambiguousProducesPreFlightRequest() throws Exception {
253
273
HandlerExecutionChain chain = this .handlerMapping .getHandler (request );
254
274
CorsConfiguration config = getCorsConfiguration (chain , true );
255
275
assertNotNull (config );
256
- assertArrayEquals (new String []{"*" }, config .getAllowedMethods ().toArray ());
257
- assertArrayEquals (new String []{"*" }, config .getAllowedOrigins ().toArray ());
258
- assertArrayEquals (new String []{"*" }, config .getAllowedHeaders ().toArray ());
276
+ assertArrayEquals (new String [] {"*" }, config .getAllowedMethods ().toArray ());
277
+ assertArrayEquals (new String [] {"*" }, config .getAllowedOrigins ().toArray ());
278
+ assertArrayEquals (new String [] {"*" }, config .getAllowedHeaders ().toArray ());
259
279
assertTrue (config .getAllowCredentials ());
260
280
assertTrue (CollectionUtils .isEmpty (config .getExposedHeaders ()));
261
281
assertNull (config .getMaxAge ());
@@ -343,8 +363,14 @@ public void customized() {
343
363
@ RequestMapping ("/customOrigin" )
344
364
public void customOriginDefinedViaValueAttribute () {
345
365
}
366
+
367
+ @ CrossOrigin ("${myOrigin}" )
368
+ @ RequestMapping ("/someOrigin" )
369
+ public void customOriginDefinedViaPlaceholder () {
370
+ }
346
371
}
347
372
373
+
348
374
@ Controller
349
375
private static class MethodLevelControllerWithBogusAllowCredentialsValue {
350
376
@@ -354,6 +380,7 @@ public void bogusAllowCredentialsValue() {
354
380
}
355
381
}
356
382
383
+
357
384
@ Controller
358
385
@ CrossOrigin (allowCredentials = "false" )
359
386
private static class ClassLevelController {
@@ -374,14 +401,18 @@ public void baz() {
374
401
375
402
}
376
403
404
+
377
405
@ Target ({ElementType .METHOD , ElementType .TYPE })
378
406
@ Retention (RetentionPolicy .RUNTIME )
379
407
@ CrossOrigin
380
408
private @interface ComposedCrossOrigin {
409
+
381
410
String [] origins () default {};
411
+
382
412
String allowCredentials () default "" ;
383
413
}
384
414
415
+
385
416
@ Controller
386
417
@ ComposedCrossOrigin (origins = "http://foo.com" , allowCredentials = "true" )
387
418
private static class ClassLevelMappingWithComposedAnnotation {
@@ -401,6 +432,7 @@ public void foo() {
401
432
}
402
433
}
403
434
435
+
404
436
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingHandlerMapping {
405
437
406
438
public void registerHandler (Object handler ) {
0 commit comments