/** * Creates the {@link com.mockrunner.mock.web.MockFilterChain} using <code>new</code>. * This method can be overridden to return a subclass of {@link com.mockrunner.mock.web.MockFilterChain}. * @return the {@link com.mockrunner.mock.web.MockFilterChain} */ public MockFilterChain createMockFilterChain() { return new MockFilterChain(); }
/** * Deletes all filters in the filter chain. */ public void releaseFilters() { mockFactory.getMockFilterChain().release(); mockFactory.getMockFilterChain().setServlet(servlet); }
/** * Returns the last request from the filter chain. Since * filters can replace the request with a request wrapper, * this method makes only sense after calling at least * one filter, i.e. after calling {@link #doFilter} or * after calling one servlet method with <i>doChain</i> * set to <code>true</code>. * @return the filtered request */ public ServletRequest getFilteredRequest() { return mockFactory.getMockFilterChain().getLastRequest(); }
/** * Clears all filters and sets the current servlet to <code>null</code>. */ public void release() { filters.clear(); setServlet(null); reset(); }
public void testFilterIp() throws IOException, ServletException { Properties props = new Properties(); props.put("ip.blacklist", "192.168.1.8,192.168.1.10"); IpBlacklistFilter filter = new IpBlacklistFilter(props); assertNotNull(filter); MockFilterChain filterChain = new MockFilterChain(); filterChain.addFilter(filter); TestServlet testServlet = new TestServlet(); filterChain.setServlet(testServlet); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRemoteAddr("192.168.1.8"); MockHttpServletResponse response = new MockHttpServletResponse(); filterChain.doFilter(request, response); assertFalse(testServlet.wasServiceCalled()); testServlet.reset(); request.setRemoteAddr("192.168.1.9"); filterChain.doFilter(request, response); assertTrue(testServlet.wasServiceCalled()); testServlet.reset(); request.setRemoteAddr("192.168.1.10"); filterChain.doFilter(request, response); assertFalse(testServlet.wasServiceCalled()); testServlet.reset(); }
@Test public void testMultipartFilterFileDeletion() throws Throwable { //GIVEN OncePerRequestAbstractMgnlFilter testFilter = getTestFilter(); MultipartRequestFilter multiFilter = new MultipartRequestFilter(); filterChain = new MockFilterChain(); ((MockFilterChain) filterChain).addFilter(testFilter); //WHEN doTest(multiFilter, "text/xml; charset=ISO-8859-1"); //THEN verify(file).delete(); }
@Before public void setUp() throws Exception { dao = new DummyMonitorDAO(); filter = new MonitorFilter(new Monitor(dao), new MonitorRequestFilter()); chain = new MockFilterChain(); chain.setServlet(new HttpServlet() { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { req.getInputStream().read(new byte[LONG_BODY_SIZE]); res.getOutputStream().write(new byte[0]); } }); filter.monitor.config.props.put("maxBodySize", Integer.toString(MAX_BODY_SIZE)); // Ensure the configured property is correct for the tests }
/** * Sets the specified servlet as the current servlet. * Initializes it, if <code>doInit</code> is <code>true</code>. * @param servlet the servlet * @param doInit should <code>init</code> be called */ public void setServlet(HttpServlet servlet, boolean doInit) { try { this.servlet = servlet; if(doInit) { servlet.init(mockFactory.getMockServletConfig()); } mockFactory.getMockFilterChain().setServlet(servlet); } catch(Exception exc) { throw new NestedApplicationException(exc); } }
/** * Loops through the filter chain and calls the current servlets * <code>service</code> method at the end (only if a current servlet * is set). You can use it to test single filters or the interaction * of filters and servlets. * If you set <i>doChain</i> to <code>true</code> (use {@link #setDoChain}), * this method is called before any call of a servlet method. If a filter * does not call it's chains <code>doFilter</code> method, the chain * breaks and the servlet will not be called (just like it in the * real container). */ public void doFilter() { try { mockFactory.getMockFilterChain().doFilter(mockFactory.getWrappedRequest(), mockFactory.getWrappedResponse()); mockFactory.getMockFilterChain().reset(); } catch(Exception exc) { throw new NestedApplicationException(exc); } }
/** * Adds the specified filter it to the filter chain. Initializes it, * if <code>doInit</code> is <code>true</code>. * @param filter the filter * @param doInit should <code>init</code> be called */ public void addFilter(Filter filter, boolean doInit) { if(doInit) { try { filter.init(mockFactory.getMockFilterConfig()); } catch(Exception exc) { throw new NestedApplicationException(exc); } } mockFactory.getMockFilterChain().addFilter(filter); }
/** * Returns the last response from the filter chain. Since * filters can replace the response with a response wrapper, * this method makes only sense after calling at least * one filter, i.e. after calling {@link #doFilter} or * after calling one servlet method with <i>doChain</i> * set to <code>true</code>. * @return the filtered response */ public ServletResponse getFilteredResponse() { return mockFactory.getMockFilterChain().getLastResponse(); }
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { requestList.add(request); responseList.add(response); if(null == iterator) { iterator = filters.iterator(); } if(iterator.hasNext()) { Filter nextFilter = (Filter)iterator.next(); nextFilter.doFilter(request, response, this); } else { reset(); if(null == servlet) return; servlet.service(request, response); } }
@Test public void testCOSMultipartFilterFileDeletion() throws Throwable { //GIVEN final String TEMP_SUBDIR_PREFIX = "multipart"; OncePerRequestAbstractMgnlFilter testFilter = getTestFilter(); CosMultipartRequestFilter multiFilter = new CosMultipartRequestFilter(); filterChain = new MockFilterChain(); ((MockFilterChain) filterChain).addFilter(testFilter); //WHEN doTest(multiFilter, "text/xml"); //THEN verify(file).delete(); assertFalse("All multipart request subfolders were deleted.", Arrays.asList(Components.getComponent(FileSystemHelper.class).getTempDirectory().listFiles()).stream().anyMatch(file -> file.getName().startsWith(TEMP_SUBDIR_PREFIX))); }
/** * Clears all filters and sets the current servlet to <code>null</code>. */ public void release() { filters.clear(); setServlet(null); reset(); }
/** * Sets the specified servlet as the current servlet. * Initializes it, if <code>doInit</code> is <code>true</code>. * @param servlet the servlet * @param doInit should <code>init</code> be called */ public void setServlet(HttpServlet servlet, boolean doInit) { try { this.servlet = servlet; if(doInit) { servlet.init(mockFactory.getMockServletConfig()); } mockFactory.getMockFilterChain().setServlet(servlet); } catch(Exception exc) { throw new NestedApplicationException(exc); } }
/** * Loops through the filter chain and calls the current servlets * <code>service</code> method at the end (only if a current servlet * is set). You can use it to test single filters or the interaction * of filters and servlets. * If you set <i>doChain</i> to <code>true</code> (use {@link #setDoChain}), * this method is called before any call of a servlet method. If a filter * does not call it's chains <code>doFilter</code> method, the chain * breaks and the servlet will not be called (just like it in the * real container). */ public void doFilter() { try { mockFactory.getMockFilterChain().doFilter(mockFactory.getWrappedRequest(), mockFactory.getWrappedResponse()); mockFactory.getMockFilterChain().reset(); } catch(Exception exc) { throw new NestedApplicationException(exc); } }
/** * Adds the specified filter it to the filter chain. Initializes it, * if <code>doInit</code> is <code>true</code>. * @param filter the filter * @param doInit should <code>init</code> be called */ public void addFilter(Filter filter, boolean doInit) { if(doInit) { try { filter.init(mockFactory.getMockFilterConfig()); } catch(Exception exc) { throw new NestedApplicationException(exc); } } mockFactory.getMockFilterChain().addFilter(filter); }
/** * Returns the last response from the filter chain. Since * filters can replace the response with a response wrapper, * this method makes only sense after calling at least * one filter, i.e. after calling {@link #doFilter} or * after calling one servlet method with <i>doChain</i> * set to <code>true</code>. * @return the filtered response */ public ServletResponse getFilteredResponse() { return mockFactory.getMockFilterChain().getLastResponse(); }
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { requestList.add(request); responseList.add(response); if(null == iterator) { iterator = filters.iterator(); } if(iterator.hasNext()) { Filter nextFilter = (Filter)iterator.next(); nextFilter.doFilter(request, response, this); } else { reset(); if(null == servlet) return; servlet.service(request, response); } }
/** * Creates the {@link com.mockrunner.mock.web.MockFilterChain} using <code>new</code>. * This method can be overridden to return a subclass of {@link com.mockrunner.mock.web.MockFilterChain}. * @return the {@link com.mockrunner.mock.web.MockFilterChain} */ public MockFilterChain createMockFilterChain() { return new MockFilterChain(); }