use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.
the class JolokiaAutoConfigurationTests method agentServletWithCustomPath.
@Test
public void agentServletWithCustomPath() throws Exception {
this.context = new AnnotationConfigServletWebServerApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "endpoints.jolokia.path=/foo/bar");
this.context.register(EndpointsConfig.class, WebMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(JolokiaMvcEndpoint.class)).hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
mockMvc.perform(MockMvcRequestBuilders.get("/foo/bar")).andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("\"request\":{\"type\"")));
}
use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.
the class ManagementWebSecurityAutoConfigurationTests method testMarkAllEndpointsSensitive.
@Test
public void testMarkAllEndpointsSensitive() throws Exception {
// gh-4368
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(WebConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "endpoints.sensitive:true");
this.context.refresh();
MockMvc mockMvc = //
MockMvcBuilders.webAppContextSetup(this.context).apply(//
springSecurity()).build();
//
mockMvc.perform(//
get("/health")).andExpect(status().isUnauthorized());
//
mockMvc.perform(//
get("/info")).andExpect(status().isUnauthorized());
}
use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.
the class ManagementWebSecurityAutoConfigurationTests method realmSameForManagement.
// gh-2466
@Test
public void realmSameForManagement() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(AuthenticationConfig.class, SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, AuditAutoConfiguration.class);
this.context.refresh();
Filter filter = this.context.getBean("springSecurityFilterChain", Filter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).addFilters(filter).build();
// no user (Main)
mockMvc.perform(MockMvcRequestBuilders.get("/home")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
// invalid user (Main)
mockMvc.perform(MockMvcRequestBuilders.get("/home").header("authorization", "Basic xxx")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
// no user (Management)
mockMvc.perform(MockMvcRequestBuilders.get("/beans")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
// invalid user (Management)
mockMvc.perform(MockMvcRequestBuilders.get("/beans").header("authorization", "Basic xxx")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
}
use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.
the class MvcEndpointIntegrationTests method sensitiveEndpointsAreSecureWithNonActuatorRoleWithCustomContextPath.
@Test
public void sensitiveEndpointsAreSecureWithNonActuatorRoleWithCustomContextPath() throws Exception {
TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_USER"));
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(SecureConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "management.context-path:/management");
MockMvc mockMvc = createSecureMockMvc();
mockMvc.perform(get("/management/beans")).andExpect(status().isForbidden());
}
use of org.springframework.test.web.servlet.MockMvc in project spring-boot by spring-projects.
the class MvcEndpointIntegrationTests method sensitiveEndpointsAreSecureWithActuatorRoleWithCustomContextPath.
@Test
public void sensitiveEndpointsAreSecureWithActuatorRoleWithCustomContextPath() throws Exception {
TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(SecureConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "management.context-path:/management");
MockMvc mockMvc = createSecureMockMvc();
mockMvc.perform(get("/management/beans")).andExpect(status().isOk());
}
Aggregations