ResponseContentEncoding implements HttpResponseInterceptor { public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { String codecname = codec.getName().toLowerCase(Locale.US); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } else if ("identity".equals(codecname)) { return; } else { throw new HttpException("Unsupported Content-Coding: " + codec.getName()); } } } } }  void process(
            final HttpResponse response,
            final HttpContext context);  }
@Test public void testContentEncodingNoEntity() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNull(entity); }
@Test public void testNoContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("plain stuff"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity instanceof StringEntity); }
@Test public void testGzipContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("encoded stuff"); original.setContentEncoding("GZip"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity instanceof GzipDecompressingEntity); }
@Test public void testXGzipContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("encoded stuff"); original.setContentEncoding("x-gzip"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity instanceof GzipDecompressingEntity); }
@Test public void testDeflateContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("encoded stuff"); original.setContentEncoding("deFlaTe"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity instanceof DeflateDecompressingEntity); }
@Test public void testIdentityContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("encoded stuff"); original.setContentEncoding("identity"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity instanceof StringEntity); }
@Test(expected=HttpException.class) public void testUnknownContentEncoding() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); StringEntity original = new StringEntity("encoded stuff"); original.setContentEncoding("whatever"); response.setEntity(original); HttpContext context = new BasicHttpContext(); HttpResponseInterceptor interceptor = new ResponseContentEncoding(); interceptor.process(response, context); }