diff --git a/config/src/test/java/org/springframework/security/config/http/HttpCorsConfigTests.java b/config/src/test/java/org/springframework/security/config/http/HttpCorsConfigTests.java index 3298e6079c..2f732fa7ad 100644 --- a/config/src/test/java/org/springframework/security/config/http/HttpCorsConfigTests.java +++ b/config/src/test/java/org/springframework/security/config/http/HttpCorsConfigTests.java @@ -18,6 +18,8 @@ package org.springframework.security.config.http; import java.util.Arrays; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -36,6 +38,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.PreFlightRequestHandler; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -115,6 +118,19 @@ public class HttpCorsConfigTests { // @formatter:on } + @Test + public void optionsWhenUsingPreFlightRequestHandlerThenHandlesPreFlightRequest() throws Exception { + this.spring.configLocations(this.xml("WithPreFlightRequestHandler")).autowire(); + // @formatter:off + this.mvc.perform(get("/").with(this.approved())) + .andExpect(header().doesNotExist("X-Pre-Flight")) + .andExpect(status().isIAmATeapot()); + this.mvc.perform(options("/").with(this.preflight())) + .andExpect(status().isOk()) + .andExpect(header().exists("X-Pre-Flight")); + // @formatter:on + } + private String xml(String configName) { return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml"; } @@ -167,4 +183,13 @@ public class HttpCorsConfigTests { } + static class MyPreFlightRequestHandler implements PreFlightRequestHandler { + + @Override + public void handlePreFlight(HttpServletRequest request, HttpServletResponse response) { + response.addHeader("X-Pre-Flight", "Handled"); + } + + } + } diff --git a/config/src/test/resources/org/springframework/security/config/http/HttpCorsConfigTests-WithPreFlightRequestHandler.xml b/config/src/test/resources/org/springframework/security/config/http/HttpCorsConfigTests-WithPreFlightRequestHandler.xml new file mode 100644 index 0000000000..5352651683 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/HttpCorsConfigTests-WithPreFlightRequestHandler.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + +