|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright © 2017 FAIR Data Team |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package org.fairdatateam.fairdatapoint.acceptance.general; |
| 24 | + |
| 25 | +import org.eclipse.jetty.http.HttpHeader; |
| 26 | +import org.fairdatateam.fairdatapoint.Profiles; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 30 | +import org.springframework.boot.test.context.SpringBootTest; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.http.HttpMethod; |
| 33 | +import org.springframework.http.HttpStatus; |
| 34 | +import org.springframework.test.context.ActiveProfiles; |
| 35 | +import org.springframework.test.web.servlet.assertj.MockMvcTester; |
| 36 | +import org.springframework.test.web.servlet.assertj.MvcTestResult; |
| 37 | + |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
| 42 | +/** |
| 43 | + * Verifies Cross-Origin Resource Sharing (CORS) configuration. For more information see |
| 44 | + * <a href="https://docs.spring.io/spring-security/reference/servlet/integrations/cors.html">spring security CORS</a> |
| 45 | + * and <a href="https://docs.spring.io/spring-framework/reference/web/webmvc-cors.html">spring web mvc CORS</a>. |
| 46 | + */ |
| 47 | +@ActiveProfiles(Profiles.TESTING) |
| 48 | +@AutoConfigureMockMvc |
| 49 | +@SpringBootTest |
| 50 | +public class CorsTest { |
| 51 | + |
| 52 | + private final String otherOrigin = "https://other.origin"; |
| 53 | + private final String uri = "/"; |
| 54 | + |
| 55 | + private final MockMvcTester mockMvc; |
| 56 | + |
| 57 | + // todo: do we need to enable the security filter chain as well, to test http.cors()? |
| 58 | + // https://docs.spring.io/spring-security/reference/servlet/test/mockmvc/setup.html |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor |
| 62 | + */ |
| 63 | + @Autowired |
| 64 | + public CorsTest(MockMvcTester mockMvc) { |
| 65 | + this.mockMvc = mockMvc; |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void normalRequestYieldsNoAccessControlHeaders() { |
| 70 | + // request without Origin header |
| 71 | + MvcTestResult testResult = mockMvc.options().uri(uri).exchange(); |
| 72 | + assertThat(testResult).hasStatusOk() |
| 73 | + .doesNotContainHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void corsPreflightRequestFailsIfAccessControlRequestMethodHeaderMissing() { |
| 78 | + // request with Origin header, but without Access-Control-Request-Method header |
| 79 | + MvcTestResult testResult = mockMvc.options() |
| 80 | + .uri(uri) |
| 81 | + .header(HttpHeaders.ORIGIN, otherOrigin) |
| 82 | + .exchange(); |
| 83 | + assertThat(testResult).hasStatus(HttpStatus.FORBIDDEN) |
| 84 | + .hasBodyTextEqualTo("Invalid CORS request"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void corsPreflightRequestYieldsExpectedAccessControlHeaders() { |
| 89 | + // valid CORS request, with both required headers |
| 90 | + MvcTestResult testResult = mockMvc.options() |
| 91 | + .uri(uri) |
| 92 | + // this preflight says: "I plan to make a GET request with this header from this origin." |
| 93 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.GET) |
| 94 | + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, HttpHeaders.AUTHORIZATION) |
| 95 | + .header(HttpHeaders.ORIGIN, otherOrigin) |
| 96 | + .exchange(); |
| 97 | + List.of( |
| 98 | + // note that Access-Control-Allow-Headers is only returned if the request |
| 99 | + // contains Access-Control-Request-Headers |
| 100 | + HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, |
| 101 | + HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, |
| 102 | + HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, |
| 103 | + HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS |
| 104 | + ).forEach(header -> assertThat(testResult).hasStatusOk().containsHeader(header)); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments