Search in sources :

Example 1 with MockApplication

use of org.apache.wicket.mock.MockApplication in project wicket by apache.

the class AjaxBehaviorEnabledTest method before.

/**
 */
@Before
public void before() {
    final IAuthorizationStrategy strategy = new CustomStrategy();
    tester = new WicketTester(new MockApplication() {

        @Override
        public Session newSession(Request request, Response response) {
            return new WebSession(request) {

                private static final long serialVersionUID = 1L;

                @Override
                public IAuthorizationStrategy getAuthorizationStrategy() {
                    return strategy;
                }
            };
        }
    });
}
Also used : Response(org.apache.wicket.request.Response) WebSession(org.apache.wicket.protocol.http.WebSession) MockApplication(org.apache.wicket.mock.MockApplication) Request(org.apache.wicket.request.Request) WicketTester(org.apache.wicket.util.tester.WicketTester) IAuthorizationStrategy(org.apache.wicket.authorization.IAuthorizationStrategy) Before(org.junit.Before)

Example 2 with MockApplication

use of org.apache.wicket.mock.MockApplication in project wicket by apache.

the class WicketFilterTest method ignorePaths.

/**
 * <a href="https://issues.apache.org/jira/browse/WICKET-3750">WICKET-3750</a>
 *
 * @throws Exception
 */
@Test
public void ignorePaths() throws Exception {
    application = spy(new MockApplication());
    WicketFilter filter = new WicketFilter();
    filter.init(new FilterTestingConfig());
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getLocale()).thenReturn(new Locale("bg", "BG"));
    when(request.getRequestURI()).thenReturn("/contextPath/js/bla.js").thenReturn("/contextPath/css/bla.css").thenReturn("/contextPath/images/bla.img").thenReturn("/contextPath/servlet/wicket/bookmarkable/" + DummyHomePage.class.getName());
    when(request.getContextPath()).thenReturn("/contextPath");
    when(request.getMethod()).thenReturn("POST");
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.encodeRedirectURL(Matchers.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return (String) invocation.getArguments()[0];
        }
    });
    FilterChain chain = mock(FilterChain.class);
    // execute 3 requests - 1 for bla.js, 1 for bla.css and 1 for bla.img
    for (int i = 0; i < 3; i++) {
        boolean isProcessed = filter.processRequest(request, response, chain);
        assertFalse(isProcessed);
        verify(application, Mockito.never()).newWebRequest(Matchers.eq(request), Matchers.anyString());
        verify(application, Mockito.never()).newWebResponse(Matchers.any(WebRequest.class), Matchers.eq(response));
        verify(chain, Mockito.times(i + 1)).doFilter(request, response);
    }
    // execute the request to /something/real
    boolean isProcessed = filter.processRequest(request, response, chain);
    assertTrue(isProcessed);
    verify(application).newWebRequest(Matchers.eq(request), Matchers.anyString());
    verify(application).newWebResponse(Matchers.any(WebRequest.class), Matchers.eq(response));
    // the request is processed so the chain is not executed
    verify(chain, Mockito.times(3)).doFilter(request, response);
}
Also used : Locale(java.util.Locale) MockApplication(org.apache.wicket.mock.MockApplication) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) WebRequest(org.apache.wicket.request.http.WebRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 3 with MockApplication

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

Example 4 with MockApplication

use of org.apache.wicket.mock.MockApplication in project wicket by apache.

the class InterceptTest method newApplication.

@Override
protected WebApplication newApplication() {
    return new MockApplication() {

        @Override
        protected void init() {
            getSecuritySettings().setAuthorizationStrategy(new IAuthorizationStrategy.AllowAllAuthorizationStrategy() {

                private boolean block = true;

                @Override
                public <T extends IRequestableComponent> boolean isInstantiationAuthorized(Class<T> componentClass) {
                    if (block && (componentClass == TargetPage.class || componentClass == HomePage.class)) {
                        block = false;
                        throw new RestartResponseAtInterceptPageException(InterceptPage.class);
                    }
                    return true;
                }
            });
            super.init();
        }

        @Override
        public Class<? extends Page> getHomePage() {
            return HomePage.class;
        }
    };
}
Also used : MockApplication(org.apache.wicket.mock.MockApplication) RestartResponseAtInterceptPageException(org.apache.wicket.RestartResponseAtInterceptPageException) IAuthorizationStrategy(org.apache.wicket.authorization.IAuthorizationStrategy)

Example 5 with MockApplication

use of org.apache.wicket.mock.MockApplication in project wicket by apache.

the class ApplicationSettingsTest method testDefaultStringResourceLoaderSetup.

/**
 */
@Test
public void testDefaultStringResourceLoaderSetup() {
    ResourceSettings settings = new ResourceSettings(new MockApplication());
    List<IStringResourceLoader> loaders = settings.getStringResourceLoaders();
    Assert.assertEquals("There should be 5 default loaders", 5, loaders.size());
    Assert.assertTrue("First loader one should be the component one", loaders.get(0) instanceof ComponentStringResourceLoader);
    Assert.assertTrue("Second loader should be the package one", loaders.get(1) instanceof PackageStringResourceLoader);
    Assert.assertTrue("Third loader should be the application one", loaders.get(2) instanceof ClassStringResourceLoader);
    Assert.assertTrue("Fourth loader should be the validator one", loaders.get(3) instanceof ValidatorStringResourceLoader);
    Assert.assertTrue("Fifth should be the initializer one", loaders.get(4) instanceof InitializerStringResourceLoader);
}
Also used : PackageStringResourceLoader(org.apache.wicket.resource.loader.PackageStringResourceLoader) ComponentStringResourceLoader(org.apache.wicket.resource.loader.ComponentStringResourceLoader) MockApplication(org.apache.wicket.mock.MockApplication) ResourceSettings(org.apache.wicket.settings.ResourceSettings) InitializerStringResourceLoader(org.apache.wicket.resource.loader.InitializerStringResourceLoader) IStringResourceLoader(org.apache.wicket.resource.loader.IStringResourceLoader) ValidatorStringResourceLoader(org.apache.wicket.resource.loader.ValidatorStringResourceLoader) ClassStringResourceLoader(org.apache.wicket.resource.loader.ClassStringResourceLoader) Test(org.junit.Test)

Aggregations

MockApplication (org.apache.wicket.mock.MockApplication)32 Test (org.junit.Test)14 ResourceSettings (org.apache.wicket.settings.ResourceSettings)6 WicketTester (org.apache.wicket.util.tester.WicketTester)6 Request (org.apache.wicket.request.Request)4 Response (org.apache.wicket.request.Response)4 Before (org.junit.Before)4 FilterChain (javax.servlet.FilterChain)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Session (org.apache.wicket.Session)3 IAuthorizationStrategy (org.apache.wicket.authorization.IAuthorizationStrategy)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)3 IOException (java.io.IOException)2 Locale (java.util.Locale)2 ServletException (javax.servlet.ServletException)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 Application (org.apache.wicket.Application)2