HTTPCLIENT-1009: content-type / content-encoding headers on cache response entities

Contributed by Felix Berger <bflat1 at gmx.net>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1006348 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-10-10 19:59:39 +00:00
parent c1f9d2b336
commit 0f2cc20b22
2 changed files with 30 additions and 1 deletions

View File

@ -117,7 +117,13 @@ class SizeLimitedResponseReader {
ensureConsumed(); ensureConsumed();
HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine()); HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine());
reconstructed.setHeaders(response.getAllHeaders()); 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; return reconstructed;
} }

View File

@ -26,12 +26,14 @@
*/ */
package org.apache.http.impl.client.cache; package org.apache.http.impl.client.cache;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.Assert; import org.junit.Assert;
@ -113,6 +115,27 @@ public class TestSizeLimitedResponseReader {
Assert.assertFalse(tooLarge); 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 @Test
public void testResponseCopiesAllOriginalHeaders() throws Exception { public void testResponseCopiesAllOriginalHeaders() throws Exception {
byte[] buf = new byte[] { 1, 2, 3 }; byte[] buf = new byte[] { 1, 2, 3 };