Search in sources :

Example 21 with ServletRequest

use of javax.servlet.ServletRequest 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 22 with ServletRequest

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

the class HiddenHttpMethodFilterTests method filterWithParameter.

@Test
public void filterWithParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    request.addParameter("_method", "delete");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "DELETE", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 23 with ServletRequest

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

the class HiddenHttpMethodFilterTests method filterWithNoParameter.

@Test
public void filterWithNoParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "POST", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 24 with ServletRequest

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

the class ErrorPageFilterTests method responseUncommittedWithoutErrorPage.

@Test
public void responseUncommittedWithoutErrorPage() throws Exception {
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.chain.getRequest()).isEqualTo(this.request);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse()).isEqualTo(this.response);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.response.getForwardedUrl()).isNull();
    assertThat(this.response.isCommitted()).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 25 with ServletRequest

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

the class ErrorPageFilterTests method errorMessageForRequestWithPathInfo.

@Test
public void errorMessageForRequestWithPathInfo() throws IOException, ServletException {
    this.request.setServletPath("/test");
    this.request.setPathInfo("/alpha");
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            super.doFilter(request, response);
            throw new RuntimeException();
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.output.toString()).contains("request [/test/alpha]");
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Aggregations

ServletRequest (javax.servlet.ServletRequest)314 HttpServletRequest (javax.servlet.http.HttpServletRequest)188 ServletResponse (javax.servlet.ServletResponse)183 HttpServletResponse (javax.servlet.http.HttpServletResponse)118 FilterChain (javax.servlet.FilterChain)113 Test (org.junit.Test)82 IOException (java.io.IOException)65 ServletException (javax.servlet.ServletException)64 Filter (javax.servlet.Filter)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)28 Injector (com.google.inject.Injector)26 JspException (javax.servlet.jsp.JspException)26 RequestContext (com.agiletec.aps.system.RequestContext)25 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)25 FilterConfig (javax.servlet.FilterConfig)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 ServletContext (javax.servlet.ServletContext)23 HttpSession (javax.servlet.http.HttpSession)21 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18