mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-16 23:16:33 +00:00
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 HttpCacheEntry(
|
||||
if (responseHeaders == 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.responseDate = responseDate;
|
||||
this.statusLine = statusLine;
|
||||
|
@ -35,6 +35,7 @@
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.cache.Resource;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
@Immutable
|
||||
@ -66,7 +67,8 @@ public boolean isRepeatable() {
|
||||
}
|
||||
|
||||
public long getContentLength() {
|
||||
return this.cacheEntry.getResource().length();
|
||||
Resource resource = this.cacheEntry.getResource();
|
||||
return (resource != null) ? resource.length() : 0L;
|
||||
}
|
||||
|
||||
public InputStream getContent() throws IOException {
|
||||
|
@ -177,16 +177,6 @@ public void mustProvideResponseHeaders() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mustProvideResource() {
|
||||
try {
|
||||
new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||
new Header[]{}, null);
|
||||
fail("Should have thrown exception");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canRetrieveOriginalStatusLine() {
|
||||
entry = new HttpCacheEntry(new Date(), new Date(), statusLine,
|
||||
|
@ -50,6 +50,7 @@ public class DummyHttpClient implements HttpClient {
|
||||
private ClientConnectionManager connManager = new SingleClientConnManager();
|
||||
private HttpRequest request;
|
||||
private HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP",1,1), HttpStatus.SC_OK, "OK");
|
||||
private int executions = 0;
|
||||
|
||||
public void setParams(HttpParams params) {
|
||||
this.params = params;
|
||||
@ -78,24 +79,28 @@ public HttpRequest getCapturedRequest() {
|
||||
public HttpResponse execute(HttpUriRequest request) throws IOException,
|
||||
ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse execute(HttpUriRequest request, HttpContext context)
|
||||
throws IOException, ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse execute(HttpHost target, HttpRequest request)
|
||||
throws IOException, ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse execute(HttpHost target, HttpRequest request,
|
||||
HttpContext context) throws IOException, ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -103,6 +108,7 @@ public <T> T execute(HttpUriRequest request,
|
||||
ResponseHandler<? extends T> responseHandler) throws IOException,
|
||||
ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return responseHandler.handleResponse(response);
|
||||
}
|
||||
|
||||
@ -110,6 +116,7 @@ public <T> T execute(HttpUriRequest request,
|
||||
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
||||
throws IOException, ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return responseHandler.handleResponse(response);
|
||||
}
|
||||
|
||||
@ -117,6 +124,7 @@ public <T> T execute(HttpHost target, HttpRequest request,
|
||||
ResponseHandler<? extends T> responseHandler) throws IOException,
|
||||
ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return responseHandler.handleResponse(response);
|
||||
}
|
||||
|
||||
@ -124,7 +132,11 @@ public <T> T execute(HttpHost target, HttpRequest request,
|
||||
ResponseHandler<? extends T> responseHandler, HttpContext context)
|
||||
throws IOException, ClientProtocolException {
|
||||
this.request = request;
|
||||
executions++;
|
||||
return responseHandler.handleResponse(response);
|
||||
}
|
||||
|
||||
public int getExecutions() {
|
||||
return executions;
|
||||
}
|
||||
}
|
||||
|
@ -2003,6 +2003,19 @@ public void testAllowsBackendToSetContextVariablesOnRevalidation() throws Except
|
||||
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 {
|
||||
expect(mockCache.getCacheEntry(host, request)).andReturn(result);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user