|
| 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