Search in sources :

Example 16 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingRegularRequest.

/**
     * Unwrapping a non-wrapper request should return the request itself.
     */
@Test
public void testUnwrappingRegularRequest() {
    final ServletRequest req = context.mock(ServletRequest.class);
    ServletRequest unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletRequest(req);
    assertEquals(req, unwrapped);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 17 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class ExternalServletContextWrapperTest method testUnwrappingSlingRequest.

/**
     * Unwrapping a sling request should return the first-level request wrapped
     * by the sling request.
     */
@Test
public void testUnwrappingSlingRequest() {
    final HttpServletRequest req = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            allowing(req).getServletPath();
            will(returnValue("/"));
            allowing(req).getPathInfo();
            will(returnValue("/test"));
        }
    });
    final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(req);
    final HttpServletRequestWrapper wrapper2 = new HttpServletRequestWrapper(wrapper);
    final SlingHttpServletRequestImpl slingRequest = new SlingHttpServletRequestImpl(null, wrapper2);
    ServletRequest unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletRequest(slingRequest);
    assertEquals(wrapper2, unwrapped);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Expectations(org.jmock.Expectations) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) SlingHttpServletRequestImpl(org.apache.sling.engine.impl.SlingHttpServletRequestImpl) Test(org.junit.Test)

Example 18 with ServletRequest

use of javax.servlet.ServletRequest in project sling by apache.

the class SlingAuthenticator method requestDestroyed.

@Override
public void requestDestroyed(ServletRequestEvent sre) {
    ServletRequest request = sre.getServletRequest();
    Object resolverAttr = request.getAttribute(REQUEST_ATTRIBUTE_RESOLVER);
    if (resolverAttr instanceof ResourceResolver) {
        ((ResourceResolver) resolverAttr).close();
        request.removeAttribute(REQUEST_ATTRIBUTE_RESOLVER);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 19 with ServletRequest

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

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

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