From 0f2cc20b22c565e997b5911085a25700c128c635 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 10 Oct 2010 19:59:39 +0000 Subject: [PATCH] HTTPCLIENT-1009: content-type / content-encoding headers on cache response entities Contributed by Felix Berger git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1006348 13f79535-47bb-0310-9956-ffa450edef68 --- .../cache/SizeLimitedResponseReader.java | 8 ++++++- .../cache/TestSizeLimitedResponseReader.java | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/SizeLimitedResponseReader.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/SizeLimitedResponseReader.java index e102bdb64..c4b5ed882 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/SizeLimitedResponseReader.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/SizeLimitedResponseReader.java @@ -117,7 +117,13 @@ HttpResponse getReconstructedResponse() throws IOException { ensureConsumed(); HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine()); reconstructed.setHeaders(response.getAllHeaders()); - reconstructed.setEntity(new CombinedEntity(resource, instream)); + CombinedEntity combinedEntity = new CombinedEntity(resource, instream); + HttpEntity entity = response.getEntity(); + if (entity != null) { + combinedEntity.setContentType(entity.getContentType()); + combinedEntity.setContentEncoding(entity.getContentEncoding()); + } + reconstructed.setEntity(combinedEntity); return reconstructed; } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java index 5db6dc813..53ba64ef1 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestSizeLimitedResponseReader.java @@ -26,12 +26,14 @@ */ package org.apache.http.impl.client.cache; +import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHttpResponse; import org.apache.http.util.EntityUtils; import org.junit.Assert; @@ -113,6 +115,27 @@ public void testResponseWithNoEntityIsNotTooLarge() throws Exception { Assert.assertFalse(tooLarge); } + @Test + public void testTooLargeEntityHasOriginalContentTypes() throws Exception { + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); + StringEntity entity = new StringEntity("large entity content", "text/plain", "utf-8"); + response.setEntity(entity); + + impl = new SizeLimitedResponseReader(new HeapResourceFactory(), MAX_SIZE, request, response); + + impl.readResponse(); + boolean tooLarge = impl.isLimitReached(); + HttpResponse result = impl.getReconstructedResponse(); + HttpEntity reconstructedEntity = result.getEntity(); + Assert.assertEquals(entity.getContentEncoding(), reconstructedEntity.getContentEncoding()); + Assert.assertEquals(entity.getContentType(), reconstructedEntity.getContentType()); + + String content = EntityUtils.toString(reconstructedEntity); + + Assert.assertTrue(tooLarge); + Assert.assertEquals("large entity content", content); + } + @Test public void testResponseCopiesAllOriginalHeaders() throws Exception { byte[] buf = new byte[] { 1, 2, 3 };