Search in sources :

Example 11 with IRequestMapper

use of org.apache.wicket.request.IRequestMapper in project wicket by apache.

the class RequestCycleListenerTest method newRequestCycle.

private RequestCycle newRequestCycle(final RuntimeException exception) {
    final Response originalResponse = newResponse();
    Request request = new MockWebRequest(Url.parse("http://wicket.apache.org"));
    handler = new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            if (exception != null) {
                throw exception;
            }
            responses++;
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            detaches++;
        }
    };
    IRequestMapper requestMapper = new IRequestMapper() {

        @Override
        public IRequestHandler mapRequest(Request request) {
            return handler;
        }

        @Override
        public Url mapHandler(IRequestHandler requestHandler) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int getCompatibilityScore(Request request) {
            throw new UnsupportedOperationException();
        }
    };
    IExceptionMapper exceptionMapper = new IExceptionMapper() {

        @Override
        public IRequestHandler map(Exception e) {
            exceptionsMapped++;
            return null;
        }
    };
    RequestCycleContext context = new RequestCycleContext(request, originalResponse, requestMapper, exceptionMapper);
    RequestCycle cycle = new RequestCycle(context);
    if (Application.exists()) {
        cycle.getListeners().add(Application.get().getRequestCycleListeners());
    }
    return cycle;
}
Also used : Response(org.apache.wicket.request.Response) IRequestHandler(org.apache.wicket.request.IRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle) MockWebRequest(org.apache.wicket.mock.MockWebRequest) Request(org.apache.wicket.request.Request) MockWebRequest(org.apache.wicket.mock.MockWebRequest) IExceptionMapper(org.apache.wicket.request.IExceptionMapper) IRequestMapper(org.apache.wicket.request.IRequestMapper) IRequestCycle(org.apache.wicket.request.IRequestCycle) ReplaceHandlerException(org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)

Example 12 with IRequestMapper

use of org.apache.wicket.request.IRequestMapper in project wicket by apache.

the class HttpsMapperTest method getSchemeOfRequest.

@Test
public void getSchemeOfRequest() {
    IRequestMapper delegate = mock(IRequestMapper.class);
    HttpsMapper mapper = new HttpsMapper(delegate, new HttpsConfig());
    ServletWebRequest request = mock(ServletWebRequest.class);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(request.getContainerRequest()).thenReturn(req);
    when(req.getScheme()).thenReturn("https");
    assertThat(mapper.getSchemeOf(request), is(Scheme.HTTPS));
    reset(req);
    when(req.getScheme()).thenReturn("hTTps");
    assertThat(mapper.getSchemeOf(request), is(Scheme.HTTPS));
    reset(req);
    when(req.getScheme()).thenReturn("http");
    assertThat(mapper.getSchemeOf(request), is(Scheme.HTTP));
    try {
        reset(req);
        when(req.getScheme()).thenReturn("ftp");
        mapper.getSchemeOf(request);
        assertThat("expected error", false);
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IRequestMapper(org.apache.wicket.request.IRequestMapper) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) Test(org.junit.Test)

Example 13 with IRequestMapper

use of org.apache.wicket.request.IRequestMapper in project wicket by apache.

the class HttpsMapperTest method getDesiredSchemeOfPageClass.

@Test
public void getDesiredSchemeOfPageClass() {
    IRequestMapper delegate = mock(IRequestMapper.class);
    HttpsMapper mapper = new HttpsMapper(delegate, new HttpsConfig());
    assertThat(mapper.getDesiredSchemeFor(SecurePage.class), is(Scheme.HTTPS));
    assertThat(mapper.getDesiredSchemeFor(SecureDecendantPage.class), is(Scheme.HTTPS));
    assertThat(mapper.getDesiredSchemeFor(SecureMixinPage.class), is(Scheme.HTTPS));
    assertThat(mapper.getDesiredSchemeFor(InsecurePage.class), is(Scheme.HTTP));
}
Also used : IRequestMapper(org.apache.wicket.request.IRequestMapper) Test(org.junit.Test)

Example 14 with IRequestMapper

use of org.apache.wicket.request.IRequestMapper in project wicket by apache.

the class HttpsMapperTest method mapRequest.

@Test
public void mapRequest() {
    IRequestMapper delegate = mock(IRequestMapper.class);
    HttpsMapper mapper = new HttpsMapper(delegate, new HttpsConfig());
    ServletWebRequest request = mock(ServletWebRequest.class);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(request.getContainerRequest()).thenReturn(req);
    // https handler on http request, redirect to https
    setupRequest(req, "http", "localhost", 80, "/ctx", "foo=bar");
    IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(SecurePage.class));
    when(delegate.mapRequest(request)).thenReturn(handler);
    IRequestHandler resolved = mapper.mapRequest(request);
    assertThat(resolved, is(instanceOf(RedirectHandler.class)));
    assertThat(((RedirectHandler) resolved).getUrl(), is("https://localhost/ctx?foo=bar"));
    // http handler on http request, return the original handler
    handler = new RenderPageRequestHandler(new PageProvider(InsecurePage.class));
    reset(delegate);
    when(delegate.mapRequest(request)).thenReturn(handler);
    setupRequest(req, "http", "localhost", 80, "/ctx", "foo=bar");
    resolved = mapper.mapRequest(request);
    assertThat(resolved, is(sameInstance(handler)));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IRequestHandler(org.apache.wicket.request.IRequestHandler) RenderPageRequestHandler(org.apache.wicket.core.request.handler.RenderPageRequestHandler) PageProvider(org.apache.wicket.core.request.handler.PageProvider) IRequestMapper(org.apache.wicket.request.IRequestMapper) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) Test(org.junit.Test)

Aggregations

IRequestMapper (org.apache.wicket.request.IRequestMapper)14 Test (org.junit.Test)8 IRequestHandler (org.apache.wicket.request.IRequestHandler)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)5 PageProvider (org.apache.wicket.core.request.handler.PageProvider)4 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)4 Request (org.apache.wicket.request.Request)3 Url (org.apache.wicket.request.Url)3 IExceptionMapper (org.apache.wicket.request.IExceptionMapper)2 Response (org.apache.wicket.request.Response)2 ICompoundRequestMapper (org.apache.wicket.request.mapper.ICompoundRequestMapper)2 IRequestMapperDelegate (org.apache.wicket.request.mapper.IRequestMapperDelegate)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 HomePageMapper (org.apache.wicket.core.request.mapper.HomePageMapper)1 MockWebRequest (org.apache.wicket.mock.MockWebRequest)1 IRequestCycle (org.apache.wicket.request.IRequestCycle)1 ReplaceHandlerException (org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)1 WebRequest (org.apache.wicket.request.http.WebRequest)1