FastInfosetCompressorProcessor implements ResponseProcessor { @Override public byte[] process(final HttpServletRequest req, final HttpServletResponse resp, final byte[] responseBody) { ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody); byte[] result = responseBody; if (isFastInfoSetCompressionNeeded(req, resp)) { resp.setContentType(ACCEPT_VALUE_FASTINFOSET); result = compressResponseXmlToFastInfoset(inputStream); } return result; }  @Override byte[] process(final HttpServletRequest req, final HttpServletResponse resp, final byte[] responseBody);  }
@Test public void testProcessShouldCheckAcceptHeaderOfTheRequest() { given(resp.getContentType()).willReturn(ACCEPT_VALUE_XML); underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(req).getHeader(ACCEPT_HEADER_KEY); }
@Test public void testProcessShouldDoNothingWhenResponseFISCompressionIsNotNeeded() { given(resp.getContentType()).willReturn("html"); given(req.getHeader(ACCEPT_HEADER_KEY)).willReturn(ACCEPT_VALUE_XML); underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(fastInfosetCompressor, times(0)).compress(any(ByteArrayInputStream.class)); verify(resp, times(0)).setHeader(ACCEPT_HEADER_KEY, ACCEPT_VALUE_FASTINFOSET); }
@Test public void testProcessShouldDoNothingWhenResponseFISCompressionIsNotNeededBecauseOfRequestHeader() { given(resp.getContentType()).willReturn(ACCEPT_VALUE_XML); given(req.getHeader(ACCEPT_HEADER_KEY)).willReturn(ACCEPT_VALUE_XML); underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(fastInfosetCompressor, times(0)).compress(any(ByteArrayInputStream.class)); verify(resp, times(0)).setHeader(ACCEPT_HEADER_KEY, ACCEPT_VALUE_FASTINFOSET); }
@Test public void testProcessShouldDoNothingWhenResponseFISCompressionIsNotNeededBecauseOfRespContentType() { given(resp.getContentType()).willReturn("html"); given(req.getHeader(ACCEPT_HEADER_KEY)).willReturn(ACCEPT_VALUE_FASTINFOSET); underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(fastInfosetCompressor, times(0)).compress(any(ByteArrayInputStream.class)); verify(resp, times(0)).setHeader(ACCEPT_HEADER_KEY, ACCEPT_VALUE_FASTINFOSET); }
@Test public void testProcessShouldCompressResponseBodyAndSetContentTypeWhenResponseFISCompressionIsNeeded() { given(resp.getContentType()).willReturn(ACCEPT_VALUE_XML); given(req.getHeader(ACCEPT_HEADER_KEY)).willReturn(ACCEPT_VALUE_FASTINFOSET); given(fastInfosetCompressor.compress(any(ByteArrayInputStream.class))).willReturn(outputStream); given(outputStream.toByteArray()).willReturn(RESPONSE_BODY.getBytes()); byte[] actual = underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(resp).setContentType(ACCEPT_VALUE_FASTINFOSET); assertEquals(actual, RESPONSE_BODY.getBytes()); }
@Test public void testProcessShouldCompressResponseBodyAndSetContentTypeWhenContentTypeIsFastinfoset() { given(resp.getContentType()).willReturn(ACCEPT_VALUE_FASTINFOSET); given(req.getHeader(ACCEPT_HEADER_KEY)).willReturn(ACCEPT_VALUE_FASTINFOSET); given(fastInfosetCompressor.compress(any(ByteArrayInputStream.class))).willReturn(outputStream); given(outputStream.toByteArray()).willReturn(RESPONSE_BODY.getBytes()); byte[] actual = underTest.process(req, resp, RESPONSE_BODY.getBytes()); verify(resp).setContentType(ACCEPT_VALUE_FASTINFOSET); assertEquals(actual, RESPONSE_BODY.getBytes()); }