From 189441891552f455b1ec3cd8c3b0f9a13255ae55 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 18 Aug 2014 07:55:10 +0000 Subject: [PATCH] HTTPCLIENT-1542: Caching HttpClient uses absolute URIs for validation (follow-up patch, more tests) Based on contribution by Joseph Walton git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1618558 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/client/cache/CachingExec.java | 2 +- .../impl/client/cache/TestCachingExec.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java index 338d26852..937d0fdc6 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java @@ -745,7 +745,7 @@ public class CachingExec implements ClientExecChain { final URI uri = conditionalRequest.getURI(); if (uri != null) { try { - request.setURI(URIUtils.rewriteURIForRoute(uri, route)); + conditionalRequest.setURI(URIUtils.rewriteURIForRoute(uri, route)); } catch (final URISyntaxException ex) { throw new ProtocolException("Invalid URI: " + uri, ex); } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java index 018d141e9..abda62ae7 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java @@ -53,6 +53,7 @@ import org.apache.http.HttpVersion; import org.apache.http.client.cache.HttpCacheEntry; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpExecutionAware; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.DateUtils; @@ -308,6 +309,36 @@ public class TestCachingExec extends TestCachingExecChain { verifyMocks(); } + @Test + public void testRevalidationRewritesAbsoluteUri() throws Exception { + + mockImplMethods(GET_CURRENT_DATE); + + // Fail on an unexpected request, rather than causing a later NPE + EasyMock.resetToStrict(mockBackend); + + final HttpRequestWrapper validate = HttpRequestWrapper.wrap( + new HttpGet("http://foo.example.com/resource")); + final HttpRequestWrapper relativeValidate = HttpRequestWrapper.wrap( + new BasicHttpRequest("GET", "/resource", HttpVersion.HTTP_1_1)); + final CloseableHttpResponse originResponse = Proxies.enhanceResponse( + new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "Okay")); + + conditionalRequestBuilderReturns(validate); + getCurrentDateReturns(requestDate); + + final CloseableHttpResponse resp = mockBackend.execute(EasyMock.isA(HttpRoute.class), + eqRequest(relativeValidate), EasyMock.isA(HttpClientContext.class), + EasyMock. isNull()); + expect(resp).andReturn(originResponse); + + getCurrentDateReturns(responseDate); + + replayMocks(); + impl.revalidateCacheEntry(route, request, context, null, entry); + verifyMocks(); + } + @Test public void testEndlessResponsesArePassedThrough() throws Exception { impl = createCachingExecChain(mockBackend, new BasicHttpCache(), CacheConfig.DEFAULT);