AmpModeForwardFilter implements Filter { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; AMP_MODE ampMode = getAmpMode(slingRequest); Supplier<Stream<String>> selectors = () -> Stream.of(slingRequest.getRequestPathInfo().getSelectors()); if (selectors.get().anyMatch(a -> a.equals(AMP_SELECTOR)) || ampMode == AMP_MODE.AMP_ONLY) { RequestDispatcherOptions options = new RequestDispatcherOptions(); Stream<String> newSelectors = selectors.get().filter(e -> !e.equals(AMP_SELECTOR)); if (ampMode != AMP_MODE.NO_AMP) { newSelectors = Stream.concat(Stream.of(AMP_SELECTOR), newSelectors); } options.setReplaceSelectors(newSelectors.collect(Collectors.joining(DOT))); if (forward(slingRequest, response, options)) { return; } } chain.doFilter(request, response); }  @Override void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain); @Override void init(final FilterConfig config); @Override void destroy();  }
@Test void testFilteringWithNoSelector() throws IOException, ServletException { context.currentPage(AMP_PAGE_PROPERTY); context.currentResource(); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); }
@Test void testFilteringWithAmpSelector() throws IOException, ServletException { context.currentPage(AMP_SELECTOR); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); context.currentPage(AMP_SELECTOR); context.requestPathInfo().setResourcePath(AMP_SELECTOR); context.requestPathInfo().setSelectorString("amp"); context.requestPathInfo().setExtension("html"); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); context.requestPathInfo().setSelectorString(".amp"); context.requestPathInfo().setExtension("html"); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); context.requestPathInfo().setSelectorString("amp."); context.requestPathInfo().setExtension("html"); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); }
@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); }
@Test void testFilteringWithSelectorWithAmpMode() throws IOException, ServletException { context.currentPage(AMP_SELECTOR_WITH_AMP_MODE); context.requestPathInfo().setResourcePath(AMP_SELECTOR); context.requestPathInfo().setSelectorString(".amp"); context.requestPathInfo().setExtension("html"); spyAmpModeForwardFilter.doFilter(context.request(), context.response(), filterChain); verify(spyAmpModeForwardFilter, atLeastOnce()).doFilter(eq(context.request()), eq(context.response()) , eq(filterChain)); }