Search in sources :

Example 1 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest in project wicket by apache.

the class WicketFilterTest method options.

@Test
public void options() throws IOException, ServletException, ParseException {
    try {
        application = new MockApplication();
        WicketFilter filter = new WicketFilter();
        filter.init(new FilterTestingConfig());
        ThreadContext.setApplication(application);
        final String failure = "Should never get here when an OPTIONS request is issued";
        IResource resource = new AbstractResource() {

            @Override
            protected ResourceResponse newResourceResponse(Attributes attributes) {
                fail(failure);
                return null;
            }
        };
        application.getSharedResources().add("foo.txt", resource);
        // check OPTIONS request is processed correctly
        MockHttpServletRequest request = new MockHttpServletRequest(application, null, null);
        request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.txt");
        // test that we do not care about case
        request.setMethod("OPtioNS");
        MockHttpServletResponse response = new MockHttpServletResponse(request);
        filter.doFilter(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            }
        });
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        assertEquals("0", response.getHeader("Content-Length"));
        assertFalse(Strings.isEmpty(response.getHeader("Allow")));
        assertTrue(response.getHeader("Allow").toUpperCase().contains("GET"));
        assertTrue(response.getHeader("Allow").toUpperCase().contains("POST"));
        // try with a GET request to make sure we fail correctly
        request = new MockHttpServletRequest(application, null, null);
        request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.txt");
        response = new MockHttpServletResponse(request);
        try {
            filter.doFilter(request, response, new FilterChain() {

                @Override
                public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
                }
            });
        } catch (AssertionError e) {
            assertTrue(failure.equals(e.getMessage()));
        }
    } finally {
        ThreadContext.detach();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockApplication(org.apache.wicket.mock.MockApplication) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) IResource(org.apache.wicket.request.resource.IResource) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest in project wicket by apache.

the class ServletWebRequestTest method parseForwardAttributes.

/**
 * https://issues.apache.org/jira/browse/WICKET-4138
 *
 * Relative Urls should be calculated against 'javax.servlet.forward.request_uri'
 */
@Test
public void parseForwardAttributes() {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest(null, null, null);
    httpRequest.setURL(httpRequest.getContextPath() + "/request/Uri");
    String forwardedURI = httpRequest.getContextPath() + "/some/forwarded/url";
    httpRequest.setAttribute("javax.servlet.forward.request_uri", forwardedURI);
    ServletWebRequest forwardWebRequest = new ServletWebRequest(httpRequest, "");
    Url forwardClientUrl = forwardWebRequest.getClientUrl();
    assertEquals("some/forwarded/url", forwardClientUrl.toString());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 3 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest in project wicket by apache.

the class ServletWebRequestTest method getClientUrlAjaxWithoutBaseUrl.

/**
 * Assert that ServletWebRequest#getClientUrl() will throw an AbortWithHttpErrorCodeException
 * with error code 400 (Bad Request) when an Ajax request doesn't provide the base url.
 *
 * https://issues.apache.org/jira/browse/WICKET-4841
 */
@Test
public void getClientUrlAjaxWithoutBaseUrl() {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest(null, null, null);
    httpRequest.setHeader(ServletWebRequest.HEADER_AJAX, "true");
    ServletWebRequest webRequest = new ServletWebRequest(httpRequest, "");
    try {
        webRequest.getClientUrl();
        fail("Should not be possible to get the request client url in Ajax request without base url");
    } catch (AbortWithHttpErrorCodeException awhex) {
        assertEquals(HttpServletResponse.SC_BAD_REQUEST, awhex.getErrorCode());
    }
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest in project wicket by apache.

the class ServletWebRequestTest method parseUrlWhichLooksLikeFullInItsContextRelativePart.

/**
 * WICKET-5287
 */
@Test
public void parseUrlWhichLooksLikeFullInItsContextRelativePart() {
    String filterPath = "filterPath";
    MockHttpServletRequest httpRequest = new MockHttpServletRequest(null, null, null);
    String looksLikeFullUrl = "/foo://:/";
    httpRequest.setURL("http://localhost" + '/' + httpRequest.getContextPath() + '/' + filterPath + looksLikeFullUrl);
    ServletWebRequest webRequest = new ServletWebRequest(httpRequest, filterPath);
    assertEquals(looksLikeFullUrl, webRequest.getClientUrl().toString());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest in project wicket by apache.

the class BaseWicketTester method setupNextRequestCycle.

private void setupNextRequestCycle() {
    request = new MockHttpServletRequest(application, httpSession, servletContext, servletRequestLocale());
    request.setURL(request.getContextPath() + request.getServletPath() + "/");
    // assign protocol://host:port to next request unless the last request was ajax
    final boolean assignBaseLocation = lastRequest != null && lastRequest.getHeader("Wicket-Ajax") == null;
    // resume request processing with scheme://host:port from last request
    if (assignBaseLocation) {
        request.setScheme(lastRequest.getScheme());
        request.setSecure(lastRequest.isSecure());
        request.setServerName(lastRequest.getServerName());
        request.setServerPort(lastRequest.getServerPort());
    }
    response = new MockHttpServletResponse(request);
    // They should assert that the cookie is in the next *request*
    if (lastResponse != null) {
        List<Cookie> lastResponseCookies = lastResponse.getCookies();
        if (lastResponse.isRedirect()) {
            CookieCollection responseCookies = new CookieCollection();
            // if the last request is a redirect, all cookies from last response should appear
            // in current response
            // this call will filter duplicates
            responseCookies.addAll(lastResponseCookies);
            for (Cookie cookie : responseCookies.allAsList()) {
                response.addCookie(cookie);
            }
            // handling this way, the cookie will be send to the next requested page
            if (lastRequest != null) {
                CookieCollection requestCookies = new CookieCollection();
                // this call will filter duplicates
                requestCookies.addAll(lastRequest.getCookies());
                request.addCookies(requestCookies.asList());
            }
        } else {
            // if the last response is not a redirect
            // - copy last request cookies to collection
            // - copy last response cookies to collection
            // - set only the not expired cookies to the next request
            CookieCollection cookies = new CookieCollection();
            if (lastRequest != null) {
                // this call will filter duplicates
                cookies.addAll(lastRequest.getCookies());
            }
            // this call will filter duplicates
            cookies.addAll(lastResponseCookies);
            request.addCookies(cookies.asList());
        }
    }
    ServletWebRequest servletWebRequest = newServletWebRequest();
    requestCycle = application.createRequestCycle(servletWebRequest, newServletWebResponse(servletWebRequest));
    ThreadContext.setRequestCycle(requestCycle);
    if (session == null) {
        newSession();
    }
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) CookieCollection(org.apache.wicket.protocol.http.mock.CookieCollection)

Aggregations

MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)24 Test (org.junit.Test)13 MockHttpServletResponse (org.apache.wicket.protocol.http.mock.MockHttpServletResponse)5 Url (org.apache.wicket.request.Url)5 Application (org.apache.wicket.Application)4 IOException (java.io.IOException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 FilterChain (javax.servlet.FilterChain)2 ServletException (javax.servlet.ServletException)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 MockApplication (org.apache.wicket.mock.MockApplication)2 WebApplication (org.apache.wicket.protocol.http.WebApplication)2 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)2 Request (org.apache.wicket.request.Request)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)2 UrlAttributes (org.apache.wicket.request.resource.ResourceReference.UrlAttributes)2 ByteArrayResponse (org.apache.wicket.response.ByteArrayResponse)2 StringReader (java.io.StringReader)1