DefaultCorsProcessorTests.java 17.6 KB
Newer Older
S
Sebastien Deleuze 已提交
1
/*
2
 * Copyright 2002-2018 the original author or authors.
S
Sebastien Deleuze 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.cors.reactive;

import org.junit.Before;
import org.junit.Test;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
25
import org.springframework.http.server.reactive.ServerHttpResponse;
S
Sebastien Deleuze 已提交
26
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
27
import org.springframework.mock.web.test.server.MockServerWebExchange;
S
Sebastien Deleuze 已提交
28
import org.springframework.web.cors.CorsConfiguration;
29
import org.springframework.web.server.ServerWebExchange;
S
Sebastien Deleuze 已提交
30

31
import static org.hamcrest.Matchers.contains;
32 33 34
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
35
import static org.junit.Assert.assertThat;
36 37 38 39 40
import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS;
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS;
import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD;
41 42
import static org.springframework.http.HttpHeaders.ORIGIN;
import static org.springframework.http.HttpHeaders.VARY;
R
Rossen Stoyanchev 已提交
43

S
Sebastien Deleuze 已提交
44
/**
R
Rossen Stoyanchev 已提交
45
 * {@link DefaultCorsProcessor} tests with simple or pre-flight CORS request.
S
Sebastien Deleuze 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 *
 * @author Sebastien Deleuze
 */
public class DefaultCorsProcessorTests {

	private DefaultCorsProcessor processor;

	private CorsConfiguration conf;


	@Before
	public void setup() {
		this.conf = new CorsConfiguration();
		this.processor = new DefaultCorsProcessor();
	}


	@Test
	public void actualRequestWithOriginHeader() throws Exception {
65
		ServerWebExchange exchange = actualRequest();
66
		this.processor.process(this.conf, exchange);
67 68 69

		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
70 71
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
72
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
73 74 75 76
	}

	@Test
	public void actualRequestWithOriginHeaderAndNullConfig() throws Exception {
77
		ServerWebExchange exchange = actualRequest();
78
		this.processor.process(null, exchange);
79 80 81 82

		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
83 84 85 86
	}

	@Test
	public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception {
87
		ServerWebExchange exchange = actualRequest();
S
Sebastien Deleuze 已提交
88
		this.conf.addAllowedOrigin("*");
89
		this.processor.process(this.conf, exchange);
90 91 92 93 94 95

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
		assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
96 97
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
98
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
99 100 101 102
	}

	@Test
	public void actualRequestCredentials() throws Exception {
103
		ServerWebExchange exchange = actualRequest();
S
Spring Operator 已提交
104 105
		this.conf.addAllowedOrigin("https://domain1.com");
		this.conf.addAllowedOrigin("https://domain2.com");
S
Sebastien Deleuze 已提交
106 107
		this.conf.addAllowedOrigin("http://domain3.com");
		this.conf.setAllowCredentials(true);
108
		this.processor.process(this.conf, exchange);
109 110 111

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
S
Spring Operator 已提交
112
		assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
113 114
		assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
		assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
115 116
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
117
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
118 119 120 121
	}

	@Test
	public void actualRequestCredentialsWithOriginWildcard() throws Exception {
122
		ServerWebExchange exchange = actualRequest();
S
Sebastien Deleuze 已提交
123 124
		this.conf.addAllowedOrigin("*");
		this.conf.setAllowCredentials(true);
125
		this.processor.process(this.conf, exchange);
126 127 128

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
S
Spring Operator 已提交
129
		assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
130 131
		assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
		assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
132 133
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
134
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
135 136 137 138
	}

	@Test
	public void actualRequestCaseInsensitiveOriginMatch() throws Exception {
139
		ServerWebExchange exchange = actualRequest();
S
Spring Operator 已提交
140
		this.conf.addAllowedOrigin("https://DOMAIN2.com");
141
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
142

143 144
		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
145 146
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
147
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
148 149 150 151
	}

	@Test
	public void actualRequestExposedHeaders() throws Exception {
152
		ServerWebExchange exchange = actualRequest();
S
Sebastien Deleuze 已提交
153 154
		this.conf.addExposedHeader("header1");
		this.conf.addExposedHeader("header2");
S
Spring Operator 已提交
155
		this.conf.addAllowedOrigin("https://domain2.com");
156
		this.processor.process(this.conf, exchange);
157 158 159

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
S
Spring Operator 已提交
160
		assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
161 162 163
		assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
		assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header1"));
		assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header2"));
164 165
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
166
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
167 168 169 170
	}

	@Test
	public void preflightRequestAllOriginsAllowed() throws Exception {
171 172
		ServerWebExchange exchange = MockServerWebExchange.from(
				preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET"));
S
Sebastien Deleuze 已提交
173
		this.conf.addAllowedOrigin("*");
174
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
175

176
		ServerHttpResponse response = exchange.getResponse();
177 178
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
179
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
180 181
	}

182

S
Sebastien Deleuze 已提交
183 184
	@Test
	public void preflightRequestWrongAllowedMethod() throws Exception {
185 186
		ServerWebExchange exchange = MockServerWebExchange.from(
				preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE"));
S
Sebastien Deleuze 已提交
187
		this.conf.addAllowedOrigin("*");
188
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
189

190
		ServerHttpResponse response = exchange.getResponse();
191 192
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
193
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
194 195 196 197
	}

	@Test
	public void preflightRequestMatchedAllowedMethod() throws Exception {
198 199
		ServerWebExchange exchange = MockServerWebExchange.from(
				preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET"));
S
Sebastien Deleuze 已提交
200
		this.conf.addAllowedOrigin("*");
201
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
202

203 204
		ServerHttpResponse response = exchange.getResponse();
		assertNull(response.getStatusCode());
205 206
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
207
		assertEquals("GET,HEAD", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
S
Sebastien Deleuze 已提交
208 209 210 211
	}

	@Test
	public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Exception {
212
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest());
213
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
214

215 216
		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
217 218
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
219
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
220 221 222 223
	}

	@Test
	public void preflightRequestWithoutRequestMethod() throws Exception {
224 225
		ServerWebExchange exchange = MockServerWebExchange.from(
				preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"));
226
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
227

228 229
		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
230 231
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
232
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
233 234 235 236
	}

	@Test
	public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Exception {
237
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
238
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
239
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"));
240

241
		this.processor.process(this.conf, exchange);
S
Sebastien Deleuze 已提交
242

243 244
		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
245 246
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
247
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
248 249 250 251
	}

	@Test
	public void preflightRequestValidRequestAndConfig() throws Exception {
252
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
253
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
254
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"));
255

S
Sebastien Deleuze 已提交
256 257 258 259 260 261
		this.conf.addAllowedOrigin("*");
		this.conf.addAllowedMethod("GET");
		this.conf.addAllowedMethod("PUT");
		this.conf.addAllowedHeader("header1");
		this.conf.addAllowedHeader("header2");

262
		this.processor.process(this.conf, exchange);
263 264 265 266 267 268 269

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
		assertEquals("GET,PUT", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
		assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
270 271
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
272
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
273 274 275 276
	}

	@Test
	public void preflightRequestCredentials() throws Exception {
277
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
278
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
279
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"));
280

S
Spring Operator 已提交
281 282
		this.conf.addAllowedOrigin("https://domain1.com");
		this.conf.addAllowedOrigin("https://domain2.com");
S
Sebastien Deleuze 已提交
283 284 285 286
		this.conf.addAllowedOrigin("http://domain3.com");
		this.conf.addAllowedHeader("Header1");
		this.conf.setAllowCredentials(true);

287
		this.processor.process(this.conf, exchange);
288 289 290

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
S
Spring Operator 已提交
291
		assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
292 293
		assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
		assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
294 295
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
296
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
297 298 299 300
	}

	@Test
	public void preflightRequestCredentialsWithOriginWildcard() throws Exception {
301
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
302
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
303
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"));
304

S
Spring Operator 已提交
305
		this.conf.addAllowedOrigin("https://domain1.com");
S
Sebastien Deleuze 已提交
306 307 308 309 310
		this.conf.addAllowedOrigin("*");
		this.conf.addAllowedOrigin("http://domain3.com");
		this.conf.addAllowedHeader("Header1");
		this.conf.setAllowCredentials(true);

311
		this.processor.process(this.conf, exchange);
312 313 314

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
S
Spring Operator 已提交
315
		assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
316 317
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
318
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
319 320 321 322
	}

	@Test
	public void preflightRequestAllowedHeaders() throws Exception {
323
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
324
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
325
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2"));
326

S
Sebastien Deleuze 已提交
327 328 329
		this.conf.addAllowedHeader("Header1");
		this.conf.addAllowedHeader("Header2");
		this.conf.addAllowedHeader("Header3");
S
Spring Operator 已提交
330
		this.conf.addAllowedOrigin("https://domain2.com");
S
Sebastien Deleuze 已提交
331

332
		this.processor.process(this.conf, exchange);
333 334 335 336 337 338 339

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
		assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
		assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
		assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header3"));
340 341
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
342
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
343 344 345 346
	}

	@Test
	public void preflightRequestAllowsAllHeaders() throws Exception {
347
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
348
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
349
				.header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2"));
350

S
Sebastien Deleuze 已提交
351
		this.conf.addAllowedHeader("*");
S
Spring Operator 已提交
352
		this.conf.addAllowedOrigin("https://domain2.com");
S
Sebastien Deleuze 已提交
353

354
		this.processor.process(this.conf, exchange);
355 356 357 358 359 360 361

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
		assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
		assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
		assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("*"));
362 363
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
364
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
365 366 367 368
	}

	@Test
	public void preflightRequestWithEmptyHeaders() throws Exception {
369
		ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()
370
				.header(ACCESS_CONTROL_REQUEST_METHOD, "GET")
371
				.header(ACCESS_CONTROL_REQUEST_HEADERS, ""));
372

S
Sebastien Deleuze 已提交
373
		this.conf.addAllowedHeader("*");
S
Spring Operator 已提交
374
		this.conf.addAllowedOrigin("https://domain2.com");
S
Sebastien Deleuze 已提交
375

376
		this.processor.process(this.conf, exchange);
377 378 379 380

		ServerHttpResponse response = exchange.getResponse();
		assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
381 382
		assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
				ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
383
		assertNull(response.getStatusCode());
S
Sebastien Deleuze 已提交
384 385 386 387
	}

	@Test
	public void preflightRequestWithNullConfig() throws Exception {
388 389
		ServerWebExchange exchange = MockServerWebExchange.from(
				preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET"));
S
Sebastien Deleuze 已提交
390
		this.conf.addAllowedOrigin("*");
391
		this.processor.process(null, exchange);
S
Sebastien Deleuze 已提交
392

393 394 395
		ServerHttpResponse response = exchange.getResponse();
		assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
S
Sebastien Deleuze 已提交
396 397
	}

398

399
	private ServerWebExchange actualRequest() {
400
		return MockServerWebExchange.from(corsRequest(HttpMethod.GET));
401 402 403 404 405 406
	}

	private MockServerHttpRequest.BaseBuilder<?> preFlightRequest() {
		return corsRequest(HttpMethod.OPTIONS);
	}

407
	private MockServerHttpRequest.BaseBuilder<?> corsRequest(HttpMethod method) {
408
		return MockServerHttpRequest
409
				.method(method, "http://localhost/test.html")
S
Spring Operator 已提交
410
				.header(HttpHeaders.ORIGIN, "https://domain2.com");
411 412
	}

S
Sebastien Deleuze 已提交
413
}