HttpCacheToolkitImpl implements HttpCacheToolkit { public boolean isModified(Optional<String> etag, Optional<Long> lastModified, Context context) { final String browserEtag = context.getHeader(HttpHeaderConstants.IF_NONE_MATCH); if (browserEtag != null && etag.isPresent()) { if (browserEtag.equals(etag.get())) { return false; } else { return true; } } final String ifModifiedSince = context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE); if (ifModifiedSince != null && lastModified.isPresent()) { if (!ifModifiedSince.isEmpty()) { try { Date browserDate = DateUtil .parseHttpDateFormat(ifModifiedSince); if (browserDate.getTime() >= lastModified.get()) { return false; } } catch (IllegalArgumentException ex) { logger.warn("Can't parse HTTP date", ex); } return true; } } return true; } @Inject  HttpCacheToolkitImpl(NinjaProperties ninjaProperties); boolean isModified(Optional<String> etag, Optional<Long> lastModified, Context context); void addEtag(Context context, Result result, Long lastModified);  }
@Test public void testIsModified() { HttpCacheToolkit httpCacheToolkit = new HttpCacheToolkitImpl( ninjaProperties); when(context.getHeader(HttpHeaderConstants.IF_NONE_MATCH)).thenReturn( "etag_xyz"); assertFalse(httpCacheToolkit.isModified(Optional.of("etag_xyz"), Optional.of(0L), context)); assertTrue(httpCacheToolkit .isModified(Optional.of("etag_xyz_modified"), Optional.of(0L), context)); when(context.getHeader(HttpHeaderConstants.IF_NONE_MATCH)).thenReturn( null); when(context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)) .thenReturn(null); assertTrue(httpCacheToolkit .isModified(Optional.of("etag_xyz_modified"), Optional.of(0L), context)); when(context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)) .thenReturn("Thu, 01 Jan 1970 00:00:00 GMT"); assertTrue(httpCacheToolkit.isModified(Optional.of("etag_xyz_modified"), Optional.of(1000L), context)); when(context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)) .thenReturn("Thu, 01 Jan 1970 00:00:00 GMT"); assertFalse(httpCacheToolkit.isModified(Optional.of("etag_xyz_modified"), Optional.of(0L), context)); when(context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)) .thenReturn("Thu, 01 Jan 1971 00:00:00 GMT"); assertFalse(httpCacheToolkit.isModified(Optional.of("etag_xyz_modified"), Optional.of(0L), context)); when(context.getHeader(HttpHeaderConstants.IF_MODIFIED_SINCE)) .thenReturn("STRANGE_TIMESTAMP"); assertTrue(httpCacheToolkit .isModified(Optional.of("etag_xyz_modified"), Optional.of(0L), context)); }