Search in sources :

Example 56 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot by spring-projects.

the class MetricFilterAutoConfigurationTests method whenExceptionIsThrownResponseStatusIsUsedWhenResponseHasBeenCommitted.

@Test
public void whenExceptionIsThrownResponseStatusIsUsedWhenResponseHasBeenCommitted() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class, MetricFilterAutoConfiguration.class);
    context.refresh();
    Filter filter = context.getBean(Filter.class);
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test/path");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    willAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            response.setStatus(200);
            response.setCommitted(true);
            throw new IOException();
        }
    }).given(chain).doFilter(request, response);
    try {
        filter.doFilter(request, response, chain);
        fail();
    } catch (IOException ex) {
    // Continue
    }
    verify(context.getBean(CounterService.class)).increment(eq("status.200.test.path"));
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot by spring-projects.

the class MetricFilterAutoConfigurationTests method additionallyRecordsMetricsWithHttpMethodNameIfConfigured.

@Test
public void additionallyRecordsMetricsWithHttpMethodNameIfConfigured() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class, MetricFilterAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.gauge-submissions=merged,per-http-method", "endpoints.metrics.filter.counter-submissions=merged,per-http-method");
    context.refresh();
    Filter filter = context.getBean(Filter.class);
    final MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/test/path");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    willAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            response.setStatus(200);
            return null;
        }
    }).given(chain).doFilter(request, response);
    filter.doFilter(request, response, chain);
    verify(context.getBean(GaugeService.class)).submit(eq("response.test.path"), anyDouble());
    verify(context.getBean(GaugeService.class)).submit(eq("response.PUT.test.path"), anyDouble());
    verify(context.getBean(CounterService.class)).increment(eq("status.200.test.path"));
    verify(context.getBean(CounterService.class)).increment(eq("status.PUT.200.test.path"));
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot by spring-projects.

the class LinksEnhancerTests method setup.

@Before
public void setup() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Before(org.junit.Before)

Example 59 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot by spring-projects.

the class CloudFoundryEndpointHandlerMappingTests method registersCloudFoundryHealthEndpoint.

@Test
public void registersCloudFoundryHealthEndpoint() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    HealthEndpoint delegate = new HealthEndpoint(new OrderedHealthAggregator(), Collections.<String, HealthIndicator>emptyMap());
    CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(Collections.singleton(new TestHealthMvcEndpoint(delegate)), null, null);
    handlerMapping.setPrefix("/test");
    handlerMapping.setApplicationContext(context);
    handlerMapping.afterPropertiesSet();
    HandlerExecutionChain handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/test/health"));
    HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
    Object handlerMethodBean = handlerMethod.getBean();
    assertThat(handlerMethodBean).isInstanceOf(CloudFoundryHealthMvcEndpoint.class);
}
Also used : HealthEndpoint(org.springframework.boot.actuate.endpoint.HealthEndpoint) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OrderedHealthAggregator(org.springframework.boot.actuate.health.OrderedHealthAggregator) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 60 with MockHttpServletRequest

use of org.springframework.mock.web.MockHttpServletRequest in project spring-boot by spring-projects.

the class CloudFoundrySecurityInterceptorTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.interceptor = new CloudFoundrySecurityInterceptor(this.tokenValidator, this.securityService, "my-app-id");
    this.endpoint = new TestMvcEndpoint(new TestEndpoint("a"));
    this.handlerMethod = new HandlerMethod(this.endpoint, "invoke");
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3144 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1989 Test (org.junit.jupiter.api.Test)1907 lombok.val (lombok.val)1124 Test (org.junit.Test)752 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)482 MockServletContext (org.springframework.mock.web.MockServletContext)471 MockRequestContext (org.springframework.webflow.test.MockRequestContext)468 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)245 MockFilterChain (org.springframework.mock.web.MockFilterChain)238 JEEContext (org.pac4j.core.context.JEEContext)156 Authentication (org.springframework.security.core.Authentication)144 BeforeEach (org.junit.jupiter.api.BeforeEach)132 HashMap (java.util.HashMap)123 FilterChain (jakarta.servlet.FilterChain)117 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)108 MockHttpSession (org.springframework.mock.web.MockHttpSession)98 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)96 Before (org.junit.Before)82 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)78