Search in sources :

Example 26 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.

the class SpringBootWebSecurityConfigurationTests method testWebConfigurationFilterChainBadCredentials.

@Test
public void testWebConfigurationFilterChainBadCredentials() throws Exception {
    this.context = SpringApplication.run(VanillaWebConfiguration.class, "--server.port=0");
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup((WebApplicationContext) this.context).addFilters(this.context.getBean("springSecurityFilterChain", Filter.class)).build();
    mockMvc.perform(MockMvcRequestBuilders.get("/").header("authorization", "Basic xxx")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(MockMvcResultMatchers.header().string("www-authenticate", Matchers.containsString("realm=\"Spring\"")));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 27 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.

the class SpringBootWebSecurityConfigurationTests method securityHeadersCanBeDisabled.

@Test
public void securityHeadersCanBeDisabled() throws Exception {
    this.context = SpringApplication.run(VanillaWebConfiguration.class, "--server.port=0", "--security.headers.content-type=false", "--security.headers.xss=false", "--security.headers.cache=false", "--security.headers.frame=false");
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup((WebApplicationContext) this.context).addFilters(this.context.getBean("springSecurityFilterChain", Filter.class)).build();
    mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(MockMvcResultMatchers.header().doesNotExist("X-Content-Type-Options")).andExpect(MockMvcResultMatchers.header().doesNotExist("X-XSS-Protection")).andExpect(MockMvcResultMatchers.header().doesNotExist("Cache-Control")).andExpect(MockMvcResultMatchers.header().doesNotExist("X-Frame-Options"));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 28 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.

the class SpringBootWebSecurityConfigurationTests method testWebConfigurationFilterChainUnauthenticatedWithAuthorizeModeNone.

@Test
public void testWebConfigurationFilterChainUnauthenticatedWithAuthorizeModeNone() throws Exception {
    this.context = SpringApplication.run(VanillaWebConfiguration.class, "--server.port=0", "--security.basic.authorize-mode=none");
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup((WebApplicationContext) this.context).addFilters(this.context.getBean("springSecurityFilterChain", Filter.class)).build();
    mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isNotFound());
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 29 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml.

@Test
public void welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml() throws Exception {
    load("spring.resources.static-locations:classpath:/welcome-page/");
    assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)).hasSize(1);
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isNotFound());
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 30 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation.

@Test
public void welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation() throws Exception {
    load("spring.resources.static-locations:classpath:/welcome-page");
    assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)).hasSize(1);
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()).andExpect(forwardedUrl("index.html"));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockMvc (org.springframework.test.web.servlet.MockMvc)92 Test (org.junit.Test)87 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)25 Todo (org.springframework.sync.Todo)16 List (java.util.List)15 TodoRepository (org.springframework.sync.TodoRepository)15 Filter (javax.servlet.Filter)13 MockServletContext (org.springframework.mock.web.MockServletContext)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)5 MvcResult (org.springframework.test.web.servlet.MvcResult)5 Ignore (org.junit.Ignore)3 FilterChainProxy (org.springframework.security.web.FilterChainProxy)3 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpSession (javax.servlet.http.HttpSession)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2