@Test public void prepareResponseBody_NeedsGZipping_butTooSmall() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); response.setHasBody(true); assertFalse(filter.shouldFilter(response)); }
@Test public void prepareResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true); //Force GZip for small response response.setHasBody(true); assertTrue(filter.shouldFilter(response)); final HttpResponseMessage result = filter.apply(response); final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain()); final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent()); final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()]; final int hc1Len = hc1.content().readableBytes(); final int hc2Len = hc2.content().readableBytes(); hc1.content().readBytes(body, 0, hc1Len); hc2.content().readBytes(body, hc1Len, hc2Len); // Check body is a gzipped version of the origin body. byte[] unzippedBytes = IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))); String bodyStr = new String(unzippedBytes, "UTF-8"); assertEquals("blah", bodyStr); assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding")); // Check Content-Length header has been removed.; assertEquals(0, result.getHeaders().get("Content-Length").size()); }
@Test public void prepareChunkedEncodedResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); response.getHeaders().set("Transfer-Encoding", "chunked"); response.setHasBody(true); assertTrue(filter.shouldFilter(response)); } }
@Test public void prepareResponseBody_NeedsGZipping_butTooSmall() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); response.setHasBody(true); assertFalse(filter.shouldFilter(response)); }
@Test public void prepareResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true); //Force GZip for small response response.setHasBody(true); assertTrue(filter.shouldFilter(response)); final HttpResponseMessage result = filter.apply(response); final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain()); final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent()); final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()]; final int hc1Len = hc1.content().readableBytes(); final int hc2Len = hc2.content().readableBytes(); hc1.content().readBytes(body, 0, hc1Len); hc2.content().readBytes(body, hc1Len, hc2Len); // Check body is a gzipped version of the origin body. byte[] unzippedBytes = IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))); String bodyStr = new String(unzippedBytes, "UTF-8"); assertEquals("blah", bodyStr); assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding")); // Check Content-Length header has been removed.; assertEquals(0, result.getHeaders().get("Content-Length").size()); }
@Test public void prepareChunkedEncodedResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); response.getHeaders().set("Transfer-Encoding", "chunked"); response.setHasBody(true); assertTrue(filter.shouldFilter(response)); } }
@Test public void prepareResponseBody_NeedsGZipping_butTooSmall() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); response.setHasBody(true); assertFalse(filter.shouldFilter(response)); }
@Test public void prepareResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true); //Force GZip for small response response.setHasBody(true); assertTrue(filter.shouldFilter(response)); final HttpResponseMessage result = filter.apply(response); final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain()); final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent()); final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()]; final int hc1Len = hc1.content().readableBytes(); final int hc2Len = hc2.content().readableBytes(); hc1.content().readBytes(body, 0, hc1Len); hc2.content().readBytes(body, hc1Len, hc2Len); // Check body is a gzipped version of the origin body. byte[] unzippedBytes = IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))); String bodyStr = new String(unzippedBytes, "UTF-8"); assertEquals("blah", bodyStr); assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding")); // Check Content-Length header has been removed.; assertEquals(0, result.getHeaders().get("Content-Length").size()); }
@Test public void prepareChunkedEncodedResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); response.getHeaders().set("Transfer-Encoding", "chunked"); response.setHasBody(true); assertTrue(filter.shouldFilter(response)); } }