|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + * https://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 org.springframework.data.rest.webmvc.jpa; |
| 17 | + |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.data.rest.tests.AbstractWebIntegrationTests; |
| 24 | +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; |
| 25 | +import org.springframework.hateoas.Link; |
| 26 | +import org.springframework.hateoas.LinkRelation; |
| 27 | +import org.springframework.http.HttpHeaders; |
| 28 | +import org.springframework.test.context.ContextConfiguration; |
| 29 | + |
| 30 | +/** |
| 31 | + * Web integration tests specific to Cross-origin resource sharing. |
| 32 | + * |
| 33 | + * @author Mark Paluch |
| 34 | + * @soundtrack RFLKTD - Liquid Crystals |
| 35 | + */ |
| 36 | +@ContextConfiguration |
| 37 | +public class LocalConfigCorsIntegrationTests extends AbstractWebIntegrationTests { |
| 38 | + |
| 39 | + static class CorsConfig extends JpaRepositoryConfig { |
| 40 | + |
| 41 | + @Bean |
| 42 | + RepositoryRestConfigurer repositoryRestConfigurer() { |
| 43 | + return RepositoryRestConfigurer.withConfig(c -> {}); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @see ItemRepository |
| 49 | + */ |
| 50 | + @Test // DATAREST-1397 |
| 51 | + public void appliesRepositoryCorsConfiguration() throws Exception { |
| 52 | + |
| 53 | + Link findItems = client.discoverUnique(LinkRelation.of("items")); |
| 54 | + |
| 55 | + // Preflight request |
| 56 | + mvc.perform(options(findItems.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.example") |
| 57 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST")) // |
| 58 | + .andExpect(status().isOk()) // |
| 59 | + .andExpect( |
| 60 | + header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE")); |
| 61 | + } |
| 62 | +} |
0 commit comments