Use Java 8 Time APIs for date / time operations in the HTTP cache tests

This commit is contained in:
Oleg Kalnichevski 2021-11-08 14:21:31 +01:00
parent 58386f857b
commit 3dd37952bc
18 changed files with 782 additions and 790 deletions

View File

@ -36,6 +36,8 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -49,22 +51,23 @@ import org.junit.Test;
public class TestHttpCacheEntry {
private Date now;
private Date elevenSecondsAgo;
private Date nineSecondsAgo;
private Instant now;
private Instant elevenSecondsAgo;
private Instant nineSecondsAgo;
private Resource mockResource;
private HttpCacheEntry entry;
@Before
public void setUp() {
now = new Date();
elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
now = Instant.now();
elevenSecondsAgo = now.minusSeconds(11);
nineSecondsAgo = now.minusSeconds(9);
mockResource = mock(Resource.class);
}
private HttpCacheEntry makeEntry(final Header[] headers) {
return new HttpCacheEntry(elevenSecondsAgo, nineSecondsAgo, HttpStatus.SC_OK, headers, mockResource);
return new HttpCacheEntry(DateUtils.toDate(elevenSecondsAgo), DateUtils.toDate(nineSecondsAgo),
HttpStatus.SC_OK, headers, mockResource);
}
@Test
@ -197,7 +200,7 @@ public class TestHttpCacheEntry {
public void canGetOriginalHeaders() {
final Header[] headers = {
new BasicHeader("Server", "MockServer/1.0"),
new BasicHeader("Date", DateUtils.formatDate(now))
new BasicHeader("Date", DateUtils.formatStandardDate(now))
};
entry = new HttpCacheEntry(new Date(), new Date(), HttpStatus.SC_OK, headers, mockResource);
final Header[] result = entry.getHeaders();
@ -282,15 +285,13 @@ public class TestHttpCacheEntry {
@Test
public void testValidDateHeaderIsParsed() {
final long nowMs = System.currentTimeMillis();
// round down to nearest second to make comparison easier
final Date date = new Date(nowMs - (nowMs % 1000L));
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(date)) };
final Instant date = Instant.now().with(ChronoField.MILLI_OF_SECOND, 0);
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatStandardDate(date)) };
entry = new HttpCacheEntry(new Date(), new Date(), HttpStatus.SC_OK,
headers, mockResource);
final Date dateHeaderValue = entry.getDate();
assertNotNull(dateHeaderValue);
assertEquals(date.getTime(), dateHeaderValue.getTime());
assertEquals(DateUtils.toDate(date), dateHeaderValue);
}
@Test

View File

@ -27,6 +27,8 @@
package org.apache.hc.client5.http.impl.cache;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@ -237,50 +239,51 @@ public class HttpTestUtils {
return new ByteArrayEntity(getRandomBytes(nbytes), null);
}
public static HttpCacheEntry makeCacheEntry(final Date requestDate, final Date responseDate) {
final Date when = new Date((responseDate.getTime() + requestDate.getTime()) / 2);
public static HttpCacheEntry makeCacheEntry(final Instant requestDate, final Instant responseDate) {
final Duration diff = Duration.between(requestDate, responseDate);
final Instant when = requestDate.plusMillis(diff.toMillis() / 2);
return makeCacheEntry(requestDate, responseDate, getStockHeaders(when));
}
public static Header[] getStockHeaders(final Date when) {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(when)),
public static Header[] getStockHeaders(final Instant when) {
return new Header[] {
new BasicHeader("Date", DateUtils.formatStandardDate(when)),
new BasicHeader("Server", "MockServer/1.0")
};
return headers;
}
public static HttpCacheEntry makeCacheEntry(final Date requestDate,
final Date responseDate, final Header... headers) {
public static HttpCacheEntry makeCacheEntry(final Instant requestDate,
final Instant responseDate, final Header... headers) {
final byte[] bytes = getRandomBytes(128);
return makeCacheEntry(requestDate, responseDate, headers, bytes);
}
public static HttpCacheEntry makeCacheEntry(final Date requestDate,
final Date responseDate, final Header[] headers, final byte[] bytes) {
return makeCacheEntry(requestDate, responseDate, headers, bytes,
null);
public static HttpCacheEntry makeCacheEntry(final Instant requestDate,
final Instant responseDate, final Header[] headers, final byte[] bytes) {
return makeCacheEntry(requestDate, responseDate, headers, bytes, null);
}
public static HttpCacheEntry makeCacheEntry(final Map<String,String> variantMap) {
final Date now = new Date();
final Instant now = Instant.now();
return makeCacheEntry(now, now, getStockHeaders(now),
getRandomBytes(128), variantMap);
}
public static HttpCacheEntry makeCacheEntry(final Date requestDate,
final Date responseDate, final Header[] headers, final byte[] bytes,
public static HttpCacheEntry makeCacheEntry(final Instant requestDate,
final Instant responseDate, final Header[] headers, final byte[] bytes,
final Map<String,String> variantMap) {
return new HttpCacheEntry(requestDate, responseDate, HttpStatus.SC_OK, headers, new HeapResource(bytes), variantMap);
return new HttpCacheEntry(DateUtils.toDate(requestDate), DateUtils.toDate(responseDate),
HttpStatus.SC_OK, headers, new HeapResource(bytes), variantMap);
}
public static HttpCacheEntry makeCacheEntry(final Header[] headers, final byte[] bytes) {
final Date now = new Date();
final Instant now = Instant.now();
return makeCacheEntry(now, now, headers, bytes);
}
public static HttpCacheEntry makeCacheEntry(final byte[] bytes) {
return makeCacheEntry(getStockHeaders(new Date()), bytes);
final Instant now = Instant.now();
return makeCacheEntry(getStockHeaders(now), bytes);
}
public static HttpCacheEntry makeCacheEntry(final Header... headers) {
@ -288,7 +291,7 @@ public class HttpTestUtils {
}
public static HttpCacheEntry makeCacheEntry() {
final Date now = new Date();
final Instant now = Instant.now();
return makeCacheEntry(now, now);
}
@ -319,16 +322,16 @@ public class HttpTestUtils {
public static ClassicHttpResponse make200Response() {
final ClassicHttpResponse out = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
out.setHeader("Date", DateUtils.formatDate(new Date()));
out.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
out.setHeader("Server", "MockOrigin/1.0");
out.setHeader("Content-Length", "128");
out.setEntity(makeBody(128));
return out;
}
public static final ClassicHttpResponse make200Response(final Date date, final String cacheControl) {
public static final ClassicHttpResponse make200Response(final Instant date, final String cacheControl) {
final ClassicHttpResponse response = HttpTestUtils.make200Response();
response.setHeader("Date", DateUtils.formatDate(date));
response.setHeader("Date", DateUtils.formatStandardDate(date));
response.setHeader("Cache-Control",cacheControl);
response.setHeader("Etag","\"etag\"");
return response;

View File

@ -32,6 +32,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
@ -134,7 +135,7 @@ public class TestBasicHttpCache {
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, new HttpGet("/bar"));
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Date", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -155,7 +156,7 @@ public class TestBasicHttpCache {
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, new HttpGet("/bar"));
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Date", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -224,7 +225,7 @@ public class TestBasicHttpCache {
final ByteArrayBuffer buf = HttpTestUtils.getRandomBuffer(128);
final HttpResponse origResponse = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
origResponse.setHeader("Date", DateUtils.formatDate(new Date()));
origResponse.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
origResponse.setHeader("Cache-Control", "max-age=3600, public");
origResponse.setHeader("ETag", "\"etag\"");
origResponse.setHeader("Vary", "Accept-Encoding");
@ -246,7 +247,7 @@ public class TestBasicHttpCache {
final ByteArrayBuffer buf = HttpTestUtils.getRandomBuffer(128);
final HttpResponse origResponse = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
origResponse.setHeader("Date", DateUtils.formatDate(new Date()));
origResponse.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
origResponse.setHeader("Cache-Control", "max-age=3600, public");
origResponse.setHeader("ETag", "\"etag\"");
origResponse.setHeader("Vary", "Accept-Encoding");
@ -278,7 +279,7 @@ public class TestBasicHttpCache {
req1.setHeader("Accept-Encoding", "gzip");
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "max-age=3600, public");
resp1.setHeader("ETag", "\"etag1\"");
resp1.setHeader("Vary", "Accept-Encoding");
@ -289,7 +290,7 @@ public class TestBasicHttpCache {
req2.setHeader("Accept-Encoding", "identity");
final HttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Date", DateUtils.formatDate(new Date()));
resp2.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp2.setHeader("Cache-Control", "max-age=3600, public");
resp2.setHeader("ETag", "\"etag2\"");
resp2.setHeader("Vary", "Accept-Encoding");

View File

@ -31,6 +31,7 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -48,28 +49,28 @@ import org.junit.Test;
public class TestCacheUpdateHandler {
private Date requestDate;
private Date responseDate;
private Instant requestDate;
private Instant responseDate;
private CacheUpdateHandler impl;
private HttpCacheEntry entry;
private Date now;
private Date oneSecondAgo;
private Date twoSecondsAgo;
private Date eightSecondsAgo;
private Date tenSecondsAgo;
private Instant now;
private Instant oneSecondAgo;
private Instant twoSecondsAgo;
private Instant eightSecondsAgo;
private Instant tenSecondsAgo;
private HttpResponse response;
@Before
public void setUp() throws Exception {
requestDate = new Date(System.currentTimeMillis() - 1000);
responseDate = new Date();
requestDate = Instant.now().minusSeconds(1);
responseDate = Instant.now();
now = new Date();
oneSecondAgo = new Date(now.getTime() - 1000L);
twoSecondsAgo = new Date(now.getTime() - 2000L);
eightSecondsAgo = new Date(now.getTime() - 8000L);
tenSecondsAgo = new Date(now.getTime() - 10000L);
now = Instant.now();
oneSecondAgo = now.minusSeconds(1);
twoSecondsAgo = now.minusSeconds(2);
eightSecondsAgo = now.minusSeconds(8);
tenSecondsAgo = now.minusSeconds(10);
response = new BasicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
@ -81,14 +82,14 @@ public class TestCacheUpdateHandler {
throws IOException {
entry = HttpTestUtils.makeCacheEntry();
final HttpCacheEntry newEntry = impl.updateCacheEntry(null, entry,
requestDate, responseDate, response);
DateUtils.toDate(requestDate), DateUtils.toDate(responseDate), response);
assertNotSame(newEntry, entry);
}
@Test
public void testHeadersAreMergedCorrectly() throws IOException {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(responseDate)),
new BasicHeader("Date", DateUtils.formatStandardDate(responseDate)),
new BasicHeader("ETag", "\"etag\"")};
entry = HttpTestUtils.makeCacheEntry(headers);
response.setHeaders();
@ -96,29 +97,29 @@ public class TestCacheUpdateHandler {
final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
new Date(), new Date(), response);
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatStandardDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", "\"etag\""));
}
@Test
public void testNewerHeadersReplaceExistingHeaders() throws IOException {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
new BasicHeader("Date", DateUtils.formatStandardDate(requestDate)),
new BasicHeader("Cache-Control", "private"),
new BasicHeader("ETag", "\"etag\""),
new BasicHeader("Last-Modified", DateUtils.formatDate(requestDate)),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(requestDate)),
new BasicHeader("Cache-Control", "max-age=0"),};
entry = HttpTestUtils.makeCacheEntry(headers);
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatStandardDate(responseDate)),
new BasicHeader("Cache-Control", "public"));
final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
new Date(), new Date(), response);
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatDate(requestDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatStandardDate(requestDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", "\"etag\""));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", DateUtils.formatDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", DateUtils.formatStandardDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Cache-Control", "public"));
}
@ -126,19 +127,19 @@ public class TestCacheUpdateHandler {
public void testNewHeadersAreAddedByMerge() throws IOException {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
new BasicHeader("Date", DateUtils.formatStandardDate(requestDate)),
new BasicHeader("ETag", "\"etag\"")};
entry = HttpTestUtils.makeCacheEntry(headers);
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatStandardDate(responseDate)),
new BasicHeader("Cache-Control", "public"));
final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
new Date(), new Date(), response);
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatDate(requestDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", DateUtils.formatStandardDate(requestDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", "\"etag\""));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", DateUtils.formatDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", DateUtils.formatStandardDate(responseDate)));
MatcherAssert. assertThat(updatedEntry, ContainsHeaderMatcher.contains("Cache-Control", "public"));
}
@ -146,15 +147,15 @@ public class TestCacheUpdateHandler {
public void oldHeadersRetainedIfResponseOlderThanEntry()
throws Exception {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(oneSecondAgo)),
new BasicHeader("ETag", "\"new-etag\"")
};
entry = HttpTestUtils.makeCacheEntry(twoSecondsAgo, now, headers);
response.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
response.setHeader("ETag", "\"old-etag\"");
final HttpCacheEntry result = impl.updateCacheEntry("A", entry, new Date(),
new Date(), response);
MatcherAssert. assertThat(result, ContainsHeaderMatcher.contains("Date", DateUtils.formatDate(oneSecondAgo)));
MatcherAssert. assertThat(result, ContainsHeaderMatcher.contains("Date", DateUtils.formatStandardDate(oneSecondAgo)));
MatcherAssert. assertThat(result, ContainsHeaderMatcher.contains("ETag", "\"new-etag\""));
}
@ -163,10 +164,10 @@ public class TestCacheUpdateHandler {
throws IOException {
entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
twoSecondsAgo, oneSecondAgo, response);
DateUtils.toDate(twoSecondsAgo), DateUtils.toDate(oneSecondAgo), response);
assertEquals(twoSecondsAgo, updated.getRequestDate());
assertEquals(oneSecondAgo, updated.getResponseDate());
assertEquals(DateUtils.toDate(twoSecondsAgo), updated.getRequestDate());
assertEquals(DateUtils.toDate(oneSecondAgo), updated.getResponseDate());
}
@Test
@ -174,13 +175,13 @@ public class TestCacheUpdateHandler {
final Header[] headers = {
new BasicHeader("Warning", "110 fred \"Response is stale\""),
new BasicHeader("ETag", "\"old\""),
new BasicHeader("Date", DateUtils.formatDate(eightSecondsAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(eightSecondsAgo))
};
entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
response.setHeader("ETag", "\"new\"");
response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(twoSecondsAgo));
final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
twoSecondsAgo, oneSecondAgo, response);
DateUtils.toDate(twoSecondsAgo), DateUtils.toDate(oneSecondAgo), response);
assertEquals(0, updated.getHeaders("Warning").length);
}
@ -193,9 +194,9 @@ public class TestCacheUpdateHandler {
};
entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
response.setHeader("ETag", "\"new\"");
response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(twoSecondsAgo));
final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
twoSecondsAgo, oneSecondAgo, response);
DateUtils.toDate(twoSecondsAgo), DateUtils.toDate(oneSecondAgo), response);
assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
}
@ -204,13 +205,13 @@ public class TestCacheUpdateHandler {
public void entryIsStillUpdatedByResponseWithMalformedDate() throws Exception {
final Header[] headers = {
new BasicHeader("ETag", "\"old\""),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo))
};
entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
response.setHeader("ETag", "\"new\"");
response.setHeader("Date", "bad-date");
final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
twoSecondsAgo, oneSecondAgo, response);
DateUtils.toDate(twoSecondsAgo), DateUtils.toDate(oneSecondAgo), response);
assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
}
@ -249,12 +250,12 @@ public class TestCacheUpdateHandler {
@Test
public void testContentEncodingHeaderIsNotUpdatedByMerge() throws IOException {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
new BasicHeader("Date", DateUtils.formatStandardDate(requestDate)),
new BasicHeader("ETag", "\"etag\""),
new BasicHeader("Content-Encoding", "identity")};
entry = HttpTestUtils.makeCacheEntry(headers);
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatStandardDate(responseDate)),
new BasicHeader("Cache-Control", "public"),
new BasicHeader("Content-Encoding", "gzip"));
@ -269,12 +270,12 @@ public class TestCacheUpdateHandler {
@Test
public void testContentLengthIsNotAddedWhenTransferEncodingIsPresent() throws IOException {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(requestDate)),
new BasicHeader("Date", DateUtils.formatStandardDate(requestDate)),
new BasicHeader("ETag", "\"etag\""),
new BasicHeader("Transfer-Encoding", "chunked")};
entry = HttpTestUtils.makeCacheEntry(headers);
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
response.setHeaders(new BasicHeader("Last-Modified", DateUtils.formatStandardDate(responseDate)),
new BasicHeader("Cache-Control", "public"),
new BasicHeader("Content-Length", "0"));

View File

@ -31,6 +31,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.time.Instant;
import java.util.Date;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@ -47,20 +48,20 @@ import org.junit.Test;
public class TestCacheValidityPolicy {
private CacheValidityPolicy impl;
private Date now;
private Date oneSecondAgo;
private Date sixSecondsAgo;
private Date tenSecondsAgo;
private Date elevenSecondsAgo;
private Instant now;
private Instant oneSecondAgo;
private Instant sixSecondsAgo;
private Instant tenSecondsAgo;
private Instant elevenSecondsAgo;
@Before
public void setUp() {
impl = new CacheValidityPolicy();
now = new Date();
oneSecondAgo = new Date(now.getTime() - 1 * 1000L);
sixSecondsAgo = new Date(now.getTime() - 6 * 1000L);
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
now = Instant.now();
oneSecondAgo = now.minusSeconds(1);
sixSecondsAgo = now.minusSeconds(6);
tenSecondsAgo = now.minusSeconds(10);
elevenSecondsAgo = now.minusSeconds(11);
}
@Test
@ -74,8 +75,7 @@ public class TestCacheValidityPolicy {
@Test
public void testApparentAgeIsResponseReceivedTimeLessDateHeader() {
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils
.formatDate(tenSecondsAgo)) };
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, sixSecondsAgo, headers);
assertEquals(TimeValue.ofSeconds(4), impl.getApparentAge(entry));
@ -83,8 +83,7 @@ public class TestCacheValidityPolicy {
@Test
public void testNegativeApparentAgeIsBroughtUpToZero() {
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils
.formatDate(sixSecondsAgo)) };
final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatStandardDate(sixSecondsAgo)) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, tenSecondsAgo, headers);
assertEquals(TimeValue.ofSeconds(0), impl.getApparentAge(entry));
}
@ -141,7 +140,7 @@ public class TestCacheValidityPolicy {
@Test
public void testResidentTimeSecondsIsTimeSinceResponseTime() {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, sixSecondsAgo);
assertEquals(TimeValue.ofSeconds(6), impl.getResidentTime(entry, now));
assertEquals(TimeValue.ofSeconds(6), impl.getResidentTime(entry, DateUtils.toDate(now)));
}
@Test
@ -190,8 +189,8 @@ public class TestCacheValidityPolicy {
@Test
public void testFreshnessLifetimeIsMaxAgeEvenIfExpiresIsPresent() {
final Header[] headers = new Header[] { new BasicHeader("Cache-Control", "max-age=10"),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatDate(sixSecondsAgo)) };
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo)) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertEquals(TimeValue.ofSeconds(10), impl.getFreshnessLifetime(entry));
@ -200,8 +199,8 @@ public class TestCacheValidityPolicy {
@Test
public void testFreshnessLifetimeIsSMaxAgeEvenIfExpiresIsPresent() {
final Header[] headers = new Header[] { new BasicHeader("Cache-Control", "s-maxage=10"),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatDate(sixSecondsAgo)) };
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo)) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertEquals(TimeValue.ofSeconds(10), impl.getFreshnessLifetime(entry));
@ -210,8 +209,8 @@ public class TestCacheValidityPolicy {
@Test
public void testFreshnessLifetimeIsFromExpiresHeaderIfNoMaxAge() {
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatDate(sixSecondsAgo)) };
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo)) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertEquals(TimeValue.ofSeconds(4), impl.getFreshnessLifetime(entry));
@ -220,8 +219,8 @@ public class TestCacheValidityPolicy {
@Test
public void testHeuristicFreshnessLifetime() {
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)),
new BasicHeader("Last-Modified", DateUtils.formatDate(elevenSecondsAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(oneSecondAgo)),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(elevenSecondsAgo))
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertEquals(TimeValue.ofSeconds(1), impl.getHeuristicFreshnessLifetime(entry, 0.1f, TimeValue.ZERO_MILLISECONDS));
@ -237,8 +236,8 @@ public class TestCacheValidityPolicy {
@Test
public void testHeuristicFreshnessLifetimeIsNonNegative() {
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(elevenSecondsAgo)),
new BasicHeader("Last-Modified", DateUtils.formatDate(oneSecondAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(elevenSecondsAgo)),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(oneSecondAgo))
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
@ -252,7 +251,7 @@ public class TestCacheValidityPolicy {
@Override
public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) {
assertSame(entry, e);
assertEquals(now, d);
assertEquals(DateUtils.toDate(now), d);
return TimeValue.ofSeconds(6);
}
@Override
@ -261,7 +260,7 @@ public class TestCacheValidityPolicy {
return TimeValue.ofSeconds(10);
}
};
assertTrue(impl.isResponseFresh(entry, now));
assertTrue(impl.isResponseFresh(entry, DateUtils.toDate(now)));
}
@Test
@ -270,7 +269,7 @@ public class TestCacheValidityPolicy {
impl = new CacheValidityPolicy() {
@Override
public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) {
assertEquals(now, d);
assertEquals(DateUtils.toDate(now), d);
assertSame(entry, e);
return TimeValue.ofSeconds(6);
}
@ -280,7 +279,7 @@ public class TestCacheValidityPolicy {
return TimeValue.ofSeconds(6);
}
};
assertFalse(impl.isResponseFresh(entry, now));
assertFalse(impl.isResponseFresh(entry, DateUtils.toDate(now)));
}
@Test
@ -289,7 +288,7 @@ public class TestCacheValidityPolicy {
impl = new CacheValidityPolicy() {
@Override
public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) {
assertEquals(now, d);
assertEquals(DateUtils.toDate(now), d);
assertSame(entry, e);
return TimeValue.ofSeconds(10);
}
@ -299,13 +298,13 @@ public class TestCacheValidityPolicy {
return TimeValue.ofSeconds(6);
}
};
assertFalse(impl.isResponseFresh(entry, now));
assertFalse(impl.isResponseFresh(entry, DateUtils.toDate(now)));
}
@Test
public void testCacheEntryIsRevalidatableIfHeadersIncludeETag() {
final Header[] headers = {
new BasicHeader("Expires", DateUtils.formatDate(new Date())),
new BasicHeader("Expires", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("ETag", "somevalue")};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertTrue(impl.isRevalidatable(entry));
@ -314,8 +313,8 @@ public class TestCacheValidityPolicy {
@Test
public void testCacheEntryIsRevalidatableIfHeadersIncludeLastModifiedDate() {
final Header[] headers = {
new BasicHeader("Expires", DateUtils.formatDate(new Date())),
new BasicHeader("Last-Modified", DateUtils.formatDate(new Date())) };
new BasicHeader("Expires", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now())) };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertTrue(impl.isRevalidatable(entry));
}
@ -323,7 +322,7 @@ public class TestCacheValidityPolicy {
@Test
public void testCacheEntryIsNotRevalidatableIfNoAppropriateHeaders() {
final Header[] headers = {
new BasicHeader("Expires", DateUtils.formatDate(new Date())),
new BasicHeader("Expires", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("Cache-Control", "public") };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertFalse(impl.isRevalidatable(entry));
@ -416,47 +415,47 @@ public class TestCacheValidityPolicy {
@Test
public void testMayReturnStaleIfErrorInResponseIsTrueWithinStaleness(){
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5, stale-if-error=15")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
final HttpRequest req = new BasicHttpRequest("GET","/");
assertTrue(impl.mayReturnStaleIfError(req, entry, now));
assertTrue(impl.mayReturnStaleIfError(req, entry, DateUtils.toDate(now)));
}
@Test
public void testMayReturnStaleIfErrorInRequestIsTrueWithinStaleness(){
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
final HttpRequest req = new BasicHttpRequest("GET","/");
req.setHeader("Cache-Control","stale-if-error=15");
assertTrue(impl.mayReturnStaleIfError(req, entry, now));
assertTrue(impl.mayReturnStaleIfError(req, entry, DateUtils.toDate(now)));
}
@Test
public void testMayNotReturnStaleIfErrorInResponseAndAfterResponseWindow(){
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5, stale-if-error=1")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
final HttpRequest req = new BasicHttpRequest("GET","/");
assertFalse(impl.mayReturnStaleIfError(req, entry, now));
assertFalse(impl.mayReturnStaleIfError(req, entry, DateUtils.toDate(now)));
}
@Test
public void testMayNotReturnStaleIfErrorInResponseAndAfterRequestWindow(){
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
final HttpRequest req = new BasicHttpRequest("GET","/");
req.setHeader("Cache-Control","stale-if-error=1");
assertFalse(impl.mayReturnStaleIfError(req, entry, now));
assertFalse(impl.mayReturnStaleIfError(req, entry, DateUtils.toDate(now)));
}
@Test
@ -464,40 +463,40 @@ public class TestCacheValidityPolicy {
final Header[] headers = new Header[] { new BasicHeader("Cache-control", "public") };
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, now));
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, DateUtils.toDate(now)));
}
@Test
public void testMayReturnStaleWhileRevalidatingIsTrueWhenWithinStaleness() {
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5, stale-while-revalidate=15")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
assertTrue(impl.mayReturnStaleWhileRevalidating(entry, now));
assertTrue(impl.mayReturnStaleWhileRevalidating(entry, DateUtils.toDate(now)));
}
@Test
public void testMayReturnStaleWhileRevalidatingIsFalseWhenPastStaleness() {
final Date twentyFiveSecondsAgo = new Date(now.getTime() - 25 * 1000L);
final Instant twentyFiveSecondsAgo = now.minusSeconds(25);
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(twentyFiveSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(twentyFiveSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5, stale-while-revalidate=15")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, now));
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, DateUtils.toDate(now)));
}
@Test
public void testMayReturnStaleWhileRevalidatingIsFalseWhenDirectiveEmpty() {
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5, stale-while-revalidate=")
};
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers);
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, now));
assertFalse(impl.mayReturnStaleWhileRevalidating(entry, DateUtils.toDate(now)));
}
}

View File

@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.util.Date;
import java.time.Instant;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.utils.DateUtils;
@ -42,10 +42,10 @@ import org.junit.Test;
public class TestCachedResponseSuitabilityChecker {
private Date now;
private Date elevenSecondsAgo;
private Date tenSecondsAgo;
private Date nineSecondsAgo;
private Instant now;
private Instant elevenSecondsAgo;
private Instant tenSecondsAgo;
private Instant nineSecondsAgo;
private HttpHost host;
private HttpRequest request;
@ -54,10 +54,10 @@ public class TestCachedResponseSuitabilityChecker {
@Before
public void setUp() {
now = new Date();
elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
now = Instant.now();
elevenSecondsAgo = now.minusSeconds(11);
tenSecondsAgo = now.minusSeconds(10);
nineSecondsAgo = now.minusSeconds(9);
host = new HttpHost("foo.example.com");
request = new BasicHttpRequest("GET", "/foo");
@ -73,118 +73,118 @@ public class TestCachedResponseSuitabilityChecker {
@Test
public void testNotSuitableIfContentLengthHeaderIsWrong() {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","1")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfCacheEntryIsFresh() {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfCacheEntryIsNotFresh() {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfRequestHasNoCache() {
request.addHeader("Cache-Control", "no-cache");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfAgeExceedsRequestMaxAge() {
request.addHeader("Cache-Control", "max-age=10");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfFreshAndAgeIsUnderRequestMaxAge() {
request.addHeader("Cache-Control", "max-age=15");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfFreshAndFreshnessLifetimeGreaterThanRequestMinFresh() {
request.addHeader("Cache-Control", "min-fresh=10");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfFreshnessLifetimeLessThanRequestMinFresh() {
request.addHeader("Cache-Control", "min-fresh=10");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=15"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableEvenIfStaleButPermittedByRequestMaxStale() {
request.addHeader("Cache-Control", "max-stale=10");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfStaleButTooStaleForRequestMaxStale() {
request.addHeader("Cache-Control", "max-stale=2");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=5"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@ -192,34 +192,34 @@ public class TestCachedResponseSuitabilityChecker {
public void testMalformedCacheControlMaxAgeRequestHeaderCausesUnsuitableEntry() {
request.addHeader(new BasicHeader("Cache-Control", "max-age=foo"));
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testMalformedCacheControlMinFreshRequestHeaderCausesUnsuitableEntry() {
request.addHeader(new BasicHeader("Cache-Control", "min-fresh=foo"));
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfCacheEntryIsHeuristicallyFreshEnough() {
final Date oneSecondAgo = new Date(now.getTime() - 1 * 1000L);
final Date twentyOneSecondsAgo = new Date(now.getTime() - 21 * 1000L);
final Instant oneSecondAgo = now.minusSeconds(1);
final Instant twentyOneSecondsAgo = now.minusSeconds(21);
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)),
new BasicHeader("Last-Modified", DateUtils.formatDate(twentyOneSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(oneSecondAgo)),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(twentyOneSecondsAgo)),
new BasicHeader("Content-Length", "128")
};
@ -230,13 +230,13 @@ public class TestCachedResponseSuitabilityChecker {
.setHeuristicCoefficient(0.1f).build();
impl = new CachedResponseSuitabilityChecker(config);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfCacheEntryIsHeuristicallyFreshEnoughByDefault() {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Content-Length", "128")
};
@ -248,70 +248,70 @@ public class TestCachedResponseSuitabilityChecker {
.build();
impl = new CachedResponseSuitabilityChecker(config);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableIfRequestMethodisHEAD() {
final HttpRequest headRequest = new BasicHttpRequest("HEAD", "/foo");
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = getEntry(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, headRequest, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, headRequest, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableIfRequestMethodIsGETAndEntryResourceIsNull() {
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = HttpTestUtils.makeHeadCacheEntry(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testNotSuitableForGETIfEntryDoesNotSpecifyARequestMethodOrEntity() {
impl = new CachedResponseSuitabilityChecker(CacheConfig.custom().build());
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = HttpTestUtils.makeCacheEntryWithNoRequestMethodOrEntity(headers);
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertFalse(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableForGETIfEntryDoesNotSpecifyARequestMethodButContainsEntity() {
impl = new CachedResponseSuitabilityChecker(CacheConfig.custom().build());
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = HttpTestUtils.makeCacheEntryWithNoRequestMethod(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
public void testSuitableForGETIfHeadResponseCachingEnabledAndEntryDoesNotSpecifyARequestMethodButContains204Response() {
impl = new CachedResponseSuitabilityChecker(CacheConfig.custom().build());
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600")
};
entry = HttpTestUtils.make204CacheEntryWithNoRequestMethod(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, DateUtils.toDate(now)));
}
@Test
@ -319,12 +319,12 @@ public class TestCachedResponseSuitabilityChecker {
final HttpRequest headRequest = new BasicHttpRequest("HEAD", "/foo");
impl = new CachedResponseSuitabilityChecker(CacheConfig.custom().build());
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Cache-Control", "max-age=3600"),
new BasicHeader("Content-Length","128")
};
entry = HttpTestUtils.makeHeadCacheEntryWithNoRequestMethod(headers);
Assert.assertTrue(impl.canCachedResponseBeUsed(host, headRequest, entry, now));
Assert.assertTrue(impl.canCachedResponseBeUsed(host, headRequest, entry, DateUtils.toDate(now)));
}
}

View File

@ -32,7 +32,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
@ -128,12 +129,12 @@ public class TestCachingExecChain {
@Test
public void testOlderCacheableResponsesDoNotGoIntoCache() throws Exception {
final Date now = new Date();
final Date fiveSecondsAgo = new Date(now.getTime() - 5 * 1000L);
final Instant now = Instant.now();
final Instant fiveSecondsAgo = now.minusSeconds(5);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "max-age=3600");
resp1.setHeader("Etag", "\"new-etag\"");
@ -141,7 +142,7 @@ public class TestCachingExecChain {
req2.setHeader("Cache-Control", "no-cache");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("ETag", "\"old-etag\"");
resp2.setHeader("Date", DateUtils.formatDate(fiveSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(fiveSecondsAgo));
resp2.setHeader("Cache-Control", "max-age=3600");
final ClassicHttpRequest req3 = HttpTestUtils.makeDefaultRequest();
@ -160,12 +161,12 @@ public class TestCachingExecChain {
@Test
public void testNewerCacheableResponsesReplaceExistingCacheEntry() throws Exception {
final Date now = new Date();
final Date fiveSecondsAgo = new Date(now.getTime() - 5 * 1000L);
final Instant now = Instant.now();
final Instant fiveSecondsAgo = now.minusSeconds(5);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(fiveSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(fiveSecondsAgo));
resp1.setHeader("Cache-Control", "max-age=3600");
resp1.setHeader("Etag", "\"old-etag\"");
@ -173,7 +174,7 @@ public class TestCachingExecChain {
req2.setHeader("Cache-Control", "max-age=0");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("ETag", "\"new-etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "max-age=3600");
final ClassicHttpRequest req3 = HttpTestUtils.makeDefaultRequest();
@ -280,7 +281,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -297,7 +298,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -315,7 +316,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -327,18 +328,18 @@ public class TestCachingExecChain {
@Test
public void testReturns304ForIfModifiedSinceHeaderIfRequestServedFromCache() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(now));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(now));
final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -349,18 +350,18 @@ public class TestCachingExecChain {
@Test
public void testReturns304ForIfModifiedSinceHeaderIf304ResponseInCache() throws Exception {
final Date now = new Date();
final Date oneHourAgo = new Date(now.getTime() - 3600 * 1000L);
final Date inTenMinutes = new Date(now.getTime() + 600 * 1000L);
final Instant now = Instant.now();
final Instant oneHourAgo = now.minus(1, ChronoUnit.HOURS);
final Instant inTenMinutes = now.plus(10, ChronoUnit.MINUTES);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
req1.addHeader("If-Modified-Since", DateUtils.formatDate(oneHourAgo));
req1.addHeader("If-Modified-Since", DateUtils.formatStandardDate(oneHourAgo));
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(oneHourAgo));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(oneHourAgo));
final ClassicHttpResponse resp1 = HttpTestUtils.make304Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-control", "max-age=600");
resp1.setHeader("Expires", DateUtils.formatDate(inTenMinutes));
resp1.setHeader("Expires", DateUtils.formatStandardDate(inTenMinutes));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -375,8 +376,8 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForIfModifiedSinceDateIsLess() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -384,12 +385,12 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(new Date()));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now()));
// The variant has been modified since this date
req2.addHeader("If-Modified-Since", DateUtils.formatDate(tenSecondsAgo));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
@ -405,8 +406,8 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForIfModifiedSinceDateIsInvalid() throws Exception {
final Date now = new Date();
final Date tenSecondsAfter = new Date(now.getTime() + 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAfter = now.plusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -414,12 +415,12 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(new Date()));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now()));
// invalid date (date in the future)
req2.addHeader("If-Modified-Since", DateUtils.formatDate(tenSecondsAfter));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(tenSecondsAfter));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -439,7 +440,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -459,7 +460,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(new Date()));
resp1.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp1.setHeader("Cache-Control", "public, max-age=3600");
req2.addHeader("If-None-Match", "\"abc\"");
@ -478,8 +479,8 @@ public class TestCachingExecChain {
@Test
public void testReturns304ForIfNoneMatchHeaderAndIfModifiedSinceIfRequestServedFromCache() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -487,12 +488,12 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(new Date()));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now()));
req2.addHeader("If-None-Match", "*");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(now));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -503,19 +504,19 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForIfNoneMatchHeaderFailsIfModifiedSinceIgnored() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
req2.addHeader("If-None-Match", "\"abc\"");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(now));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(now));
final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -527,7 +528,7 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForOptionsFollowedByGetIfAuthorizationHeaderAndSharedCache() throws Exception {
impl = new CachingExec(cache, null, CacheConfig.custom().setSharedCache(true).build());
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpOptions("http://foo.example.com/");
req1.setHeader("Authorization", StandardAuthScheme.BASIC + " QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -535,16 +536,16 @@ public class TestCachingExecChain {
final ClassicHttpResponse resp1 = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content");
resp1.setHeader("Content-Length", "0");
resp1.setHeader("ETag", "\"options-etag\"");
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(now));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(now));
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"get-etag\"");
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "public, max-age=3600");
resp1.setHeader("Last-Modified", DateUtils.formatDate(now));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
execute(req1);
@ -557,8 +558,8 @@ public class TestCachingExecChain {
@Test
public void testSetsValidatedContextIfRequestWasSuccessfullyValidated() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -567,14 +568,14 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("ETag", "\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -588,8 +589,8 @@ public class TestCachingExecChain {
@Test
public void testSetsViaHeaderIfRequestWasSuccessfullyValidated() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -598,14 +599,14 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("ETag", "\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -620,8 +621,8 @@ public class TestCachingExecChain {
@Test
public void testSetsModuleResponseContextIfValidationRequiredButFailed() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -630,7 +631,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5, must-revalidate");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -646,8 +647,8 @@ public class TestCachingExecChain {
@Test
public void testSetsModuleResponseContextIfValidationFailsButNotRequired() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -656,7 +657,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -671,8 +672,8 @@ public class TestCachingExecChain {
@Test
public void testSetViaHeaderIfValidationFailsButNotRequired() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -681,7 +682,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -697,8 +698,8 @@ public class TestCachingExecChain {
@Test
public void testReturns304ForIfNoneMatchPassesIfRequestServedFromOrigin() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -707,13 +708,13 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
req2.addHeader("If-None-Match", "\"etag\"");
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("ETag", "\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -729,8 +730,8 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForIfNoneMatchFailsIfRequestServedFromOrigin() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -739,7 +740,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
req2.addHeader("If-None-Match", "\"etag\"");
@ -747,7 +748,7 @@ public class TestCachingExecChain {
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("ETag", "\"newetag\"");
resp2.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -762,8 +763,8 @@ public class TestCachingExecChain {
@Test
public void testReturns304ForIfModifiedSincePassesIfRequestServedFromOrigin() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -772,15 +773,15 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(tenSecondsAgo));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("ETag", "\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -796,8 +797,8 @@ public class TestCachingExecChain {
@Test
public void testReturns200ForIfModifiedSinceFailsIfRequestServedFromOrigin() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
@ -806,17 +807,17 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
req2.addHeader("If-Modified-Since", DateUtils.formatDate(tenSecondsAgo));
req2.addHeader("If-Modified-Since", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("ETag", "\"newetag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Last-Modified", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Last-Modified", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -832,7 +833,7 @@ public class TestCachingExecChain {
@Test
public void testVariantMissServerIfReturns304CacheReturns200() throws Exception {
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com");
req1.addHeader("Accept-Encoding", "gzip");
@ -841,7 +842,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("Etag", "\"gzip_etag\"");
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Vary", "Accept-Encoding");
resp1.setHeader("Cache-Control", "public, max-age=3600");
@ -856,7 +857,7 @@ public class TestCachingExecChain {
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("Etag", "\"deflate_etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Vary", "Accept-Encoding");
resp2.setHeader("Cache-Control", "public, max-age=3600");
@ -871,7 +872,7 @@ public class TestCachingExecChain {
resp3.setEntity(HttpTestUtils.makeBody(128));
resp3.setHeader("Content-Length", "128");
resp3.setHeader("Etag", "\"gzip_etag\"");
resp3.setHeader("Date", DateUtils.formatDate(now));
resp3.setHeader("Date", DateUtils.formatStandardDate(now));
resp3.setHeader("Vary", "Accept-Encoding");
resp3.setHeader("Cache-Control", "public, max-age=3600");
@ -894,7 +895,7 @@ public class TestCachingExecChain {
@Test
public void testVariantsMissServerReturns304CacheReturns304() throws Exception {
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com");
req1.addHeader("Accept-Encoding", "gzip");
@ -903,7 +904,7 @@ public class TestCachingExecChain {
resp1.setEntity(HttpTestUtils.makeBody(128));
resp1.setHeader("Content-Length", "128");
resp1.setHeader("Etag", "\"gzip_etag\"");
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Vary", "Accept-Encoding");
resp1.setHeader("Cache-Control", "public, max-age=3600");
@ -918,7 +919,7 @@ public class TestCachingExecChain {
resp2.setEntity(HttpTestUtils.makeBody(128));
resp2.setHeader("Content-Length", "128");
resp2.setHeader("Etag", "\"deflate_etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Vary", "Accept-Encoding");
resp2.setHeader("Cache-Control", "public, max-age=3600");
@ -932,7 +933,7 @@ public class TestCachingExecChain {
final ClassicHttpResponse resp4 = HttpTestUtils.make304Response();
resp4.setHeader("Etag", "\"gzip_etag\"");
resp4.setHeader("Date", DateUtils.formatDate(now));
resp4.setHeader("Date", DateUtils.formatStandardDate(now));
resp4.setHeader("Vary", "Accept-Encoding");
resp4.setHeader("Cache-Control", "public, max-age=3600");
@ -955,7 +956,7 @@ public class TestCachingExecChain {
@Test
public void testSocketTimeoutExceptionIsNotSilentlyCatched() throws Exception {
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com");
@ -976,7 +977,7 @@ public class TestCachingExecChain {
throw new SocketTimeoutException("Read timed out");
}
}, 128, null));
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -996,19 +997,20 @@ public class TestCachingExecChain {
final HttpHost host = new HttpHost("foo.example.com");
final ClassicHttpRequest request = new HttpGet("http://foo.example.com/bar");
final Date now = new Date();
final Date requestSent = new Date(now.getTime() - 3 * 1000L);
final Date responseGenerated = new Date(now.getTime() - 2 * 1000L);
final Date responseReceived = new Date(now.getTime() - 1 * 1000L);
final Instant now = Instant.now();
final Instant requestSent = now.plusSeconds(3);
final Instant responseGenerated = now.plusSeconds(2);
final Instant responseReceived = now.plusSeconds(1);
final ClassicHttpResponse originResponse = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES + 1));
originResponse.setHeader("Cache-Control","public, max-age=3600");
originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
originResponse.setHeader("Date", DateUtils.formatStandardDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\"");
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, mockExecRuntime, context);
impl.cacheAndReturnResponse(host, request, originResponse, scope, requestSent, responseReceived);
impl.cacheAndReturnResponse(host, request, originResponse, scope,
DateUtils.toDate(requestSent), DateUtils.toDate(responseReceived));
Mockito.verify(cache, Mockito.never()).createCacheEntry(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
@ -1022,15 +1024,15 @@ public class TestCachingExecChain {
final HttpHost host = new HttpHost("foo.example.com");
final ClassicHttpRequest request = new HttpGet("http://foo.example.com/bar");
final Date now = new Date();
final Date requestSent = new Date(now.getTime() - 3 * 1000L);
final Date responseGenerated = new Date(now.getTime() - 2 * 1000L);
final Date responseReceived = new Date(now.getTime() - 1 * 1000L);
final Instant now = Instant.now();
final Instant requestSent = now.plusSeconds(3);
final Instant responseGenerated = now.plusSeconds(2);
final Instant responseReceived = now.plusSeconds(1);
final ClassicHttpResponse originResponse = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES - 1));
originResponse.setHeader("Cache-Control","public, max-age=3600");
originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated));
originResponse.setHeader("Date", DateUtils.formatStandardDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\"");
final HttpCacheEntry httpCacheEntry = HttpTestUtils.makeCacheEntry();
@ -1041,11 +1043,12 @@ public class TestCachingExecChain {
RequestEquivalent.eq(request),
ResponseEquivalent.eq(response),
Mockito.any(),
Mockito.eq(requestSent),
Mockito.eq(responseReceived))).thenReturn(httpCacheEntry);
Mockito.eq(DateUtils.toDate(requestSent)),
Mockito.eq(DateUtils.toDate(responseReceived)))).thenReturn(httpCacheEntry);
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, mockExecRuntime, context);
impl.cacheAndReturnResponse(host, request, originResponse, scope, requestSent, responseReceived);
impl.cacheAndReturnResponse(host, request, originResponse, scope,
DateUtils.toDate(requestSent), DateUtils.toDate(responseReceived));
Mockito.verify(mockCache).createCacheEntry(
Mockito.any(),
@ -1119,7 +1122,7 @@ public class TestCachingExecChain {
@Test
public void testCanCacheAResponseWithoutABody() throws Exception {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content");
response.setHeader("Date", DateUtils.formatDate(new Date()));
response.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
response.setHeader("Cache-Control", "max-age=300");
Mockito.when(mockExecChain.proceed(RequestEquivalent.eq(request), Mockito.any())).thenReturn(response);
@ -1132,8 +1135,8 @@ public class TestCachingExecChain {
@Test
public void testNoEntityForIfNoneMatchRequestNotYetInCache() throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
req1.addHeader("If-None-Match", "\"etag\"");
@ -1141,7 +1144,7 @@ public class TestCachingExecChain {
final ClassicHttpResponse resp1 = HttpTestUtils.make304Response();
resp1.setHeader("Content-Length", "128");
resp1.setHeader("ETag", "\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1154,7 +1157,7 @@ public class TestCachingExecChain {
@Test
public void testNotModifiedResponseUpdatesCacheEntryWhenNoEntity() throws Exception {
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
req1.addHeader("If-None-Match", "etag");
@ -1163,12 +1166,12 @@ public class TestCachingExecChain {
req2.addHeader("If-None-Match", "etag");
final ClassicHttpResponse resp1 = HttpTestUtils.make304Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "max-age=0");
resp1.setHeader("Etag", "etag");
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "max-age=0");
resp1.setHeader("Etag", "etag");
@ -1188,7 +1191,7 @@ public class TestCachingExecChain {
@Test
public void testNotModifiedResponseWithVaryUpdatesCacheEntryWhenNoEntity() throws Exception {
final Date now = new Date();
final Instant now = Instant.now();
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
req1.addHeader("If-None-Match", "etag");
@ -1197,13 +1200,13 @@ public class TestCachingExecChain {
req2.addHeader("If-None-Match", "etag");
final ClassicHttpResponse resp1 = HttpTestUtils.make304Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "max-age=0");
resp1.setHeader("Etag", "etag");
resp1.setHeader("Vary", "Accept-Encoding");
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "max-age=0");
resp1.setHeader("Etag", "etag");
resp1.setHeader("Vary", "Accept-Encoding");
@ -1225,8 +1228,8 @@ public class TestCachingExecChain {
@Test
public void testDoesNotSend304ForNonConditionalRequest() throws Exception {
final Date now = new Date();
final Date inOneMinute = new Date(System.currentTimeMillis() + 60000);
final Instant now = Instant.now();
final Instant inOneMinute = now.plus(1, ChronoUnit.MINUTES);
final ClassicHttpRequest req1 = new HttpGet("http://foo.example.com/");
req1.addHeader("If-None-Match", "etag");
@ -1234,17 +1237,17 @@ public class TestCachingExecChain {
final ClassicHttpRequest req2 = new HttpGet("http://foo.example.com/");
final ClassicHttpResponse resp1 = HttpTestUtils.make304Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Cache-Control", "public, max-age=60");
resp1.setHeader("Expires", DateUtils.formatDate(inOneMinute));
resp1.setHeader("Expires", DateUtils.formatStandardDate(inOneMinute));
resp1.setHeader("Etag", "etag");
resp1.setHeader("Vary", "Accept-Encoding");
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_OK,
"Ok");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "public, max-age=60");
resp2.setHeader("Expires", DateUtils.formatDate(inOneMinute));
resp2.setHeader("Expires", DateUtils.formatStandardDate(inOneMinute));
resp2.setHeader("Etag", "etag");
resp2.setHeader("Vary", "Accept-Encoding");
resp2.setEntity(HttpTestUtils.makeBody(128));

View File

@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.util.Date;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -66,7 +66,7 @@ public class TestConditionalRequestBuilder {
basicRequest.addHeader("Accept-Encoding", "gzip");
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Date", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("Last-Modified", lastModified) };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
@ -86,13 +86,13 @@ public class TestConditionalRequestBuilder {
@Test
public void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsValidators()
throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Date twentySecondsAgo = new Date(now.getTime() - 20 * 1000L);
final String lmDate = DateUtils.formatDate(twentySecondsAgo);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final Instant twentySecondsAgo = now.plusSeconds(20);
final String lmDate = DateUtils.formatStandardDate(twentySecondsAgo);
final String etag = "\"etag\"";
final Header[] headers = {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("Last-Modified", lmDate),
new BasicHeader("ETag", etag)
};
@ -115,8 +115,8 @@ public class TestConditionalRequestBuilder {
basicRequest.addHeader("Accept-Encoding", "gzip");
final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Last-Modified", DateUtils.formatDate(new Date())),
new BasicHeader("Date", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now())),
new BasicHeader("ETag", theETag) };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
@ -138,13 +138,13 @@ public class TestConditionalRequestBuilder {
@Test
public void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() throws Exception {
final HttpRequest basicRequest = new BasicHttpRequest("GET","/");
final Date now = new Date();
final Date elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
final Instant now = Instant.now();
final Instant elevenSecondsAgo = now.minusSeconds(11);
final Instant tenSecondsAgo = now.minusSeconds(10);
final Instant nineSecondsAgo = now.plusSeconds(9);
final Header[] cacheEntryHeaders = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"etag\""),
new BasicHeader("Cache-Control","max-age=5, must-revalidate") };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);
@ -166,13 +166,13 @@ public class TestConditionalRequestBuilder {
@Test
public void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() throws Exception {
final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
final Date now = new Date();
final Date elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
final Instant now = Instant.now();
final Instant elevenSecondsAgo = now.minusSeconds(11);
final Instant tenSecondsAgo = now.minusSeconds(10);
final Instant nineSecondsAgo = now.plusSeconds(9);
final Header[] cacheEntryHeaders = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"etag\""),
new BasicHeader("Cache-Control","max-age=5, proxy-revalidate") };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);
@ -263,7 +263,7 @@ public class TestConditionalRequestBuilder {
@Test
public void testBuildUnconditionalRequestDoesNotUseIfUnmodifiedSince()
throws Exception {
request.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date()));
request.addHeader("If-Unmodified-Since", DateUtils.formatStandardDate(Instant.now()));
final HttpRequest result = impl.buildUnconditionalRequest(request);
Assert.assertNull(result.getFirstHeader("If-Unmodified-Since"));
}
@ -271,7 +271,7 @@ public class TestConditionalRequestBuilder {
@Test
public void testBuildUnconditionalRequestDoesNotUseIfModifiedSince()
throws Exception {
request.addHeader("If-Modified-Since", DateUtils.formatDate(new Date()));
request.addHeader("If-Modified-Since", DateUtils.formatStandardDate(Instant.now()));
final HttpRequest result = impl.buildUnconditionalRequest(request);
Assert.assertNull(result.getFirstHeader("If-Modified-Since"));
}

View File

@ -31,7 +31,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.Date;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@ -74,13 +74,13 @@ public class TestDefaultAsyncCacheInvalidator {
@Mock
private Cancellable cancellable;
private Date now;
private Date tenSecondsAgo;
private Instant now;
private Instant tenSecondsAgo;
@Before
public void setUp() {
now = new Date();
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
now = Instant.now();
tenSecondsAgo = now.minusSeconds(10);
when(cacheKeyResolver.resolve(ArgumentMatchers.any())).thenAnswer((Answer<String>) invocation -> {
final URI uri = invocation.getArgument(0);
@ -352,12 +352,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -374,12 +374,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(201);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -396,7 +396,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -410,12 +410,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", "http://foo.example.com/bar");
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -432,12 +432,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", "/bar");
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -454,12 +454,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://baz.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -475,12 +475,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"same-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"same-etag\"")
});
@ -496,12 +496,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(now)),
new BasicHeader("Date", DateUtils.formatStandardDate(now)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -518,7 +518,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -535,12 +535,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.removeHeaders("ETag");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -557,12 +557,12 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag", "\"some-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
});
cacheReturnsEntryForUri(key, entry);
@ -584,7 +584,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("ETag", "\"old-etag\""),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
});
cacheReturnsEntryForUri(key, entry);
@ -601,7 +601,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -629,7 +629,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("ETag", "\"old-etag\""),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo))
});
cacheReturnsEntryForUri(key, entry);
@ -646,7 +646,7 @@ public class TestDefaultAsyncCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);

View File

@ -32,7 +32,7 @@ import static org.mockito.Mockito.when;
import java.io.IOException;
import java.net.URI;
import java.util.Date;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@ -68,13 +68,13 @@ public class TestDefaultCacheInvalidator {
@Mock
private HttpCacheStorage mockStorage;
private Date now;
private Date tenSecondsAgo;
private Instant now;
private Instant tenSecondsAgo;
@Before
public void setUp() {
now = new Date();
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
now = Instant.now();
tenSecondsAgo = now.minusSeconds(10);
when(cacheKeyResolver.resolve(ArgumentMatchers.any())).thenAnswer((Answer<String>) invocation -> {
final URI uri = invocation.getArgument(0);
@ -346,12 +346,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -368,12 +368,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(201);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -390,7 +390,7 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -404,12 +404,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String cacheKey = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", "http://foo.example.com/bar");
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -426,12 +426,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String cacheKey = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", "/bar");
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -448,7 +448,7 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String cacheKey = "http://baz.example.com:80/bar";
response.setHeader("Content-Location", cacheKey);
@ -462,12 +462,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"same-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"same-etag\"")
});
@ -483,12 +483,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(now)),
new BasicHeader("Date", DateUtils.formatStandardDate(now)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -505,7 +505,7 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -522,12 +522,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.removeHeaders("ETag");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
new BasicHeader("ETag", "\"old-etag\"")
});
@ -544,12 +544,12 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag", "\"some-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
});
when(mockStorage.getEntry(key)).thenReturn(entry);
@ -571,7 +571,7 @@ public class TestDefaultCacheInvalidator {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("ETag", "\"old-etag\""),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
});
when(mockStorage.getEntry(key)).thenReturn(entry);
@ -588,7 +588,7 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);
@ -616,7 +616,7 @@ public class TestDefaultCacheInvalidator {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
new BasicHeader("ETag", "\"old-etag\""),
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo))
});
when(mockStorage.getEntry(key)).thenReturn(entry);
@ -633,7 +633,7 @@ public class TestDefaultCacheInvalidator {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
response.setHeader("ETag","\"new-etag\"");
response.setHeader("Date", DateUtils.formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
final String key = "http://foo.example.com:80/bar";
response.setHeader("Content-Location", key);

View File

@ -33,7 +33,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.util.Date;
import java.time.Instant;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
@ -102,15 +102,15 @@ public class TestHttpCacheJiraNumber1147 {
final ClassicHttpRequest get = new HttpGet("http://somehost/");
final HttpCacheContext context = HttpCacheContext.create();
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
response.setEntity(HttpTestUtils.makeBody(128));
response.setHeader("Content-Length", "128");
response.setHeader("ETag", "\"etag\"");
response.setHeader("Cache-Control", "public, max-age=3600");
response.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
response.setHeader("Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
when(mockExecChain.proceed(
isA(ClassicHttpRequest.class),

View File

@ -28,7 +28,7 @@ package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.time.Instant;
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.classic.ExecChain;
@ -111,9 +111,9 @@ public class TestProtocolAllowedBehavior {
@Test
public void testNonSharedCacheReturnsStaleResponseWhenRevalidationFailsForProxyRevalidate() throws Exception {
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET","/");
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
originResponse.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
originResponse.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
originResponse.setHeader("Cache-Control","max-age=5,proxy-revalidate");
originResponse.setHeader("Etag","\"etag\"");

View File

@ -27,7 +27,7 @@
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.Date;
import java.time.Instant;
import java.util.Random;
import org.apache.hc.client5.http.ClientProtocolException;
@ -79,13 +79,9 @@ public class TestProtocolDeviations {
HttpHost host;
HttpRoute route;
@Mock
HttpEntity mockEntity;
@Mock
ExecRuntime mockEndpoint;
@Mock
ExecChain mockExecChain;
@Mock
HttpCache mockCache;
ClassicHttpRequest request;
HttpCacheContext context;
ClassicHttpResponse originResponse;
@ -125,7 +121,7 @@ public class TestProtocolDeviations {
private ClassicHttpResponse make200Response() {
final ClassicHttpResponse out = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
out.setHeader("Date", DateUtils.formatDate(new Date()));
out.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
out.setHeader("Server", "MockOrigin/1.0");
out.setEntity(makeBody(128));
return out;

View File

@ -34,8 +34,9 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
@ -89,16 +90,14 @@ public class TestProtocolRecommendations {
ExecChain mockExecChain;
@Mock
ExecRuntime mockExecRuntime;
@Mock
HttpCache mockCache;
ClassicHttpRequest request;
ClassicHttpResponse originResponse;
CacheConfig config;
CachingExec impl;
HttpCache cache;
Date now;
Date tenSecondsAgo;
Date twoMinutesAgo;
Instant now;
Instant tenSecondsAgo;
Instant twoMinutesAgo;
@Before
public void setUp() throws Exception {
@ -122,9 +121,9 @@ public class TestProtocolRecommendations {
cache = new BasicHttpCache(config);
impl = new CachingExec(cache, null, config);
now = new Date();
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
twoMinutesAgo = new Date(now.getTime() - 2 * 60 * 1000L);
now = Instant.now();
tenSecondsAgo = now.minus(10, ChronoUnit.SECONDS);
twoMinutesAgo = now.minus(1, ChronoUnit.MINUTES);
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse);
}
@ -200,7 +199,7 @@ public class TestProtocolRecommendations {
private void cacheGenerated304ForStrongDateValidatorShouldNotContainEntityHeader(
final String headerName, final String headerValue) throws Exception {
cacheGenerated304ForValidatorShouldNotContainEntityHeader(headerName,
headerValue, "Last-Modified", DateUtils.formatDate(twoMinutesAgo),
headerValue, "Last-Modified", DateUtils.formatStandardDate(twoMinutesAgo),
"If-Modified-Since");
}
@ -281,7 +280,7 @@ public class TestProtocolRecommendations {
req2.setHeader(conditionalHeader, validator);
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader(validatorHeader, validator);
// cache module does not currently deal with byte ranges, but we want
@ -309,7 +308,7 @@ public class TestProtocolRecommendations {
@Test
public void cacheGenerated304ForStrongDateValidatorShouldNotContainContentRange() throws Exception {
cacheGenerated304ForStrongValidatorShouldNotContainContentRange(
"Last-Modified", DateUtils.formatDate(twoMinutesAgo), "If-Modified-Since");
"Last-Modified", DateUtils.formatStandardDate(twoMinutesAgo), "If-Modified-Since");
}
@Test
@ -327,13 +326,13 @@ public class TestProtocolRecommendations {
@Test
public void cacheGenerated304ForStrongEtagValidatorShouldNotContainLastModified() throws Exception {
cacheGenerated304ForStrongETagValidatorShouldNotContainEntityHeader(
"Last-Modified", DateUtils.formatDate(tenSecondsAgo));
"Last-Modified", DateUtils.formatStandardDate(tenSecondsAgo));
}
@Test
public void cacheGenerated304ForStrongDateValidatorShouldNotContainLastModified() throws Exception {
cacheGenerated304ForStrongDateValidatorShouldNotContainEntityHeader(
"Last-Modified", DateUtils.formatDate(twoMinutesAgo));
"Last-Modified", DateUtils.formatStandardDate(twoMinutesAgo));
}
private void shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
@ -342,7 +341,7 @@ public class TestProtocolRecommendations {
req.setHeader("If-None-Match", "\"etag\"");
final ClassicHttpResponse resp = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
resp.setHeader("Date", DateUtils.formatDate(now));
resp.setHeader("Date", DateUtils.formatStandardDate(now));
resp.setHeader("Etag", "\"etag\"");
resp.setHeader(entityHeader, entityHeaderValue);
@ -396,7 +395,7 @@ public class TestProtocolRecommendations {
req.setHeader("Range","bytes=0-127");
final ClassicHttpResponse resp = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
resp.setHeader("Date", DateUtils.formatDate(now));
resp.setHeader("Date", DateUtils.formatStandardDate(now));
resp.setHeader("ETag", "\"etag\"");
resp.setHeader("Content-Range", "bytes 0-127/256");
@ -410,7 +409,7 @@ public class TestProtocolRecommendations {
@Test
public void shouldStripLastModifiedFromOrigin304ResponseToStrongValidation() throws Exception {
shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
"Last-Modified", DateUtils.formatDate(twoMinutesAgo));
"Last-Modified", DateUtils.formatStandardDate(twoMinutesAgo));
}
/*
@ -421,7 +420,7 @@ public class TestProtocolRecommendations {
private ClassicHttpRequest requestToPopulateStaleCacheEntry() throws Exception {
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control","public,max-age=5");
resp1.setHeader("Etag","\"etag\"");
@ -547,7 +546,7 @@ public class TestProtocolRecommendations {
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control", "public, max-age=5");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
@ -588,7 +587,7 @@ public class TestProtocolRecommendations {
*/
@Test
public void testDoesNotAddNewWarningHeaderIfResponseArrivesStale() throws Exception {
originResponse.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
originResponse.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
originResponse.setHeader("Cache-Control","public, max-age=5");
originResponse.setHeader("ETag","\"etag\"");
@ -601,7 +600,7 @@ public class TestProtocolRecommendations {
@Test
public void testForwardsExistingWarningHeadersOnResponseThatArrivesStale() throws Exception {
originResponse.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
originResponse.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
originResponse.setHeader("Cache-Control","public, max-age=5");
originResponse.setHeader("ETag","\"etag\"");
originResponse.addHeader("Age","10");
@ -727,13 +726,13 @@ public class TestProtocolRecommendations {
@Test
public void testDoesNotModifyDateOnRequests() throws Exception {
request.setHeader("Date", DateUtils.formatDate(new Date()));
request.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnRequests("Date");
}
@Test
public void testDoesNotModifyDateOnResponses() throws Exception {
originResponse.setHeader("Date", DateUtils.formatDate(new Date()));
originResponse.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnResponses("Date");
}
@ -745,7 +744,7 @@ public class TestProtocolRecommendations {
@Test
public void testDoesNotModifyExpiresOnResponses() throws Exception {
originResponse.setHeader("Expires", DateUtils.formatDate(new Date()));
originResponse.setHeader("Expires", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnResponses("Expires");
}
@ -764,7 +763,7 @@ public class TestProtocolRecommendations {
@Test
public void testDoesNotModifyIfModifiedSinceOnRequests() throws Exception {
request.setHeader("If-Modified-Since", DateUtils.formatDate(new Date()));
request.setHeader("If-Modified-Since", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnRequests("If-Modified-Since");
}
@ -784,13 +783,13 @@ public class TestProtocolRecommendations {
@Test
public void testDoesNotModifyIfUnmodifiedSinceOnRequests() throws Exception {
request = new BasicClassicHttpRequest("DELETE", "/");
request.setHeader("If-Unmodified-Since", DateUtils.formatDate(new Date()));
request.setHeader("If-Unmodified-Since", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnRequests("If-Unmodified-Since");
}
@Test
public void testDoesNotModifyLastModifiedOnResponses() throws Exception {
originResponse.setHeader("Last-Modified", DateUtils.formatDate(new Date()));
originResponse.setHeader("Last-Modified", DateUtils.formatStandardDate(Instant.now()));
testDoesNotModifyHeaderOnResponses("Last-Modified");
}
@ -863,13 +862,13 @@ public class TestProtocolRecommendations {
*/
@Test
public void testUsesLastModifiedDateForCacheConditionalRequests() throws Exception {
final Date twentySecondsAgo = new Date(now.getTime() - 20 * 1000L);
final String lmDate = DateUtils.formatDate(twentySecondsAgo);
final Instant twentySecondsAgo = now.plusSeconds(20);
final String lmDate = DateUtils.formatStandardDate(twentySecondsAgo);
final ClassicHttpRequest req1 =
new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", lmDate);
resp1.setHeader("Cache-Control","max-age=5");
@ -902,13 +901,13 @@ public class TestProtocolRecommendations {
*/
@Test
public void testUsesBothLastModifiedAndETagForConditionalRequestsIfAvailable() throws Exception {
final Date twentySecondsAgo = new Date(now.getTime() - 20 * 1000L);
final String lmDate = DateUtils.formatDate(twentySecondsAgo);
final Instant twentySecondsAgo = now.plusSeconds(20);
final String lmDate = DateUtils.formatStandardDate(twentySecondsAgo);
final String etag = "\"etag\"";
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Last-Modified", lmDate);
resp1.setHeader("Cache-Control","max-age=5");
resp1.setHeader("ETag", etag);
@ -944,14 +943,14 @@ public class TestProtocolRecommendations {
*/
@Test
public void testRevalidatesCachedResponseWithExpirationInThePast() throws Exception {
final Date oneSecondAgo = new Date(now.getTime() - 1 * 1000L);
final Date oneSecondFromNow = new Date(now.getTime() + 1 * 1000L);
final Date twoSecondsFromNow = new Date(now.getTime() + 2 * 1000L);
final Instant oneSecondAgo = now.minusSeconds(1);
final Instant oneSecondFromNow = now.plusSeconds(1);
final Instant twoSecondsFromNow = now.plusSeconds(2);
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Expires",DateUtils.formatDate(oneSecondAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Expires",DateUtils.formatStandardDate(oneSecondAgo));
resp1.setHeader("Cache-Control", "public");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -961,8 +960,8 @@ public class TestProtocolRecommendations {
revalidate.setHeader("If-None-Match","\"etag\"");
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("Date", DateUtils.formatDate(twoSecondsFromNow));
resp2.setHeader("Expires", DateUtils.formatDate(oneSecondFromNow));
resp2.setHeader("Date", DateUtils.formatStandardDate(twoSecondsFromNow));
resp2.setHeader("Expires", DateUtils.formatStandardDate(oneSecondFromNow));
resp2.setHeader("ETag","\"etag\"");
Mockito.when(mockExecChain.proceed(RequestEquivalent.eq(revalidate), Mockito.any())).thenReturn(resp2);
@ -988,17 +987,17 @@ public class TestProtocolRecommendations {
*/
@Test
public void testRetriesValidationThatResultsInAnOlderDated304Response() throws Exception {
final Date elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
final Instant elevenSecondsAgo = now.minusSeconds(11);
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control","max-age=5");
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp2 = HttpTestUtils.make304Response();
resp2.setHeader("ETag","\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(elevenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(elevenSecondsAgo));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1110,7 +1109,7 @@ public class TestProtocolRecommendations {
req1.setHeader("User-Agent", "agent1");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Vary", "User-Agent");
resp1.setHeader("Cache-Control", "max-age=3600");
resp1.setHeader("ETag", "\"etag1\"");
@ -1119,7 +1118,7 @@ public class TestProtocolRecommendations {
req2.setHeader("User-Agent", "agent2");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp2.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp2.setHeader("Vary", "User-Agent");
resp2.setHeader("Cache-Control", "max-age=3600");
resp2.setHeader("ETag", "\"etag2\"");
@ -1128,7 +1127,7 @@ public class TestProtocolRecommendations {
req3.setHeader("User-Agent", "agent3");
final ClassicHttpResponse resp3 = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
resp3.setHeader("Date", DateUtils.formatDate(now));
resp3.setHeader("Date", DateUtils.formatStandardDate(now));
resp3.setHeader("ETag", "\"etag1\"");
final ClassicHttpRequest req4 = new BasicClassicHttpRequest("GET", "/");
@ -1149,8 +1148,8 @@ public class TestProtocolRecommendations {
assertEquals(HttpStatus.SC_OK, result1.getCode());
assertEquals("\"etag1\"", result1.getFirstHeader("ETag").getValue());
assertEquals(DateUtils.formatDate(now), result1.getFirstHeader("Date").getValue());
assertEquals(DateUtils.formatDate(now), result2.getFirstHeader("Date").getValue());
assertEquals(DateUtils.formatStandardDate(now), result1.getFirstHeader("Date").getValue());
assertEquals(DateUtils.formatStandardDate(now), result2.getFirstHeader("Date").getValue());
}
@Test
@ -1160,7 +1159,7 @@ public class TestProtocolRecommendations {
req1.setHeader("User-Agent", "agent1");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Vary", "User-Agent");
resp1.setHeader("Cache-Control", "max-age=3600");
resp1.setHeader("ETag", "\"etag1\"");
@ -1171,7 +1170,7 @@ public class TestProtocolRecommendations {
req2.setHeader("User-Agent", "agent2");
final ClassicHttpResponse resp2 = new BasicClassicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("ETag", "\"etag1\"");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp2);
@ -1210,7 +1209,7 @@ public class TestProtocolRecommendations {
resp2.setHeader("Vary","User-Agent");
resp2.setHeader("ETag", "\"etag2\"");
resp2.setHeader("Cache-Control","max-age=3600");
resp2.setHeader("Date", DateUtils.formatDate(new Date()));
resp2.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
final ClassicHttpRequest req3 = HttpTestUtils.makeDefaultRequest();
req3.setHeader("User-Agent", "agent3");
@ -1258,14 +1257,14 @@ public class TestProtocolRecommendations {
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control","max-age=3600");
resp1.setHeader("ETag", "\"old-etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
final ClassicHttpRequest req2 = new HttpPost("http://foo.example.com/bar");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("ETag", "\"new-etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Content-Location", "http://foo.example.com/");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp2);
@ -1294,7 +1293,7 @@ public class TestProtocolRecommendations {
resp2.setVersion(HttpVersion.HTTP_1_0);
resp2.setEntity(HttpTestUtils.makeBody(200));
resp2.setHeader("Content-Length","200");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp2);
@ -1308,7 +1307,7 @@ public class TestProtocolRecommendations {
resp2.setVersion(HttpVersion.HTTP_1_0);
resp2.setEntity(HttpTestUtils.makeBody(200));
resp2.setHeader("Content-Length","200");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Via","1.0 someproxy");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp2);
@ -1414,7 +1413,7 @@ public class TestProtocolRecommendations {
public void cacheShouldUpdateWithNewCacheableResponse() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "max-age=3600");
resp1.setHeader("ETag", "\"etag1\"");
@ -1423,7 +1422,7 @@ public class TestProtocolRecommendations {
final ClassicHttpRequest req2 = HttpTestUtils.makeDefaultRequest();
req2.setHeader("Cache-Control", "max-age=0");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
resp2.setHeader("Cache-Control", "max-age=3600");
resp2.setHeader("ETag", "\"etag2\"");
@ -1453,8 +1452,8 @@ public class TestProtocolRecommendations {
public void expiresEqualToDateWithNoCacheControlIsNotCacheable() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Expires", DateUtils.formatDate(now));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Expires", DateUtils.formatStandardDate(now));
resp1.removeHeaders("Cache-Control");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1476,8 +1475,8 @@ public class TestProtocolRecommendations {
public void expiresPriorToDateWithNoCacheControlIsNotCacheable() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(now));
resp1.setHeader("Expires", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(now));
resp1.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.removeHeaders("Cache-Control");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1571,7 +1570,7 @@ public class TestProtocolRecommendations {
public void returns504ForStaleEntryWithOnlyIfCached() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control","max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1590,7 +1589,7 @@ public class TestProtocolRecommendations {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control","max-age=5");
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -1608,7 +1607,7 @@ public class TestProtocolRecommendations {
public void issues304EvenWithWeakETag() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "max-age=300");
resp1.setHeader("ETag","W/\"weak-sauce\"");

View File

@ -32,7 +32,7 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@ -80,8 +80,6 @@ public class TestRFC5861Compliance {
ExecChain mockExecChain;
@Mock
ExecRuntime mockExecRuntime;
@Mock
HttpCache mockCache;
ClassicHttpRequest request;
ClassicHttpResponse originResponse;
CacheConfig config;
@ -147,7 +145,7 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInResponseIsTrueReturnsStaleEntryWithWarning()
throws Exception{
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60");
@ -169,7 +167,7 @@ public class TestRFC5861Compliance {
@Test
public void testConsumesErrorResponseWhenServingStale()
throws Exception{
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60");
@ -196,7 +194,7 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInResponseYieldsToMustRevalidate()
throws Exception{
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60, must-revalidate");
@ -219,7 +217,7 @@ public class TestRFC5861Compliance {
public void testStaleIfErrorInResponseYieldsToProxyRevalidateForSharedCache()
throws Exception{
assertTrue(config.isSharedCache());
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60, proxy-revalidate");
@ -245,7 +243,7 @@ public class TestRFC5861Compliance {
.setSharedCache(false).build();
impl = new CachingExec(new BasicHttpCache(configUnshared), null, configUnshared);
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60, proxy-revalidate");
@ -267,7 +265,7 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInResponseYieldsToExplicitFreshnessRequest()
throws Exception{
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=60");
@ -290,7 +288,7 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInRequestIsTrueReturnsStaleEntryWithWarning()
throws Exception{
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5");
@ -313,10 +311,10 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInRequestIsTrueReturnsStaleNonRevalidatableEntryWithWarning()
throws Exception {
final Date tenSecondsAgo = new Date(new Date().getTime() - 10 * 1000L);
final Instant tenSecondsAgo = Instant.now().minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
resp1.setHeader("Cache-Control", "public, max-age=5");
final ClassicHttpRequest req2 = HttpTestUtils.makeDefaultRequest();
@ -337,8 +335,8 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInResponseIsFalseReturnsError()
throws Exception{
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5, stale-if-error=2");
@ -361,8 +359,8 @@ public class TestRFC5861Compliance {
@Test
public void testStaleIfErrorInRequestIsFalseReturnsError()
throws Exception{
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response(tenSecondsAgo,
"public, max-age=5");
@ -403,11 +401,11 @@ public class TestRFC5861Compliance {
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
@ -442,10 +440,10 @@ public class TestRFC5861Compliance {
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
@ -484,11 +482,11 @@ public class TestRFC5861Compliance {
final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
resp1.setHeader("Cache-Control", "private, stale-while-revalidate=15");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
req2.setHeader("If-None-Match","\"etag\"");
@ -518,8 +516,8 @@ public class TestRFC5861Compliance {
public void testStaleWhileRevalidateYieldsToMustRevalidate()
throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
config = CacheConfig.custom()
.setMaxCacheEntries(MAX_ENTRIES)
@ -532,13 +530,13 @@ public class TestRFC5861Compliance {
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, must-revalidate");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, must-revalidate");
resp2.setHeader("ETag","\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -565,8 +563,8 @@ public class TestRFC5861Compliance {
public void testStaleWhileRevalidateYieldsToProxyRevalidateForSharedCache()
throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
config = CacheConfig.custom()
.setMaxCacheEntries(MAX_ENTRIES)
@ -580,13 +578,13 @@ public class TestRFC5861Compliance {
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, proxy-revalidate");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, proxy-revalidate");
resp2.setHeader("ETag","\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);
@ -613,8 +611,8 @@ public class TestRFC5861Compliance {
public void testStaleWhileRevalidateYieldsToExplicitFreshnessRequest()
throws Exception {
final Date now = new Date();
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
final Instant now = Instant.now();
final Instant tenSecondsAgo = now.minusSeconds(10);
config = CacheConfig.custom()
.setMaxCacheEntries(MAX_ENTRIES)
@ -628,14 +626,14 @@ public class TestRFC5861Compliance {
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET", "/");
req2.setHeader("Cache-Control","min-fresh=2");
final ClassicHttpResponse resp2 = HttpTestUtils.make200Response();
resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
resp2.setHeader("ETag","\"etag\"");
resp2.setHeader("Date", DateUtils.formatDate(now));
resp2.setHeader("Date", DateUtils.formatStandardDate(now));
Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(resp1);

View File

@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.util.Date;
import java.time.Instant;
import java.util.Random;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
@ -52,24 +52,20 @@ public class TestResponseCachingPolicy {
private final int[] acceptableCodes = new int[] { HttpStatus.SC_OK,
HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION, HttpStatus.SC_MULTIPLE_CHOICES,
HttpStatus.SC_MOVED_PERMANENTLY, HttpStatus.SC_GONE };
private Date now;
private Date tenSecondsFromNow;
private Date sixSecondsAgo;
static String formatDate(final Date date) {
return DateUtils.formatStandardDate(DateUtils.toInstant(date));
}
private Instant now;
private Instant tenSecondsFromNow;
private Instant sixSecondsAgo;
@Before
public void setUp() throws Exception {
now = new Date();
sixSecondsAgo = new Date(now.getTime() - 6 * 1000L);
tenSecondsFromNow = new Date(now.getTime() + 10 * 1000L);
now = Instant.now();
sixSecondsAgo = now.minusSeconds(6);
tenSecondsFromNow = now.plusSeconds(10);
policy = new ResponseCachingPolicy(0, true, false, false);
request = new BasicHttpRequest("GET","/");
response = new BasicHttpResponse(HttpStatus.SC_OK, "");
response.setHeader("Date", formatDate(new Date()));
response.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
response.setHeader("Content-Length", "0");
}
@ -209,7 +205,7 @@ public class TestResponseCachingPolicy {
public void testNon206WithExplicitExpiresIsCacheable() {
final int status = getRandomStatus();
response.setCode(status);
response.setHeader("Expires", formatDate(new Date()));
response.setHeader("Expires", DateUtils.formatStandardDate(Instant.now()));
Assert.assertTrue(policy.isResponseCacheable("GET", response));
}
@ -364,7 +360,7 @@ public class TestResponseCachingPolicy {
Assert.assertTrue(policy.isResponseCacheable("GET", response));
response = new BasicHttpResponse(HttpStatus.SC_OK, "");
response.setHeader("Date", formatDate(new Date()));
response.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
response.addHeader("Cache-Control", "no-transform");
response.setHeader("Content-Length", "0");
@ -379,7 +375,7 @@ public class TestResponseCachingPolicy {
Assert.assertTrue(policy.isResponseCacheable("HEAD", response));
response = new BasicHttpResponse(HttpStatus.SC_OK, "");
response.setHeader("Date", formatDate(new Date()));
response.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
response.addHeader("Cache-Control", "no-transform");
response.setHeader("Content-Length", "0");
@ -484,8 +480,8 @@ public class TestResponseCachingPolicy {
@Test
public void testResponsesWithMultipleDateHeadersAreNotCacheable() {
response.addHeader("Date", formatDate(now));
response.addHeader("Date", formatDate(sixSecondsAgo));
response.addHeader("Date", DateUtils.formatStandardDate(now));
response.addHeader("Date", DateUtils.formatStandardDate(sixSecondsAgo));
Assert.assertFalse(policy.isResponseCacheable("GET", response));
}
@ -495,8 +491,8 @@ public class TestResponseCachingPolicy {
request.setHeader("Authorization", StandardAuthScheme.BASIC + " QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
response.setHeader("Cache-Control", "public");
response.addHeader("Date", formatDate(now));
response.addHeader("Date", formatDate(sixSecondsAgo));
response.addHeader("Date", DateUtils.formatStandardDate(now));
response.addHeader("Date", DateUtils.formatStandardDate(sixSecondsAgo));
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -518,8 +514,8 @@ public class TestResponseCachingPolicy {
@Test
public void testResponsesWithMultipleExpiresHeadersAreNotCacheable() {
response.addHeader("Expires", formatDate(now));
response.addHeader("Expires", formatDate(sixSecondsAgo));
response.addHeader("Expires", DateUtils.formatStandardDate(now));
response.addHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo));
Assert.assertFalse(policy.isResponseCacheable("GET", response));
}
@ -529,8 +525,8 @@ public class TestResponseCachingPolicy {
request.setHeader("Authorization", StandardAuthScheme.BASIC + " QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
response.setHeader("Cache-Control", "public");
response.addHeader("Expires", formatDate(now));
response.addHeader("Expires", formatDate(sixSecondsAgo));
response.addHeader("Expires", DateUtils.formatStandardDate(now));
response.addHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo));
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -591,8 +587,8 @@ public class TestResponseCachingPolicy {
@Test
public void testResponsesToGETWithQueryParamsAndExplicitCachingAreCacheable() {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -600,8 +596,8 @@ public class TestResponseCachingPolicy {
public void testResponsesToHEADWithQueryParamsAndExplicitCachingAreCacheable() {
policy = new ResponseCachingPolicy(0, true, false, false);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -609,8 +605,8 @@ public class TestResponseCachingPolicy {
public void testResponsesToGETWithQueryParamsAndExplicitCachingAreCacheableEvenWhen1_0QueryCachingDisabled() {
policy = new ResponseCachingPolicy(0, true, true, false);
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -618,8 +614,8 @@ public class TestResponseCachingPolicy {
public void testResponsesToHEADWithQueryParamsAndExplicitCachingAreCacheableEvenWhen1_0QueryCachingDisabled() {
policy = new ResponseCachingPolicy(0, true, true, false);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -662,8 +658,8 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -673,8 +669,8 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -684,8 +680,8 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -695,8 +691,8 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -717,8 +713,8 @@ public class TestResponseCachingPolicy {
@Test
public void getsWithQueryParametersFrom1_0OriginsViaProxiesAreCacheableWithExpires() {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.0 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -727,8 +723,8 @@ public class TestResponseCachingPolicy {
public void headsWithQueryParametersFrom1_0OriginsViaProxiesAreCacheableWithExpires() {
policy = new ResponseCachingPolicy(0, true, false, false);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.0 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -737,8 +733,8 @@ public class TestResponseCachingPolicy {
public void getsWithQueryParametersFrom1_0OriginsViaProxiesCanNotBeCacheableEvenWithExpires() {
policy = new ResponseCachingPolicy(0, true, true, true);
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.0 someproxy");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -747,8 +743,8 @@ public class TestResponseCachingPolicy {
public void headsWithQueryParametersFrom1_0OriginsViaProxiesCanNotBeCacheableEvenWithExpires() {
policy = new ResponseCachingPolicy(0, true, true, true);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.0 someproxy");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -756,8 +752,8 @@ public class TestResponseCachingPolicy {
@Test
public void getsWithQueryParametersFrom1_0OriginsViaExplicitProxiesAreCacheableWithExpires() {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "HTTP/1.0 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -766,8 +762,8 @@ public class TestResponseCachingPolicy {
public void headsWithQueryParametersFrom1_0OriginsViaExplicitProxiesAreCacheableWithExpires() {
policy = new ResponseCachingPolicy(0, true, false, false);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "HTTP/1.0 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -776,8 +772,8 @@ public class TestResponseCachingPolicy {
public void getsWithQueryParametersFrom1_0OriginsViaExplicitProxiesCanNotBeCacheableEvenWithExpires() {
policy = new ResponseCachingPolicy(0, true, true, true);
request = new BasicHttpRequest("GET", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "HTTP/1.0 someproxy");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -786,8 +782,8 @@ public class TestResponseCachingPolicy {
public void headsWithQueryParametersFrom1_0OriginsViaExplicitProxiesCanNotBeCacheableEvenWithExpires() {
policy = new ResponseCachingPolicy(0, true, true, true);
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "HTTP/1.0 someproxy");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -797,8 +793,8 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("GET", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.1 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -809,24 +805,24 @@ public class TestResponseCachingPolicy {
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
response.setVersion(HttpVersion.HTTP_1_0);
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(tenSecondsFromNow));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(tenSecondsFromNow));
response.setHeader("Via", "1.1 someproxy");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@Test
public void notCacheableIfExpiresEqualsDateAndNoCacheControl() {
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(now));
response.removeHeaders("Cache-Control");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@Test
public void notCacheableIfExpiresPrecedesDateAndNoCacheControl() {
response.setHeader("Date", formatDate(now));
response.setHeader("Expires", formatDate(sixSecondsAgo));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Expires", DateUtils.formatStandardDate(sixSecondsAgo));
response.removeHeaders("Cache-Control");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -834,7 +830,7 @@ public class TestResponseCachingPolicy {
@Test
public void test302WithExplicitCachingHeaders() {
response.setCode(HttpStatus.SC_MOVED_TEMPORARILY);
response.setHeader("Date", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Cache-Control","max-age=300");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -843,7 +839,7 @@ public class TestResponseCachingPolicy {
public void test303WithExplicitCachingHeadersUnderDefaultBehavior() {
// RFC 2616 says: 303 should not be cached
response.setCode(HttpStatus.SC_SEE_OTHER);
response.setHeader("Date", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Cache-Control","max-age=300");
Assert.assertFalse(policy.isResponseCacheable(request, response));
}
@ -854,7 +850,7 @@ public class TestResponseCachingPolicy {
// response headers
policy = new ResponseCachingPolicy(0, true, false, true);
response.setCode(HttpStatus.SC_SEE_OTHER);
response.setHeader("Date", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Cache-Control","max-age=300");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -862,7 +858,7 @@ public class TestResponseCachingPolicy {
@Test
public void test307WithExplicitCachingHeaders() {
response.setCode(HttpStatus.SC_TEMPORARY_REDIRECT);
response.setHeader("Date", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Cache-Control","max-age=300");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}
@ -870,7 +866,7 @@ public class TestResponseCachingPolicy {
@Test
public void otherStatusCodesAreCacheableWithExplicitCachingHeaders() {
response.setCode(HttpStatus.SC_NOT_FOUND);
response.setHeader("Date", formatDate(now));
response.setHeader("Date", DateUtils.formatStandardDate(now));
response.setHeader("Cache-Control","max-age=300");
Assert.assertTrue(policy.isResponseCacheable(request, response));
}

View File

@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.util.Date;
import java.time.Instant;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
@ -50,7 +50,7 @@ public class TestResponseProtocolCompliance {
}
private void setMinimalResponseHeaders(final HttpResponse resp) {
resp.setHeader("Date", DateUtils.formatDate(new Date()));
resp.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
resp.setHeader("Server", "MyServer/1.0");
}