HTTPCLIENT-1202: Allow for caching responses without bodies.
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1347764 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2748f2748a
commit
c429b34ca9
|
@ -97,9 +97,6 @@ public class HttpCacheEntry implements Serializable {
|
||||||
if (responseHeaders == null) {
|
if (responseHeaders == null) {
|
||||||
throw new IllegalArgumentException("Response headers may not be null");
|
throw new IllegalArgumentException("Response headers may not be null");
|
||||||
}
|
}
|
||||||
if (resource == null) {
|
|
||||||
throw new IllegalArgumentException("Resource may not be null");
|
|
||||||
}
|
|
||||||
this.requestDate = requestDate;
|
this.requestDate = requestDate;
|
||||||
this.responseDate = responseDate;
|
this.responseDate = responseDate;
|
||||||
this.statusLine = statusLine;
|
this.statusLine = statusLine;
|
||||||
|
|
|
@ -35,6 +35,7 @@ import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.annotation.Immutable;
|
import org.apache.http.annotation.Immutable;
|
||||||
import org.apache.http.client.cache.HttpCacheEntry;
|
import org.apache.http.client.cache.HttpCacheEntry;
|
||||||
|
import org.apache.http.client.cache.Resource;
|
||||||
import org.apache.http.protocol.HTTP;
|
import org.apache.http.protocol.HTTP;
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
|
@ -66,7 +67,8 @@ class CacheEntity implements HttpEntity, Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getContentLength() {
|
public long getContentLength() {
|
||||||
return this.cacheEntry.getResource().length();
|
Resource resource = this.cacheEntry.getResource();
|
||||||
|
return (resource != null) ? resource.length() : 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getContent() throws IOException {
|
public InputStream getContent() throws IOException {
|
||||||
|
|
|
@ -177,16 +177,6 @@ public class TestHttpCacheEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void mustProvideResource() {
|
|
||||||
try {
|
|
||||||
new HttpCacheEntry(new Date(), new Date(), statusLine,
|
|
||||||
new Header[]{}, null);
|
|
||||||
fail("Should have thrown exception");
|
|
||||||
} catch (IllegalArgumentException expected) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canRetrieveOriginalStatusLine() {
|
public void canRetrieveOriginalStatusLine() {
|
||||||
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||||
|
|
|
@ -50,6 +50,7 @@ public class DummyHttpClient implements HttpClient {
|
||||||
private ClientConnectionManager connManager = new SingleClientConnManager();
|
private ClientConnectionManager connManager = new SingleClientConnManager();
|
||||||
private HttpRequest request;
|
private HttpRequest request;
|
||||||
private HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP",1,1), HttpStatus.SC_OK, "OK");
|
private HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP",1,1), HttpStatus.SC_OK, "OK");
|
||||||
|
private int executions = 0;
|
||||||
|
|
||||||
public void setParams(HttpParams params) {
|
public void setParams(HttpParams params) {
|
||||||
this.params = params;
|
this.params = params;
|
||||||
|
@ -78,24 +79,28 @@ public class DummyHttpClient implements HttpClient {
|
||||||
public HttpResponse execute(HttpUriRequest request) throws IOException,
|
public HttpResponse execute(HttpUriRequest request) throws IOException,
|
||||||
ClientProtocolException {
|
ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse execute(HttpUriRequest request, HttpContext context)
|
public HttpResponse execute(HttpUriRequest request, HttpContext context)
|
||||||
throws IOException, ClientProtocolException {
|
throws IOException, ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse execute(HttpHost target, HttpRequest request)
|
public HttpResponse execute(HttpHost target, HttpRequest request)
|
||||||
throws IOException, ClientProtocolException {
|
throws IOException, ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse execute(HttpHost target, HttpRequest request,
|
public HttpResponse execute(HttpHost target, HttpRequest request,
|
||||||
HttpContext context) throws IOException, ClientProtocolException {
|
HttpContext context) throws IOException, ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +108,7 @@ public class DummyHttpClient implements HttpClient {
|
||||||
ResponseHandler<? extends T> responseHandler) throws IOException,
|
ResponseHandler<? extends T> responseHandler) throws IOException,
|
||||||
ClientProtocolException {
|
ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return responseHandler.handleResponse(response);
|
return responseHandler.handleResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +116,7 @@ public class DummyHttpClient implements HttpClient {
|
||||||
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
||||||
throws IOException, ClientProtocolException {
|
throws IOException, ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return responseHandler.handleResponse(response);
|
return responseHandler.handleResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,6 +124,7 @@ public class DummyHttpClient implements HttpClient {
|
||||||
ResponseHandler<? extends T> responseHandler) throws IOException,
|
ResponseHandler<? extends T> responseHandler) throws IOException,
|
||||||
ClientProtocolException {
|
ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return responseHandler.handleResponse(response);
|
return responseHandler.handleResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +132,11 @@ public class DummyHttpClient implements HttpClient {
|
||||||
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
||||||
throws IOException, ClientProtocolException {
|
throws IOException, ClientProtocolException {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
executions++;
|
||||||
return responseHandler.handleResponse(response);
|
return responseHandler.handleResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getExecutions() {
|
||||||
|
return executions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2003,6 +2003,19 @@ public class TestCachingHttpClient {
|
||||||
assertAllContextVariablesAreEqualTo(ctx, value);
|
assertAllContextVariablesAreEqualTo(ctx, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCanCacheAResponseWithoutABody() throws Exception {
|
||||||
|
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "No Content");
|
||||||
|
response.setHeader("Date", DateUtils.formatDate(new Date()));
|
||||||
|
response.setHeader("Cache-Control","max-age=300");
|
||||||
|
DummyHttpClient backend = new DummyHttpClient();
|
||||||
|
backend.setResponse(response);
|
||||||
|
impl = new CachingHttpClient(backend);
|
||||||
|
impl.execute(host, request);
|
||||||
|
impl.execute(host, request);
|
||||||
|
assertEquals(1, backend.getExecutions());
|
||||||
|
}
|
||||||
|
|
||||||
private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
|
private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
|
||||||
expect(mockCache.getCacheEntry(host, request)).andReturn(result);
|
expect(mockCache.getCacheEntry(host, request)).andReturn(result);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue