GzipCompressorProcessor implements ResponseProcessor { @Override public byte[] process(final HttpServletRequest req, final HttpServletResponse resp, final byte[] responseBody) { byte[] result = responseBody; if (isGzipCompressionNeeded(req, resp)) { resp.addHeader(HEADER_KEY_CONTENT_ENCODING, HEADER_VALUE_GZIP); result = compressResponseStreamWithGzip(inputStreamFactory.createByteArrayInputStream(responseBody)); } return result; }  @Override byte[] process(final HttpServletRequest req, final HttpServletResponse resp, final byte[] responseBody);  }
@Test public void testProcessShouldCheckTheAcceptEncodingHeaderInTheRequest() { underTest.process(req, resp, responseBody); verify(req).getHeader(HEADER_KEY_ACCEPT_ENCODING); }
@Test public void testProcessShouldDoNothingWhenGzipCompressionIsNotNeeded() { given(req.getHeader(HEADER_KEY_ACCEPT_ENCODING)).willReturn("nothing"); underTest.process(req, resp, responseBody); verify(resp, times(0)).addHeader(HEADER_KEY_CONTENT_ENCODING, HEADER_VALUE_GZIP); verify(gzipCompressor, times(0)).compress(any(ByteArrayInputStream.class)); }
@Test public void testProcessShouldAddContentEncodingHeaderWithValueAndCompressResponseBodyWhenGzipCompressionIsNeeded() { given(req.getHeader(HEADER_KEY_ACCEPT_ENCODING)).willReturn(HEADER_VALUE_GZIP); given(gzipCompressor.compress(any(ByteArrayInputStream.class))).willReturn(outputStream); given(outputStream.toByteArray()).willReturn(responseBody); byte[] actual = underTest.process(req, resp, responseBody); verify(resp).addHeader(HEADER_KEY_CONTENT_ENCODING, HEADER_VALUE_GZIP); assertEquals(actual, responseBody); }