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