use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse 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();
}
}
use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse in project wicket by apache.
the class PackageResourceReferenceTest method testContentRangeHeaders.
/**
* See WICKET-5819 - Media tags
*/
@Test
public void testContentRangeHeaders() {
// Test header fields
ResourceReference reference = new PackageResourceReference(scope, "resource.txt", locales[1], styles[1], variations[1]);
Request request = tester.getRequestCycle().getRequest();
Response response = tester.getRequestCycle().getResponse();
MockHttpServletResponse mockHttpServletResponse = (MockHttpServletResponse) response.getContainerResponse();
Attributes mockAttributes = new Attributes(request, response);
reference.getResource().respond(mockAttributes);
Assert.assertEquals(ContentRangeType.BYTES.getTypeName(), mockHttpServletResponse.getHeader("Accept-Range"));
// For normal: If a resource supports content range no content is delivered
// if no "Range" header is given, but we have to deliver it, because
// other resources then media should get the content. (e.g. CSS, JS, etc.) Browsers
// detecting media requests and automatically add the "Range" header for
// partial content and they don't make an initial request to detect if a media
// resource supports Content-Range (by the Accept-Range header)
Assert.assertEquals("resource_var_style_en.txt", new String(mockHttpServletResponse.getBinaryContent()));
}
use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse in project wicket-orientdb by OrienteerBAP.
the class WicketOrientDbTester method executeUrl.
public String executeUrl(String _url, final String method, final String content, String username, String password) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getApplication(), getHttpSession(), getServletContext()) {
{
setMethod(method);
}
@Override
public ServletInputStream getInputStream() throws IOException {
if (content == null)
return super.getInputStream();
else {
final StringReader sr = new StringReader(content);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return sr.read();
}
};
}
}
};
Url url = Url.parse(_url, Charset.forName(request.getCharacterEncoding()));
request.setUrl(url);
request.setMethod(method);
if (username != null && password != null) {
request.setHeader(LazyAuthorizationRequestCycleListener.AUTHORIZATION_HEADER, "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
}
if (!processRequest(request)) {
throw new IOException("Request was not sucessfully sent");
}
MockHttpServletResponse response = getLastResponse();
int status = response.getStatus();
if (status >= HttpServletResponse.SC_OK + 100) {
throw new IOException("Code: " + response.getStatus() + " Message: " + response.getErrorMessage() + " Content: " + response.getDocument());
} else {
return response.getDocument();
}
}
use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse in project wicket by apache.
the class WicketFilterTest method notModifiedResponseIncludesExpiresHeader.
/**
* @throws IOException
* @throws ServletException
* @throws ParseException
*/
@Test
public void notModifiedResponseIncludesExpiresHeader() throws IOException, ServletException, ParseException {
try {
application = new MockApplication();
WicketFilter filter = new WicketFilter();
filter.init(new FilterTestingConfig());
ThreadContext.setApplication(application);
DynamicImageResource resource = new DynamicImageResource() {
private static final long serialVersionUID = 1L;
@Override
protected byte[] getImageData(Attributes attributes) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
ResourceResponse response = super.newResourceResponse(attributes);
response.setCacheDurationToMaximum();
return response;
}
};
application.getSharedResources().add("foo.gif", resource);
MockHttpServletRequest request = new MockHttpServletRequest(application, null, null);
request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.gif");
setIfModifiedSinceToNextWeek(request);
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_NOT_MODIFIED, response.getStatus());
String responseExpiresHeader = response.getHeader("Expires");
assertNotNull("Expires header must be set on not modified response", responseExpiresHeader);
Date responseExpires = headerDateFormat.parse(responseExpiresHeader);
assertTrue("Expected later than current date but was " + responseExpires, responseExpires.after(new Date()));
} finally {
ThreadContext.detach();
}
}
use of org.apache.wicket.protocol.http.mock.MockHttpServletResponse in project wicket by apache.
the class RerenderPageTest method nonAjaxRequestAfterAjaxUpdatedComponentShouldHaveHtmlHeadSection.
/**
* Another test case for WICKET-5960.
*
* When an AJAX update was performed, the next normal request would still find the page left
* with the PartialHtmlHeaderContainer causing an empty {@code
* <head>} section to be rendered. This test case walks Wicket through this scenario.
*/
@Test
public void nonAjaxRequestAfterAjaxUpdatedComponentShouldHaveHtmlHeadSection() {
// perform a normal render of the page
tester.startPage(RerenderAjaxPage.class);
tester.assertRenderedPage(RerenderAjaxPage.class);
MockHttpServletResponse firstResponseBeforeAjaxUpdate = tester.getLastResponse();
// call an ajax event that updates a component
tester.executeAjaxEvent("form:username", "blur");
tester.assertComponentOnAjaxResponse("feedback");
// perform a normal render of the page (in this case submitting the form which triggers a
// feedback error
tester.submitForm("form");
// record the response for later reference
MockHttpServletResponse normalResponseAfterAjaxUpdate = tester.getLastResponse();
// submit the form again to ascertain if the HTML head section was restored upon the second
// render
tester.submitForm("form");
// record the response for later reference
MockHttpServletResponse secondNormalResponse = tester.getLastResponse();
// assert that the first response indeed got the correct <head> section
assertThat(firstResponseBeforeAjaxUpdate.getDocument(), containsString(RerenderAjaxPage.HEAD_TEXT));
// assert that the second normal response after the AJAX update indeed got the correct
// <head> section (this worked while the bug was still present)
assertThat(secondNormalResponse.getDocument(), containsString(RerenderAjaxPage.HEAD_TEXT));
// assert that the first normal response after the AJAX update indeed got the correct
// <head> section (this failed while the bug was still present)
assertThat(normalResponseAfterAjaxUpdate.getDocument(), containsString(RerenderAjaxPage.HEAD_TEXT));
}
Aggregations