|
| 1 | +/* |
| 2 | + * Copyright 2016 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 org.springframework.data.rest.webmvc.jpa; |
| 17 | + |
| 18 | +import static org.hamcrest.Matchers.containsString; |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; |
| 27 | +import org.springframework.data.rest.tests.AbstractWebIntegrationTests; |
| 28 | +import org.springframework.data.rest.webmvc.BasePathAwareController; |
| 29 | +import org.springframework.data.rest.webmvc.RepositoryRestController; |
| 30 | +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; |
| 31 | +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; |
| 32 | +import org.springframework.hateoas.Link; |
| 33 | +import org.springframework.http.HttpHeaders; |
| 34 | +import org.springframework.http.MediaType; |
| 35 | +import org.springframework.test.context.ContextConfiguration; |
| 36 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 37 | +import org.springframework.web.bind.annotation.GetMapping; |
| 38 | +import org.springframework.web.bind.annotation.PathVariable; |
| 39 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 40 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 41 | + |
| 42 | +/** |
| 43 | + * Web integration tests specific to Cross-origin resource sharing. |
| 44 | + * |
| 45 | + * @author Mark Paluch |
| 46 | + * @soundtrack 2 Unlimited - No Limit |
| 47 | + */ |
| 48 | +@ContextConfiguration |
| 49 | +public class CorsIntegrationTests extends AbstractWebIntegrationTests { |
| 50 | + |
| 51 | + static class CorsConfig extends JpaRepositoryConfig { |
| 52 | + |
| 53 | + @Bean |
| 54 | + RepositoryRestConfigurer repositoryRestConfigurer() { |
| 55 | + |
| 56 | + return new RepositoryRestConfigurerAdapter() { |
| 57 | + |
| 58 | + @Override |
| 59 | + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { |
| 60 | + |
| 61 | + config.addCorsMapping("/books/**") // |
| 62 | + .allowedMethods("GET", "PUT", "POST") // |
| 63 | + .allowedOrigins("http://far.far.away"); |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @see DATAREST-573 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void appliesSelectiveDefaultCorsConfiguration() throws Exception { |
| 74 | + |
| 75 | + Link findItems = client.discoverUnique("items"); |
| 76 | + |
| 77 | + // Preflight request |
| 78 | + mvc.perform(options(findItems.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.away") |
| 79 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST")) // |
| 80 | + .andExpect(status().isOk()) // |
| 81 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")) // |
| 82 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE")); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @see DATAREST-573 |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void appliesGlobalCorsConfiguration() throws Exception { |
| 90 | + |
| 91 | + Link findBooks = client.discoverUnique("books"); |
| 92 | + |
| 93 | + // Preflight request |
| 94 | + mvc.perform(options(findBooks.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.away") |
| 95 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")) // |
| 96 | + .andExpect(status().isOk()) // |
| 97 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")) // |
| 98 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,PUT,POST")); |
| 99 | + |
| 100 | + // CORS request |
| 101 | + mvc.perform(get(findBooks.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.away")) // |
| 102 | + .andExpect(status().isOk()) // |
| 103 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @see DATAREST-573 |
| 108 | + * @see BooksXmlController |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void appliesCorsConfigurationOnCustomControllers() throws Exception { |
| 112 | + |
| 113 | + // Preflight request |
| 114 | + mvc.perform(options("/books/xml/1234") // |
| 115 | + .header(HttpHeaders.ORIGIN, "http://far.far.away") // |
| 116 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")) // |
| 117 | + .andExpect(status().isOk()) // |
| 118 | + .andExpect(header().longValue(HttpHeaders.ACCESS_CONTROL_MAX_AGE, 77123)) // |
| 119 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")) // |
| 120 | + // See https://jira.spring.io/browse/SPR-14792 |
| 121 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, containsString("GET,PUT,POST"))); |
| 122 | + |
| 123 | + // CORS request |
| 124 | + mvc.perform(get("/books/xml/1234") // |
| 125 | + .header(HttpHeaders.ORIGIN, "http://far.far.away") // |
| 126 | + .accept(MediaType.APPLICATION_XML)) // |
| 127 | + .andExpect(status().isOk()) // |
| 128 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @see DATAREST-573 |
| 133 | + * @see BooksPdfController |
| 134 | + */ |
| 135 | + @Test |
| 136 | + public void appliesCorsConfigurationOnCustomControllerMethod() throws Exception { |
| 137 | + |
| 138 | + // Preflight request |
| 139 | + mvc.perform(options("/books/pdf/1234").header(HttpHeaders.ORIGIN, "http://far.far.away") |
| 140 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")) // |
| 141 | + .andExpect(status().isOk()) // |
| 142 | + .andExpect(header().longValue(HttpHeaders.ACCESS_CONTROL_MAX_AGE, 4711)) // |
| 143 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://far.far.away")) // |
| 144 | + // See https://jira.spring.io/browse/SPR-14792 |
| 145 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, containsString("GET,PUT,POST"))); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @see DATAREST-573 |
| 150 | + */ |
| 151 | + @Test |
| 152 | + public void appliesCorsConfigurationOnRepository() throws Exception { |
| 153 | + |
| 154 | + Link authorsLink = client.discoverUnique("authors"); |
| 155 | + |
| 156 | + // Preflight request |
| 157 | + mvc.perform(options(authorsLink.expand().getHref()).header(HttpHeaders.ORIGIN, "http://not.so.far.away") |
| 158 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")) // |
| 159 | + .andExpect(status().isOk()) // |
| 160 | + .andExpect(header().longValue(HttpHeaders.ACCESS_CONTROL_MAX_AGE, 1234)) // |
| 161 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://not.so.far.away")) // |
| 162 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")) // |
| 163 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,PATCH")); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @see DATAREST-573 |
| 168 | + */ |
| 169 | + @Test |
| 170 | + public void appliesCorsConfigurationOnRepositoryToCustomControllers() throws Exception { |
| 171 | + |
| 172 | + // Preflight request |
| 173 | + mvc.perform(options("/authors/pdf/1234").header(HttpHeaders.ORIGIN, "http://not.so.far.away") |
| 174 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")) // |
| 175 | + .andExpect(status().isOk()) // |
| 176 | + .andExpect(header().longValue(HttpHeaders.ACCESS_CONTROL_MAX_AGE, 1234)) // |
| 177 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "http://not.so.far.away")) // |
| 178 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")) // |
| 179 | + .andExpect(header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,PATCH")); |
| 180 | + } |
| 181 | + |
| 182 | + @RepositoryRestController |
| 183 | + static class AuthorsPdfController { |
| 184 | + |
| 185 | + @RequestMapping(method = RequestMethod.GET, path = "/authors/pdf/1234", produces = MediaType.APPLICATION_PDF_VALUE) |
| 186 | + void authorToPdf() {} |
| 187 | + } |
| 188 | + |
| 189 | + @RepositoryRestController |
| 190 | + static class BooksPdfController { |
| 191 | + |
| 192 | + @RequestMapping(method = RequestMethod.GET, path = "/books/pdf/1234", produces = MediaType.APPLICATION_PDF_VALUE) |
| 193 | + @CrossOrigin(maxAge = 4711) |
| 194 | + void bookToPdf() {} |
| 195 | + } |
| 196 | + |
| 197 | + @BasePathAwareController |
| 198 | + static class BooksXmlController { |
| 199 | + |
| 200 | + @GetMapping(value = "/books/xml/{id}", produces = MediaType.APPLICATION_XML_VALUE) |
| 201 | + @CrossOrigin(maxAge = 77123) |
| 202 | + void bookToXml(@PathVariable String id) {} |
| 203 | + } |
| 204 | +} |
0 commit comments