Skip to content

Commit 605b90d

Browse files
mp911deodrotbohm
authored andcommitted
spring-projects#249 - Add example for Spring Data REST Cross-origin resource sharing.
1 parent 319d3e1 commit 605b90d

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

rest/headers/README.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ HTTP/1.1 304 Not Modified
3838
----
3939
====
4040

41+
== Cross-Origin Resource Sharing
42+
43+
Client-side JavaScript that issue cross-origin requests require the server to evaluate cross-origin requests and respond appropriately.
44+
45+
.Cross-Origin Request
46+
====
47+
[source,bash]
48+
----
49+
$ curl 'http://localhost:8080/customers/' -i -H 'Origin: http://localhost'
50+
----
51+
52+
[source,http]
53+
----
54+
HTTP/1.1 200 OK
55+
Access-Control-Allow-Origin: http://localhost
56+
Vary: Origin
57+
Access-Control-Allow-Credentials: true
58+
----
59+
====
60+
4161
== Spring RESTDocs
4262

4363
The sample uses https://github.com/wilkinsona/spring-restdocs[Spring RESTDocs] to document the HTTP interaction implemented using the test cases. See `WebIntegrationTests.setUp()` for general setup and the individual test methods with their usage of `….andDo(document(…))`.

rest/headers/src/main/java/example/springdata/rest/headers/CustomerRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package example.springdata.rest.headers;
1717

1818
import org.springframework.data.repository.CrudRepository;
19+
import org.springframework.web.bind.annotation.CrossOrigin;
1920

2021
/**
2122
* Spring Data repository interface to manage {@link Customer} instances.
2223
*
2324
* @author Oliver Gierke
25+
* @author Mark Paluch
2426
* @soundtrack The Intersphere - Out of phase (Live at Alte Feuerwache Mannheim)
2527
*/
28+
@CrossOrigin
2629
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.rest.headers;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.springframework.http.HttpHeaders.*;
20+
import static org.springframework.restdocs.RestDocumentation.*;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
23+
24+
import java.net.URI;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.mock.web.MockHttpServletResponse;
32+
import org.springframework.restdocs.config.RestDocumentationConfigurer;
33+
import org.springframework.test.context.junit4.SpringRunner;
34+
import org.springframework.test.web.servlet.MockMvc;
35+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36+
import org.springframework.web.context.WebApplicationContext;
37+
38+
/**
39+
* Integration test for Cross-origin resource sharing.
40+
*
41+
* @author Mark Paluch
42+
*/
43+
@RunWith(SpringRunner.class)
44+
@SpringBootTest
45+
public class CrossOriginIntegrationTests {
46+
47+
@Autowired WebApplicationContext context;
48+
@Autowired CustomerRepository customers;
49+
50+
MockMvc mvc;
51+
52+
@Before
53+
public void setUp() {
54+
55+
this.mvc = MockMvcBuilders.webAppContextSetup(context).//
56+
apply(new RestDocumentationConfigurer()).//
57+
build();
58+
}
59+
60+
@Test
61+
public void executeCrossOriginRequest() throws Exception {
62+
63+
String origin = "http://localhost";
64+
URI uri = URI.create("/customers");
65+
66+
MockHttpServletResponse response = mvc.perform(get(uri).header(ORIGIN, origin)).//
67+
andExpect(header().string(ACCESS_CONTROL_ALLOW_CREDENTIALS, is("true"))).//
68+
andExpect(header().string(ACCESS_CONTROL_ALLOW_ORIGIN, is(origin))).//
69+
andDo(document("cors")).//
70+
andReturn().getResponse();
71+
}
72+
}

0 commit comments

Comments
 (0)