Search in sources :

Example 71 with MockMvc

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

the class MetricFilterAutoConfigurationTests method correctlyRecordsMetricsForFailedDeferredResultResponse.

@Test
public void correctlyRecordsMetricsForFailedDeferredResultResponse() 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("/createFailure")).andExpect(status().isOk()).andExpect(request().asyncStarted()).andExpect(request().attribute(attributeName, is(notNullValue()))).andReturn();
    latch.countDown();
    try {
        mvc.perform(asyncDispatch(result));
        fail();
    } catch (Exception ex) {
        assertThat(result.getRequest().getAttribute(attributeName)).isNull();
        verify(context.getBean(CounterService.class)).increment("status.500.createFailure");
    } finally {
        context.close();
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CounterService(org.springframework.boot.actuate.metrics.CounterService) 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) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 72 with MockMvc

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

the class MetricFilterAutoConfigurationTests method recordsHttpInteractionsWithDoubleWildcardMapping.

@Test
public void recordsHttpInteractionsWithDoubleWildcardMapping() 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("/doubleWildcardMapping/foo/bar/baz")).andExpect(status().isOk());
    verify(context.getBean(CounterService.class)).increment("status.200.doubleWildcardMapping.star-star.baz");
    verify(context.getBean(GaugeService.class)).submit(eq("response.doubleWildcardMapping.star-star.baz"), 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 73 with MockMvc

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

the class MetricFilterAutoConfigurationTests method records5xxxHttpInteractionsAsSingleMetric.

@Test
public void records5xxxHttpInteractionsAsSingleMetric() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class, ServiceUnavailableFilter.class);
    MetricsFilter filter = context.getBean(MetricsFilter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).addFilter(context.getBean(ServiceUnavailableFilter.class)).build();
    mvc.perform(get("/unknownPath/1")).andExpect(status().isServiceUnavailable());
    mvc.perform(get("/unknownPath/2")).andExpect(status().isServiceUnavailable());
    verify(context.getBean(CounterService.class), times(2)).increment("status.503.unmapped");
    verify(context.getBean(GaugeService.class), times(2)).submit(eq("response.unmapped"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 74 with MockMvc

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

the class DeviceResolverAutoConfigurationTests method deviceHandlerMethodArgumentWorksWithSpringData.

@Test
public void deviceHandlerMethodArgumentWorksWithSpringData() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(Config.class);
    this.context.setServletContext(new MockServletContext());
    this.context.refresh();
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    mockMvc.perform(get("/")).andExpect(status().isOk());
}
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 75 with MockMvc

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

the class SpringBootWebSecurityConfigurationTests method defaultHeaderConfiguration.

@Test
public void defaultHeaderConfiguration() throws Exception {
    this.context = SpringApplication.run(VanillaWebConfiguration.class, "--server.port=0");
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup((WebApplicationContext) this.context).addFilters((FilterChainProxy) this.context.getBean("springSecurityFilterChain", Filter.class)).build();
    mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.header().string("X-Content-Type-Options", is(notNullValue()))).andExpect(MockMvcResultMatchers.header().string("X-XSS-Protection", is(notNullValue()))).andExpect(MockMvcResultMatchers.header().string("Cache-Control", is(notNullValue()))).andExpect(MockMvcResultMatchers.header().string("X-Frame-Options", is(notNullValue()))).andExpect(MockMvcResultMatchers.header().doesNotExist("Content-Security-Policy"));
}
Also used : FilterChainProxy(org.springframework.security.web.FilterChainProxy) Filter(javax.servlet.Filter) 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