From 7a204a5f58fb63e8350136b9033494e46054dfbf Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Tue, 27 Mar 2018 21:46:04 -0500 Subject: [PATCH] Fixes for SPR-16624 Fixes: gh-5164 --- .../configurers/LogoutConfigurerTests.groovy | 3 ++- .../samples/config/SecurityConfigTests.java | 21 ++++--------------- .../samples/config/SecurityConfigTests.java | 17 +++++++-------- .../SecurityMockMvcRequestBuilders.java | 1 + .../showcase/login/AuthenticationTests.java | 6 +++++- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.groovy b/config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.groovy index 69168d509a..bfee75539c 100644 --- a/config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.groovy +++ b/config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.groovy @@ -186,13 +186,14 @@ class LogoutConfigurerTests extends BaseSpringSpec { } } - def "LogoutConfigurer content negotiation default redirects"() { + def "LogoutConfigurer content negotiation text/html redirects"() { setup: loadConfig(LogoutHandlerContentNegotiation) when: login() request.method = 'POST' request.servletPath = '/logout' + request.addHeader('Accept', 'text/html') springSecurityFilterChain.doFilter(request,response,chain) then: response.status == 302 diff --git a/samples/javaconfig/hellomvc/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java b/samples/javaconfig/hellomvc/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java index b5c3773404..f798b89f3d 100644 --- a/samples/javaconfig/hellomvc/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java +++ b/samples/javaconfig/hellomvc/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java @@ -18,25 +18,19 @@ package org.springframework.security.samples.config; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import javax.servlet.Filter; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.security.samples.mvc.config.WebMvcConfiguration; import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; -import org.springframework.test.context.support.DirtiesContextTestExecutionListener; -import org.springframework.test.context.transaction.TransactionalTestExecutionListener; -import org.springframework.test.context.web.ServletTestExecutionListener; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -50,25 +44,18 @@ import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { RootConfiguration.class, WebMvcConfiguration.class }) @WebAppConfiguration -@TestExecutionListeners(listeners = { ServletTestExecutionListener.class, - DependencyInjectionTestExecutionListener.class, - DirtiesContextTestExecutionListener.class, - TransactionalTestExecutionListener.class, - WithSecurityContextTestExecutionListener.class }) public class SecurityConfigTests { private MockMvc mvc; @Autowired private WebApplicationContext context; - @Autowired - private Filter springSecurityFilterChain; @Before public void setup() { mvc = MockMvcBuilders.webAppContextSetup(context) - .addFilters(springSecurityFilterChain) - .defaultRequest(get("/").with(testSecurityContext())).build(); + .apply(springSecurity()) + .defaultRequest(get("/").accept(MediaType.TEXT_HTML)).build(); } @Test diff --git a/samples/javaconfig/inmemory/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java b/samples/javaconfig/inmemory/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java index 535a7cb06b..15cf016344 100644 --- a/samples/javaconfig/inmemory/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java +++ b/samples/javaconfig/inmemory/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java @@ -16,17 +16,16 @@ package org.springframework.security.samples.config; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import javax.servlet.Filter; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.security.samples.mvc.config.WebMvcConfiguration; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; @@ -49,14 +48,11 @@ public class SecurityConfigTests { @Autowired private WebApplicationContext context; - @Autowired - private Filter springSecurityFilterChain; - @Before public void setup() { mvc = MockMvcBuilders.webAppContextSetup(context) - .addFilters(springSecurityFilterChain) - .defaultRequest(get("/").with(testSecurityContext())).build(); + .apply(springSecurity()) + .defaultRequest(get("/").accept(MediaType.TEXT_HTML)).build(); } @Test @@ -84,7 +80,8 @@ public class SecurityConfigTests { @Test @WithMockUser public void logoutSuccess() throws Exception { - mvc.perform(logout()).andExpect(redirectedUrl("/login?logout")) + mvc.perform(logout()) + .andExpect(redirectedUrl("/login?logout")) .andExpect(unauthenticated()); } -} \ No newline at end of file +} diff --git a/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestBuilders.java b/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestBuilders.java index 6012d899fb..62e5451996 100644 --- a/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestBuilders.java +++ b/test/src/main/java/org/springframework/security/test/web/servlet/request/SecurityMockMvcRequestBuilders.java @@ -90,6 +90,7 @@ public final class SecurityMockMvcRequestBuilders { @Override public MockHttpServletRequest buildRequest(ServletContext servletContext) { MockHttpServletRequest request = post(this.logoutUrl) + .accept(MediaType.TEXT_HTML, MediaType.ALL) .buildRequest(servletContext); return this.postProcessor.postProcessRequest(request); } diff --git a/test/src/test/java/org/springframework/security/test/web/servlet/showcase/login/AuthenticationTests.java b/test/src/test/java/org/springframework/security/test/web/servlet/showcase/login/AuthenticationTests.java index 3f8b1db797..3eb7776620 100644 --- a/test/src/test/java/org/springframework/security/test/web/servlet/showcase/login/AuthenticationTests.java +++ b/test/src/test/java/org/springframework/security/test/web/servlet/showcase/login/AuthenticationTests.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; +import org.springframework.http.MediaType; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.userdetails.User; @@ -53,7 +54,10 @@ public class AuthenticationTests { @Before public void setup() { - mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build(); + mvc = MockMvcBuilders.webAppContextSetup(context) + .apply(springSecurity()) + .defaultRequest(get("/").accept(MediaType.TEXT_HTML)) + .build(); } @Test