### Question:
GzipFilter implements Filter { public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException, ServletException { final HttpServletRequest request = (HttpServletRequest) req; final HttpServletResponse response = (HttpServletResponse) res; if (isGzipAllowed(request)) { doGzipResponse(request, response, chain); } else { LOG.debug("Gzip not allowed. Proceeding with chain."); chain.doFilter(request, response); } }  void init(final FilterConfig filterConfig); void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain); void destroy();  }### Answer:
@Test public void shouldChainWhenGzipIsNotSupported() throws Exception { victim.doFilter(mockRequest, mockResponse, mockFilterChain); verify(mockFilterChain, Mockito.times(1)).doFilter(mockRequest, mockResponse); }
@Test public void shouldGzipWhenGzipSupported() throws Exception { markGzipAsAllowed(); final String content = "sampleContent"; final InputStream sourceStream = new ByteArrayInputStream(content.getBytes()); final ByteArrayOutputStream targetStream = new ByteArrayOutputStream(); when(mockRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(sourceStream)); when(mockResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(targetStream)); victim.doFilter(mockRequest, mockResponse, mockFilterChain); verify(mockFilterChain, Mockito.never()).doFilter(mockRequest, mockResponse); verify(mockFilterChain, Mockito.times(1)).doFilter(Mockito.eq(mockRequest), Mockito.any(HttpServletResponse.class)); final InputStream ungzippedStream = new GZIPInputStream(new ByteArrayInputStream(targetStream.toByteArray())); assertEquals(content, IOUtils.toString(ungzippedStream)); }
@Test public void shouldGzipWhenGzipSupported() throws Exception { markGzipAsAllowed(); final String content = "sampleContent"; final InputStream sourceStream = new ByteArrayInputStream(content.getBytes()); final ByteArrayOutputStream targetStream = new ByteArrayOutputStream(); when(mockRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(sourceStream)); when(mockResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(targetStream)); victim.doFilter(mockRequest, mockResponse, mockFilterChain); verify(mockFilterChain, Mockito.never()).doFilter(mockRequest, mockResponse); verify(mockFilterChain, Mockito.times(1)).doFilter(Mockito.eq(mockRequest), Mockito.any(HttpServletResponse.class)); final InputStream ungzippedStream = new GZIPInputStream(new ByteArrayInputStream(targetStream.toByteArray())); assertEquals(content, IOUtils.toString(ungzippedStream, Charset.defaultCharset())); }