@Test public void testDoFilterGzipResponseCompression() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.POST.name(), "http://localhost:8080/gzip"); request.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain( servlet, new GzipServletFilter(), new OncePerRequestFilter() { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.getOutputStream().write("Should be compressed".getBytes()); } }); filterChain.doFilter(request, response); ByteArrayOutputStream unzippedStream = new ByteArrayOutputStream(); StreamUtils.copy( new GZIPInputStream(new ByteArrayInputStream(response.getContentAsByteArray())), unzippedStream); String unzipped = new String(unzippedStream.toByteArray()); Assert.assertEquals(unzipped, "Should be compressed"); }
public void testFilterForwardsWhenPathDoesntMatch() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/editProfile.html"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); filter.doFilter(request, response, chain); assertNotNull(chain.getForwardURL()); }
public void testFilterDoesntForwardWhenPathMatches() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/scripts/dojo/test.html"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); filter.doFilter(request, response, chain); assertNull(chain.getForwardURL()); }
/** * Returns the destination path. * * @return the destination path */ @Override public String getDestinationPath() { String path = super.getDestinationPath(); if (path != null) { return path; } return filterChain.getPath(); }
@Test public void testDoFilterNoCompression() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.POST.name(), "http://localhost:8080/gzip"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain( servlet, new GzipServletFilter(), new OncePerRequestFilter() { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.getOutputStream().write("Should be compressed".getBytes()); } }); filterChain.doFilter(request, response); String unzipped = new String(response.getContentAsByteArray()); Assert.assertEquals(unzipped, "Should be compressed"); }
private HttpServletRequest testRemoteIpFilter(FilterDef filterDef, Request request) throws LifecycleException, IOException, ServletException { Tomcat tomcat = getTomcatInstance(); Context root = tomcat.addContext("", TEMP_DIR); RemoteIpFilter remoteIpFilter = new RemoteIpFilter(); filterDef.setFilterClass(RemoteIpFilter.class.getName()); filterDef.setFilter(remoteIpFilter); filterDef.setFilterName(RemoteIpFilter.class.getName()); root.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName(RemoteIpFilter.class.getName()); filterMap.addURLPattern("*"); root.addFilterMap(filterMap); getTomcatInstance().start(); MockFilterChain filterChain = new MockFilterChain(); // TEST remoteIpFilter.doFilter(request, new Response(), filterChain); return filterChain.getRequest(); }