### Question:
CacheValidityPolicy { public boolean mayReturnStaleIfError(final HttpRequest request, final HttpCacheEntry entry, final Date now) { final TimeValue staleness = getStaleness(entry, now); return mayReturnStaleIfError(request, HeaderConstants.CACHE_CONTROL, staleness) || mayReturnStaleIfError(entry, HeaderConstants.CACHE_CONTROL, staleness); } CacheValidityPolicy(); TimeValue getCurrentAge(final HttpCacheEntry entry, final Date now); TimeValue getFreshnessLifetime(final HttpCacheEntry entry); boolean isResponseFresh(final HttpCacheEntry entry, final Date now); boolean isResponseHeuristicallyFresh(final HttpCacheEntry entry,
            final Date now, final float coefficient, final TimeValue defaultLifetime); TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry,
            final float coefficient, final TimeValue defaultLifetime); boolean isRevalidatable(final HttpCacheEntry entry); boolean mustRevalidate(final HttpCacheEntry entry); boolean proxyRevalidate(final HttpCacheEntry entry); boolean mayReturnStaleWhileRevalidating(final HttpCacheEntry entry, final Date now); boolean mayReturnStaleIfError(final HttpRequest request, final HttpCacheEntry entry, final Date now); boolean hasCacheControlDirective(final HttpCacheEntry entry, final String directive); TimeValue getStaleness(final HttpCacheEntry entry, final Date now); static final TimeValue MAX_AGE; }### Answer:
@Test public void testMayReturnStaleIfErrorInResponseIsTrueWithinStaleness(){ final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }
@Test public void testMayReturnStaleIfErrorInRequestIsTrueWithinStaleness(){ final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }
@Test public void testMayNotReturnStaleIfErrorInResponseAndAfterResponseWindow(){ final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }
@Test public void testMayNotReturnStaleIfErrorInResponseAndAfterRequestWindow(){ final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }