Skip to content

Commit ea1ec64

Browse files
author
Steve Riesenberg
committed
Fix test failures related to response headers
These tests began failing on snapshots after changes in Spring Framework's `DispatcherServlet` to reset the response on an error. For now, we can have these tests operate with a 200 OK response. An issue was opened in the spring-framework issuer tracker to discuss this and address `CorsFilter` (and any other filter) that writes headers that would be cleared on an error. See spring-projects/spring-framework#31154
1 parent d87d055 commit ea1ec64

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Diff for: config/src/test/java/org/springframework/security/config/annotation/web/configurers/HeadersConfigurerEagerHeadersTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.security.web.SecurityFilterChain;
3232
import org.springframework.security.web.header.HeaderWriterFilter;
3333
import org.springframework.test.web.servlet.MockMvc;
34+
import org.springframework.web.bind.annotation.GetMapping;
35+
import org.springframework.web.bind.annotation.RestController;
3436

3537
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3638
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
@@ -50,7 +52,7 @@ public class HeadersConfigurerEagerHeadersTests {
5052

5153
@Test
5254
public void requestWhenHeadersEagerlyConfiguredThenHeadersAreWritten() throws Exception {
53-
this.spring.register(HeadersAtTheBeginningOfRequestConfig.class).autowire();
55+
this.spring.register(HeadersAtTheBeginningOfRequestConfig.class, HomeController.class).autowire();
5456
this.mvc.perform(get("/").secure(true)).andExpect(header().string("X-Content-Type-Options", "nosniff"))
5557
.andExpect(header().string("X-Frame-Options", "DENY"))
5658
.andExpect(header().string("Strict-Transport-Security", "max-age=31536000 ; includeSubDomains"))
@@ -82,4 +84,14 @@ public HeaderWriterFilter postProcess(HeaderWriterFilter filter) {
8284

8385
}
8486

87+
@RestController
88+
private static class HomeController {
89+
90+
@GetMapping("/")
91+
String ok() {
92+
return "ok";
93+
}
94+
95+
}
96+
8597
}

Diff for: config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void httpBasicWhenInvokedTwiceThenUsesOriginalEntryPoint() throws Excepti
124124
// SEC-3019
125125
@Test
126126
public void httpBasicWhenRememberMeConfiguredThenSetsRememberMeCookie() throws Exception {
127-
this.spring.register(BasicUsesRememberMeConfig.class).autowire();
127+
this.spring.register(BasicUsesRememberMeConfig.class, Home.class).autowire();
128128
MockHttpServletRequestBuilder rememberMeRequest = get("/").with(httpBasic("user", "password"))
129129
.param("remember-me", "true");
130130
this.mvc.perform(rememberMeRequest).andExpect(cookie().exists("remember-me"));

Diff for: config/src/test/kotlin/org/springframework/security/config/annotation/web/CorsDslTests.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import org.springframework.security.config.test.SpringTestContextExtension
3131
import org.springframework.security.web.SecurityFilterChain
3232
import org.springframework.test.web.servlet.MockMvc
3333
import org.springframework.test.web.servlet.get
34+
import org.springframework.web.bind.annotation.GetMapping
3435
import org.springframework.web.bind.annotation.RequestMethod
36+
import org.springframework.web.bind.annotation.RestController
3537
import org.springframework.web.cors.CorsConfiguration
3638
import org.springframework.web.cors.CorsConfigurationSource
3739
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
@@ -72,7 +74,7 @@ class CorsDslTests {
7274

7375
@Test
7476
fun `CORS when CORS configuration source bean then responds with CORS header`() {
75-
this.spring.register(CorsCrossOriginBeanConfig::class.java).autowire()
77+
this.spring.register(CorsCrossOriginBeanConfig::class.java, HomeController::class.java).autowire()
7678

7779
this.mockMvc.get("/")
7880
{
@@ -149,7 +151,7 @@ class CorsDslTests {
149151

150152
@Test
151153
fun `CORS when CORS configuration source dsl then responds with CORS header`() {
152-
this.spring.register(CorsCrossOriginBeanConfig::class.java).autowire()
154+
this.spring.register(CorsCrossOriginBeanConfig::class.java, HomeController::class.java).autowire()
153155

154156
this.mockMvc.get("/")
155157
{
@@ -180,4 +182,13 @@ class CorsDslTests {
180182
return http.build()
181183
}
182184
}
185+
186+
@RestController
187+
private class HomeController {
188+
@GetMapping("/")
189+
fun ok(): String {
190+
return "ok"
191+
}
192+
}
193+
183194
}

0 commit comments

Comments
 (0)