Search in sources :

Example 11 with MockHttpServletRequest

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

the class PackageResourceReferenceTest method makeRangeRequestToBigResource.

private String makeRangeRequestToBigResource(String range) {
    ResourceReference reference = new PackageResourceReference(scope, "resource_gt_4096.txt", null, null, null);
    ByteArrayResponse byteResponse = new ByteArrayResponse();
    Request request = tester.getRequestCycle().getRequest();
    MockHttpServletRequest mockHttpServletRequest = (MockHttpServletRequest) request.getContainerRequest();
    mockHttpServletRequest.setHeader("range", range);
    Attributes mockAttributes = new Attributes(request, byteResponse);
    reference.getResource().respond(mockAttributes);
    return new String(byteResponse.getBytes());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Request(org.apache.wicket.request.Request) Attributes(org.apache.wicket.request.resource.IResource.Attributes) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse)

Example 12 with MockHttpServletRequest

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

the class PackageResourceReferenceTest method makeRangeRequest.

private String makeRangeRequest(String range) {
    ResourceReference reference = new PackageResourceReference(scope, "resource.txt", locales[1], styles[1], variations[1]);
    ByteArrayResponse byteResponse = new ByteArrayResponse();
    Request request = tester.getRequestCycle().getRequest();
    MockHttpServletRequest mockHttpServletRequest = (MockHttpServletRequest) request.getContainerRequest();
    mockHttpServletRequest.setHeader("range", range);
    Attributes mockAttributes = new Attributes(request, byteResponse);
    reference.getResource().respond(mockAttributes);
    return new String(byteResponse.getBytes());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Request(org.apache.wicket.request.Request) Attributes(org.apache.wicket.request.resource.IResource.Attributes) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse)

Example 13 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest 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();
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) StringReader(java.io.StringReader) IOException(java.io.IOException) Url(org.apache.wicket.request.Url) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse)

Example 14 with MockHttpServletRequest

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

the class WebApplicationTest method testBodyNotReadBeforeApplicationSetsCharacterEncoding.

/**
 * WICKET-6260
 */
@Test
public void testBodyNotReadBeforeApplicationSetsCharacterEncoding() throws Exception {
    WebApplication application = tester.getApplication();
    HttpServletRequest request = new MockHttpServletRequest(application, null, null) {

        @Override
        public Map<String, String[]> getParameterMap() {
            fail("body should not be read before character encoding is set");
            return null;
        }

        @Override
        public String getParameter(String name) {
            fail("body should not be read before character encoding is set");
            return null;
        }

        @Override
        public Enumeration<String> getParameterNames() {
            fail("body should not be read before character encoding is set");
            return null;
        }

        @Override
        public String[] getParameterValues(String name) {
            fail("body should not be read before character encoding is set");
            return null;
        }

        @Override
        public MockRequestParameters getPostParameters() {
            fail("body should not be read before character encoding is set");
            return null;
        }
    };
    // character encoding not set yet
    request.setCharacterEncoding(null);
    application.createWebRequest(request, "/");
    assertEquals("UTF-8", request.getCharacterEncoding());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Test(org.junit.Test)

Example 15 with MockHttpServletRequest

use of org.apache.wicket.protocol.http.mock.MockHttpServletRequest 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();
    }
}
Also used : DynamicImageResource(org.apache.wicket.request.resource.DynamicImageResource) 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) IOException(java.io.IOException) Date(java.util.Date) ServletException(javax.servlet.ServletException) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

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