HTTPCacheHelper { boolean shouldBeStored(HTTPResponse response) { boolean hasValidator = response.getHeaders().getLastModified().isPresent() || response.getHeaders().getETag().isPresent(); boolean hasExpiry = response.getHeaders().getExpires().isPresent() || (OptionalUtils.exists(response.getHeaders().getCacheControl(), cc -> cc.getMaxAge() > 0)); return hasExpiry || hasValidator; } HTTPCacheHelper(CacheHeaderBuilder cacheHeaderBuilder);   }
@Test public void responseWithExpiryShouldBeStored() { Headers headers = new Headers(); headers = headers.add(HeaderUtils.toHttpDate("date", createDateTime(10))); headers = headers.add(HeaderUtils.toHttpDate(HeaderConstants.EXPIRES, createDateTime(11))); assertTrue(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithETagShouldBeStored() { Headers headers = new Headers().add("etag", new Tag("foo").format()); assertTrue(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithLastModifiedShouldBeStored() { Headers headers = new Headers().add(HeaderUtils.toHttpDate(HeaderConstants.LAST_MODIFIED, createDateTime(11))); assertTrue(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithMaxAgeShouldBeStored() { Headers headers = new Headers().add(HeaderConstants.CACHE_CONTROL, "max-age=10"); assertTrue(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithEverythingShouldBeStored() { Headers headers = new Headers(); headers = headers.add(HeaderUtils.toHttpDate("date", createDateTime(10))); headers = headers.add(HeaderUtils.toHttpDate(HeaderConstants.EXPIRES, createDateTime(20))); headers = headers.add(HeaderUtils.toHttpDate(HeaderConstants.LAST_MODIFIED, createDateTime(9))); headers = headers.add(HeaderConstants.CACHE_CONTROL, "max-age=10"); headers = headers.add("etag", new Tag("foo").format()); assertTrue(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithNotHeadersShouldNotBeStored() { Headers headers = new Headers(); assertFalse(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }
@Test public void responseWithOnlyDateShouldNotBeStored() { Headers headers = new Headers(); headers = headers.add(HeaderUtils.toHttpDate("date", createDateTime(10))); assertFalse(helper.shouldBeStored(new HTTPResponse(Status.OK, headers))); }