Search in sources :

Example 1 with MockHttpServletResponse

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

the class InternalErrorCallsAjaxOnFailureTest method callsOnFailure.

/**
 * Setup {@link org.apache.wicket.settings.ExceptionSettings.AjaxErrorStrategy#INVOKE_FAILURE_HANDLER}
 * so Wicket will not redirect to the configured {@link InternalErrorPage}/{@link ExceptionErrorPage}
 * but will preserve the current page and send http status 500 to wicket-ajax.js
 */
@Test
public void callsOnFailure() {
    WicketTester tester = new WicketTester(new DummyApplication() {

        /**
         * @see org.apache.wicket.protocol.http.WebApplication#init()
         */
        @Override
        protected void init() {
            super.init();
            getExceptionSettings().setAjaxErrorHandlingStrategy(ExceptionSettings.AjaxErrorStrategy.INVOKE_FAILURE_HANDLER);
        }
    });
    tester.setExposeExceptions(false);
    tester.startPage(InternalErrorCallsAjaxOnFailurePage.class);
    tester.clickLink("failure-link", true);
    MockHttpServletResponse errorPageResponse = tester.getLastResponse();
    assertEquals(500, errorPageResponse.getStatus());
    // assert that the original page is still the last rendered one
    tester.assertRenderedPage(InternalErrorCallsAjaxOnFailurePage.class);
    tester.destroy();
}
Also used : WicketTester(org.apache.wicket.util.tester.WicketTester) BaseWicketTester(org.apache.wicket.util.tester.BaseWicketTester) DummyApplication(org.apache.wicket.resource.DummyApplication) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with MockHttpServletResponse

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

the class InternalErrorCallsAjaxOnFailureTest method showsInternalErrorPage.

/**
 * The default {@link org.apache.wicket.settings.ExceptionSettings#getAjaxErrorHandlingStrategy()} is
 * {@link org.apache.wicket.settings.ExceptionSettings.AjaxErrorStrategy#REDIRECT_TO_ERROR_PAGE}
 */
@Test
public void showsInternalErrorPage() {
    tester.setExposeExceptions(false);
    tester.startPage(InternalErrorCallsAjaxOnFailurePage.class);
    tester.clickLink("failure-link", true);
    // the response before current should holds the error page markup
    MockHttpServletResponse errorPageResponse = tester.getLastResponse();
    assertEquals(500, errorPageResponse.getStatus());
    assertTrue(errorPageResponse.getDocument().contains(InternalErrorCallsAjaxOnFailurePage.ERROR_MESSAGE));
    // assert the page with detailed error explanation is rendered
    tester.assertRenderedPage(ExceptionErrorPage.class);
}
Also used : MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with MockHttpServletResponse

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

the class XForwardedRequestWrapperTest method test7.

/**
 * @throws Exception
 */
@Test
public void test7() throws Exception {
    MyApplication app = new MyApplication();
    tester = new WicketTester(app);
    app.factory.getConfig().setAllowedInternalProxies("192\\.168\\.0\\.10, 192\\.168\\.0\\.11");
    app.factory.getConfig().setRemoteIPHeader("x-forwarded-for");
    app.factory.getConfig().setProxiesHeader("x-forwarded-by");
    app.factory.getConfig().setTrustedProxies("proxy1, proxy2");
    request.setRemoteAddr("192.168.0.10");
    request.addHeader("x-forwarded-for", "140.211.11.130, untrusted-proxy, proxy1");
    tester.startPage(SimplePage.class);
    tester.assertRenderedPage(SimplePage.class);
    tester.assertResultPage(SimplePage.class, "SimplePageExpectedResult.html");
    MockHttpServletResponse resp = tester.getResponse();
// @TODO should there be any header in the response ????
// assertEquals("140.211.11.130", resp.getHeader("x-forwarded-for"));
// assertEquals("proxy1", resp.getHeader("x-forwarded-by"));
}
Also used : WicketTester(org.apache.wicket.util.tester.WicketTester) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with MockHttpServletResponse

use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse 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 5 with MockHttpServletResponse

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

the class ModifyCookiePageTest method testSetCookieWithinLinkListener.

/**
 * testSetCookieWithinLinkListener()
 */
@Test
public void testSetCookieWithinLinkListener() {
    // render page
    tester.startPage(ModifyCookiePage.class);
    tester.assertRenderedPage(ModifyCookiePage.class);
    // click link that creates a cookie with in the link listener
    tester.clickLink(ModifyCookiePage.CREATE_COOKIE_ID);
    // check page is rendered
    tester.assertRenderedPage(ModifyCookiePage.class);
    // get response
    MockHttpServletResponse response = tester.getLastResponse();
    assertNotNull(response);
    // check that one cookie was set
    List<Cookie> cookies = response.getCookies();
    assertEquals(1, cookies.size());
    // check that cookie contains proper values
    Cookie cookie = cookies.get(0);
    assertEquals(ModifyCookiePage.COOKIE_NAME, cookie.getName());
    assertEquals(ModifyCookiePage.COOKIE_VALUE, cookie.getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.apache.wicket.protocol.http.mock.MockHttpServletResponse)11 Test (org.junit.Test)9 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)6 IOException (java.io.IOException)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 Cookie (javax.servlet.http.Cookie)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Application (org.apache.wicket.Application)2 MockApplication (org.apache.wicket.mock.MockApplication)2 WicketTester (org.apache.wicket.util.tester.WicketTester)2 StringReader (java.io.StringReader)1 Date (java.util.Date)1 ServletInputStream (javax.servlet.ServletInputStream)1 CookieCollection (org.apache.wicket.protocol.http.mock.CookieCollection)1 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)1 Request (org.apache.wicket.request.Request)1