### Question:
CacheValidityPolicy { public boolean mayReturnStaleWhileRevalidating(final HttpCacheEntry entry, final Date now) { final Iterator<HeaderElement> it = MessageSupport.iterate(entry, HeaderConstants.CACHE_CONTROL); while (it.hasNext()) { final HeaderElement elt = it.next(); if (HeaderConstants.STALE_WHILE_REVALIDATE.equalsIgnoreCase(elt.getName())) { try { final int allowedStalenessLifetime = Integer.parseInt(elt.getValue()); if (getStaleness(entry, now).compareTo(TimeValue.ofSeconds(allowedStalenessLifetime)) <= 0) { return true; } } catch (final NumberFormatException nfe) { } } } return false; } 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 testMayReturnStaleWhileRevalidatingIsFalseWhenDirectiveIsAbsent() { final Header[] headers = new Header[] { new BasicHeader("Cache-control", "public") }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers); assertFalse(impl.mayReturnStaleWhileRevalidating(entry, now)); }
@Test public void testMayReturnStaleWhileRevalidatingIsTrueWhenWithinStaleness() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }
@Test public void testMayReturnStaleWhileRevalidatingIsFalseWhenPastStaleness() { final Date twentyFiveSecondsAgo = new Date(now.getTime() - 25 * 1000L); final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(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)); }
@Test public void testMayReturnStaleWhileRevalidatingIsFalseWhenDirectiveEmpty() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)), new BasicHeader("Cache-Control", "max-age=5, stale-while-revalidate=") }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, now, headers); assertFalse(impl.mayReturnStaleWhileRevalidating(entry, now)); }