Search in sources :

Example 6 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 7 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 8 with MockMvc

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

the class MetricFilterAutoConfigurationTests method correctlyRecordsMetricsForDeferredResultResponse.

@Test
public void correctlyRecordsMetricsForDeferredResultResponse() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    MetricsFilter filter = context.getBean(MetricsFilter.class);
    CountDownLatch latch = new CountDownLatch(1);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController(latch)).addFilter(filter).build();
    String attributeName = MetricsFilter.class.getName() + ".StopWatch";
    MvcResult result = mvc.perform(post("/create")).andExpect(status().isOk()).andExpect(request().asyncStarted()).andExpect(request().attribute(attributeName, is(notNullValue()))).andReturn();
    latch.countDown();
    mvc.perform(asyncDispatch(result)).andExpect(status().isCreated()).andExpect(request().attribute(attributeName, is(nullValue())));
    verify(context.getBean(CounterService.class)).increment("status.201.create");
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 9 with MockMvc

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

the class MetricFilterAutoConfigurationTests method recordsHttpInteractionsWithRegexTemplateVariable.

@Test
public void recordsHttpInteractionsWithRegexTemplateVariable() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    Filter filter = context.getBean(Filter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
    mvc.perform(get("/templateVarRegexTest/foo")).andExpect(status().isOk());
    verify(context.getBean(CounterService.class)).increment("status.200.templateVarRegexTest.someVariable");
    verify(context.getBean(GaugeService.class)).submit(eq("response.templateVarRegexTest.someVariable"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 10 with MockMvc

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

the class MetricFilterAutoConfigurationTests method controllerMethodThatThrowsUnhandledException.

@Test
public void controllerMethodThatThrowsUnhandledException() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    Filter filter = context.getBean(Filter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
    try {
        mvc.perform(get("/unhandledException")).andExpect(status().isInternalServerError());
    } catch (NestedServletException ex) {
    // Expected
    }
    verify(context.getBean(CounterService.class)).increment("status.500.unhandledException");
    verify(context.getBean(GaugeService.class)).submit(eq("response.unhandledException"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedServletException(org.springframework.web.util.NestedServletException) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) 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