1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 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.
26
26
import org .springframework .mock .web .test .MockHttpServletRequest ;
27
27
import org .springframework .mock .web .test .MockHttpServletResponse ;
28
28
29
+ import static org .hamcrest .Matchers .contains ;
29
30
import static org .junit .Assert .*;
30
31
31
32
/**
@@ -65,7 +66,8 @@ public void actualRequestWithOriginHeader() throws Exception {
65
66
66
67
this .processor .processRequest (this .conf , this .request , this .response );
67
68
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
68
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
69
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
70
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
69
71
assertEquals (HttpServletResponse .SC_FORBIDDEN , this .response .getStatus ());
70
72
}
71
73
@@ -90,7 +92,8 @@ public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception {
90
92
assertEquals ("*" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
91
93
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_MAX_AGE ));
92
94
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_EXPOSE_HEADERS ));
93
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
95
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
96
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
94
97
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
95
98
}
96
99
@@ -108,7 +111,8 @@ public void actualRequestCredentials() throws Exception {
108
111
assertEquals ("http://domain2.com" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
109
112
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
110
113
assertEquals ("true" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
111
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
114
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
115
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
112
116
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
113
117
}
114
118
@@ -124,7 +128,8 @@ public void actualRequestCredentialsWithOriginWildcard() throws Exception {
124
128
assertEquals ("http://domain2.com" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
125
129
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
126
130
assertEquals ("true" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
127
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
131
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
132
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
128
133
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
129
134
}
130
135
@@ -136,7 +141,8 @@ public void actualRequestCaseInsensitiveOriginMatch() throws Exception {
136
141
137
142
this .processor .processRequest (this .conf , this .request , this .response );
138
143
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
139
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
144
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
145
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
140
146
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
141
147
}
142
148
@@ -154,7 +160,8 @@ public void actualRequestExposedHeaders() throws Exception {
154
160
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_EXPOSE_HEADERS ));
155
161
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_EXPOSE_HEADERS ).contains ("header1" ));
156
162
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_EXPOSE_HEADERS ).contains ("header2" ));
157
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
163
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
164
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
158
165
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
159
166
}
160
167
@@ -166,7 +173,8 @@ public void preflightRequestAllOriginsAllowed() throws Exception {
166
173
this .conf .addAllowedOrigin ("*" );
167
174
168
175
this .processor .processRequest (this .conf , this .request , this .response );
169
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
176
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
177
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
170
178
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
171
179
}
172
180
@@ -178,7 +186,8 @@ public void preflightRequestWrongAllowedMethod() throws Exception {
178
186
this .conf .addAllowedOrigin ("*" );
179
187
180
188
this .processor .processRequest (this .conf , this .request , this .response );
181
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
189
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
190
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
182
191
assertEquals (HttpServletResponse .SC_FORBIDDEN , this .response .getStatus ());
183
192
}
184
193
@@ -192,7 +201,8 @@ public void preflightRequestMatchedAllowedMethod() throws Exception {
192
201
this .processor .processRequest (this .conf , this .request , this .response );
193
202
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
194
203
assertEquals ("GET,HEAD" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_METHODS ));
195
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
204
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
205
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
196
206
}
197
207
198
208
@ Test
@@ -202,7 +212,8 @@ public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Except
202
212
203
213
this .processor .processRequest (this .conf , this .request , this .response );
204
214
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
205
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
215
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
216
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
206
217
assertEquals (HttpServletResponse .SC_FORBIDDEN , this .response .getStatus ());
207
218
}
208
219
@@ -214,7 +225,8 @@ public void preflightRequestWithoutRequestMethod() throws Exception {
214
225
215
226
this .processor .processRequest (this .conf , this .request , this .response );
216
227
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
217
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
228
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
229
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
218
230
assertEquals (HttpServletResponse .SC_FORBIDDEN , this .response .getStatus ());
219
231
}
220
232
@@ -227,7 +239,8 @@ public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Excep
227
239
228
240
this .processor .processRequest (this .conf , this .request , this .response );
229
241
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
230
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
242
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
243
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
231
244
assertEquals (HttpServletResponse .SC_FORBIDDEN , this .response .getStatus ());
232
245
}
233
246
@@ -249,7 +262,8 @@ public void preflightRequestValidRequestAndConfig() throws Exception {
249
262
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_METHODS ));
250
263
assertEquals ("GET,PUT" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_METHODS ));
251
264
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_MAX_AGE ));
252
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
265
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
266
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
253
267
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
254
268
}
255
269
@@ -270,7 +284,8 @@ public void preflightRequestCredentials() throws Exception {
270
284
assertEquals ("http://domain2.com" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
271
285
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
272
286
assertEquals ("true" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_CREDENTIALS ));
273
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
287
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
288
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
274
289
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
275
290
}
276
291
@@ -289,7 +304,8 @@ public void preflightRequestCredentialsWithOriginWildcard() throws Exception {
289
304
this .processor .processRequest (this .conf , this .request , this .response );
290
305
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
291
306
assertEquals ("http://domain2.com" , this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
292
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
307
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
308
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
293
309
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
294
310
}
295
311
@@ -310,7 +326,8 @@ public void preflightRequestAllowedHeaders() throws Exception {
310
326
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("Header1" ));
311
327
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("Header2" ));
312
328
assertFalse (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("Header3" ));
313
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
329
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
330
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
314
331
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
315
332
}
316
333
@@ -329,7 +346,8 @@ public void preflightRequestAllowsAllHeaders() throws Exception {
329
346
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("Header1" ));
330
347
assertTrue (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("Header2" ));
331
348
assertFalse (this .response .getHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ).contains ("*" ));
332
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
349
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
350
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
333
351
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
334
352
}
335
353
@@ -345,7 +363,8 @@ public void preflightRequestWithEmptyHeaders() throws Exception {
345
363
this .processor .processRequest (this .conf , this .request , this .response );
346
364
assertTrue (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_ORIGIN ));
347
365
assertFalse (this .response .containsHeader (HttpHeaders .ACCESS_CONTROL_ALLOW_HEADERS ));
348
- assertEquals (HttpHeaders .ORIGIN , this .response .getHeader (HttpHeaders .VARY ));
366
+ assertThat (this .response .getHeaders (HttpHeaders .VARY ), contains (HttpHeaders .ORIGIN ,
367
+ HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , HttpHeaders .ACCESS_CONTROL_REQUEST_HEADERS ));
349
368
assertEquals (HttpServletResponse .SC_OK , this .response .getStatus ());
350
369
}
351
370
0 commit comments