Search in sources :

Example 76 with MockMvc

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

the class SpringBootWebSecurityConfigurationTests method testWebConfigurationFilterChainUnauthenticated.

@Test
public void testWebConfigurationFilterChainUnauthenticated() 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("/")).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 77 with MockMvc

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

the class WebMvcAutoConfigurationTests method welcomePageMappingHandlesRequestsWithNoAcceptHeader.

@Test
public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() 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("/")).andExpect(status().isOk()).andExpect(forwardedUrl("index.html"));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 78 with MockMvc

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

the class WebMvcAutoConfigurationTests method welcomePageMappingHandlesRequestsThatAcceptTextHtml.

@Test
public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() 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"));
    mockMvc.perform(get("/").accept("*/*")).andExpect(status().isOk()).andExpect(forwardedUrl("index.html"));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 79 with MockMvc

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

the class MvcEndpointIntegrationTests method assertIndentedJsonResponse.

private void assertIndentedJsonResponse(Class<?> configuration) throws Exception {
    TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(configuration);
    EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.serialization.indent-output:true");
    MockMvc mockMvc = createSecureMockMvc();
    mockMvc.perform(get("/mappings")).andExpect(content().string(startsWith("{" + LINE_SEPARATOR)));
}
Also used : TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc)

Example 80 with MockMvc

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

the class MvcEndpointIntegrationTests method nonSensitiveEndpointsAreNotSecureByDefaultWithCustomContextPath.

@Test
public void nonSensitiveEndpointsAreNotSecureByDefaultWithCustomContextPath() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(SecureConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "management.context-path:/management");
    MockMvc mockMvc = createSecureMockMvc();
    mockMvc.perform(get("/management/info")).andExpect(status().isOk());
    mockMvc.perform(get("/management")).andExpect(status().isOk());
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockMvc (org.springframework.test.web.servlet.MockMvc)97 Test (org.junit.Test)91 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 MvcResult (org.springframework.test.web.servlet.MvcResult)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)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