Search in sources :

Example 1 with MockRequestDispatcherFactory

use of org.apache.sling.servlethelpers.MockRequestDispatcherFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AmpModeForwardFilterTest method testFilteringWithoutAmpSelector.

@Test
void testFilteringWithoutAmpSelector() throws IOException, ServletException {
    context.request().setRequestDispatcherFactory(new MockRequestDispatcherFactory() {

        @Override
        public RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options) {
            return null;
        }

        @Override
        public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options) {
            return new MockRequestDispatcher(options);
        }
    });
    context.currentPage(AMP_SELECTOR);
    context.requestPathInfo().setSelectorString("campaign.content");
    context.requestPathInfo().setExtension("html");
    spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain);
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) MockRequestDispatcherFactory(org.apache.sling.servlethelpers.MockRequestDispatcherFactory) RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 2 with MockRequestDispatcherFactory

use of org.apache.sling.servlethelpers.MockRequestDispatcherFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AmpModeForwardFilterTest method setUp.

@BeforeEach
void setUp() {
    // use spy to verify the filter
    spyAmpModeForwardFilter = Mockito.spy(new AmpModeForwardFilter());
    filterChain = mock(FilterChain.class);
    MockRequestDispatcherFactory mockRequestDispatcherFactory = new MockRequestDispatcherFactory() {

        @Override
        public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) {
            return mock(RequestDispatcher.class);
        }

        @Override
        public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) {
            return mock(RequestDispatcher.class);
        }
    };
    context.request().setRequestDispatcherFactory(mockRequestDispatcherFactory);
    context.load().json(TEST_BASE + AmpTestContext.TEST_CONTENT_JSON, TEST_ROOT_PAGE);
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) FilterChain(javax.servlet.FilterChain) Resource(org.apache.sling.api.resource.Resource) MockRequestDispatcherFactory(org.apache.sling.servlethelpers.MockRequestDispatcherFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with MockRequestDispatcherFactory

use of org.apache.sling.servlethelpers.MockRequestDispatcherFactory in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class OptionsImplTest method getOptionsUnderTest.

private Options getOptionsUnderTest(String resourcePath) {
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource == null) {
        throw new IllegalStateException("Did you forget to define test resource " + resourcePath + "?");
    }
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    request.setResource(resource);
    SlingBindings bindings = new SlingBindings();
    bindings.put(SlingBindings.RESOURCE, resource);
    bindings.put(SlingBindings.REQUEST, request);
    bindings.put(SlingBindings.RESOLVER, context.resourceResolver());
    bindings.put(SlingBindings.RESPONSE, response);
    bindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
    request.setAttribute(SlingBindings.class.getName(), bindings);
    request.setRequestDispatcherFactory(new MockRequestDispatcherFactory() {

        @Override
        public RequestDispatcher getRequestDispatcher(String s, RequestDispatcherOptions requestDispatcherOptions) {
            return null;
        }

        @Override
        public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions requestDispatcherOptions) {
            return new MockRequestDispatcher(requestDispatcherOptions);
        }
    });
    return request.adaptTo(Options.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) MockRequestDispatcherFactory(org.apache.sling.servlethelpers.MockRequestDispatcherFactory) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)3 Resource (org.apache.sling.api.resource.Resource)3 MockRequestDispatcherFactory (org.apache.sling.servlethelpers.MockRequestDispatcherFactory)3 RequestDispatcher (javax.servlet.RequestDispatcher)2 FilterChain (javax.servlet.FilterChain)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)1 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1