Search in sources :

Example 36 with FilterChain

use of javax.servlet.FilterChain in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterHas500ResponseStatusWhenExceptionIsThrown.

@Test
@SuppressWarnings("unchecked")
public void filterHas500ResponseStatusWhenExceptionIsThrown() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        this.filter.doFilterInternal(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
                throw new RuntimeException();
            }
        });
        fail("Exception was swallowed");
    } catch (RuntimeException ex) {
        Map<String, Object> headers = (Map<String, Object>) this.repository.findAll().iterator().next().getInfo().get("headers");
        Map<String, Object> responseHeaders = (Map<String, Object>) headers.get("response");
        assertThat((String) responseHeaders.get("status")).isEqualTo("500");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 37 with FilterChain

use of javax.servlet.FilterChain in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.addHeader("Authorization", "my-auth-header");
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(((Map<Object, Object>) headers.get("request"))).hasSize(0);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 38 with FilterChain

use of javax.servlet.FilterChain in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude() throws ServletException, IOException {
    this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.addHeader("Content-Type", "application/json");
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(headers.get("response") == null).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 39 with FilterChain

use of javax.servlet.FilterChain in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method noQueryStringAvailable.

@Test
public void noQueryStringAvailable() throws Exception {
    filter.setIncludeQueryString(true);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new NoOpFilterChain();
    filter.doFilter(request, response, filterChain);
    assertNotNull(filter.beforeRequestMessage);
    assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels]"));
    assertNotNull(filter.afterRequestMessage);
    assertTrue(filter.afterRequestMessage.contains("[uri=/hotels]"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 40 with FilterChain

use of javax.servlet.FilterChain in project spring-framework by spring-projects.

the class RequestLoggingFilterTests method queryStringIncluded.

@Test
public void queryStringIncluded() throws Exception {
    filter.setIncludeQueryString(true);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setQueryString("booking=42");
    FilterChain filterChain = new NoOpFilterChain();
    filter.doFilter(request, response, filterChain);
    assertNotNull(filter.beforeRequestMessage);
    assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels?booking=42]"));
    assertNotNull(filter.afterRequestMessage);
    assertTrue(filter.afterRequestMessage.contains("[uri=/hotels?booking=42]"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19