Search in sources :

Example 36 with MockMvc

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

the class MvcEndpointIntegrationTests method jsonExtensionProvided.

@Test
public void jsonExtensionProvided() throws Exception {
    TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(SecureConfiguration.class);
    MockMvc mockMvc = createSecureMockMvc();
    mockMvc.perform(get("/beans.json")).andExpect(status().isOk());
}
Also used : TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 37 with MockMvc

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

the class MvcEndpointIntegrationTests method fileExtensionNotFound.

@Test
public void fileExtensionNotFound() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(DefaultConfiguration.class);
    MockMvc mockMvc = createMockMvc();
    mockMvc.perform(get("/beans.cmd")).andExpect(status().isNotFound());
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 38 with MockMvc

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

the class MvcEndpointIntegrationTests method endpointSecurityCanBeDisabledWithCustomContextPath.

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

Example 39 with MockMvc

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

the class NoSpringSecurityHealthMvcEndpointIntegrationTests method healthWhenRightRoleNotPresentShouldNotExposeHealthDetails.

@Test
public void healthWhenRightRoleNotPresentShouldNotExposeHealthDetails() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(TestConfiguration.class);
    this.context.refresh();
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    mockMvc.perform(get("/health").with(getRequestPostProcessor())).andExpect(status().isOk()).andExpect(content().string("{\"status\":\"UP\"}"));
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 40 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)

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