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();
}
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);
}
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"));
}
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();
}
}
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());
}
Aggregations