Search in sources :

Example 31 with MockMvc

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

the class MultipartControllerTests method multipartRequestWrapped.

// SPR-13317
@Test
public void multipartRequestWrapped() throws Exception {
    byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
    MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
    Filter filter = new RequestWrappingFilter();
    MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build();
    Map<String, String> jsonMap = Collections.singletonMap("name", "yeeeah");
    mockMvc.perform(multipart("/test-json").file(jsonPart)).andExpect(model().attribute("json", jsonMap));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) Filter(javax.servlet.Filter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 32 with MockMvc

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

the class HtmlUnitRequestBuilderTests method mergeSession.

@Test
public void mergeSession() throws Exception {
    String attrName = "PARENT";
    String attrValue = "VALUE";
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()).defaultRequest(get("/").sessionAttr(attrName, attrValue)).build();
    assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getSession().getAttribute(attrName), equalTo(attrValue));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 33 with MockMvc

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());
}
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 34 with MockMvc

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());
}
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 35 with MockMvc

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

the class MvcEndpointIntegrationTests method defaultJsonResponseIsNotIndented.

@Test
public void defaultJsonResponseIsNotIndented() 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("/mappings")).andExpect(content().string(startsWith("{\"")));
}
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)

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