#326 - Extend CORS examples.

Add examples for preflight- and rejected requests.
This commit is contained in:
Mark Paluch
2017-11-30 14:19:45 -08:00
parent 674c4e904c
commit b3d8749273
2 changed files with 24 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,10 +20,10 @@ import org.springframework.web.bind.annotation.CrossOrigin;
/**
* Spring Data repository interface to manage {@link Customer} instances.
*
*
* @author Oliver Gierke
* @author Mark Paluch
* @soundtrack The Intersphere - Out of phase (Live at Alte Feuerwache Mannheim)
*/
@CrossOrigin
@CrossOrigin(origins = { "http://localhost", "http://localhost:1234" })
public interface CustomerRepository extends CrudRepository<Customer, Long> {}

View File

@@ -51,6 +51,18 @@ public class CrossOriginIntegrationTests {
this.mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void executePreflightRequest() throws Exception {
String origin = "http://localhost:1234";
URI uri = URI.create("/customers");
mvc.perform(options(uri).header(ORIGIN, origin).header(ACCESS_CONTROL_REQUEST_METHOD, "POST")) //
.andExpect(header().string(ACCESS_CONTROL_ALLOW_ORIGIN, is(origin))) //
.andExpect(header().string(ACCESS_CONTROL_ALLOW_METHODS, containsString("GET"))) //
.andExpect(header().string(ACCESS_CONTROL_ALLOW_METHODS, containsString("POST"))); //
}
@Test
public void executeCrossOriginRequest() throws Exception {
@@ -62,4 +74,13 @@ public class CrossOriginIntegrationTests {
.andExpect(header().string(ACCESS_CONTROL_ALLOW_ORIGIN, is(origin)));
}
@Test
public void rejectCrossOriginRequest() throws Exception {
String origin = "http://foo.bar";
URI uri = URI.create("/customers");
mvc.perform(get(uri).header(ORIGIN, origin)) //
.andExpect(status().isForbidden());
}
}