DefaultRestHeadersHandler implements RestHeadersHandler { public void handle(HypermediaResource resource) { if(Cacheable.class.isAssignableFrom(resource.getClass())) { Cacheable cache = (Cacheable) resource; response.addHeader("Cache-control","max-age=" + cache.getMaximumAge()); } if(RestfulEntity.class.isInstance(resource)) { RestfulEntity entity = (RestfulEntity) resource; restfulHeadersFor(entity.getEtag(), entity.getLastModified()); } else { restfulHeadersFor(defaults.getEtagFor(resource), defaults.getLastModifiedFor(resource)); } } DefaultRestHeadersHandler(HttpServletResponse response, RestDefaults defaults); void handle(HypermediaResource resource);  }
@Test public void whenResourceIsSimpleDoNothing() { handler.handle(mock(HypermediaResource.class)); }
@Test public void whenResourceIsCacheableAddMaxAge() { CacheableOrder cacheable = new CacheableOrder(); handler.handle(cacheable); verify(response).addHeader("Cache-control","max-age=150"); }
@Test public void whenResourceHasLinksAddThemToTheHeader() { CacheableOrder cacheable = new CacheableOrder(); handler.handle(cacheable); }
@Test public void whenNotExplicitlyRestfulEntityUseDefaultEtagAndLastModified() { CacheableOrder cacheable = new CacheableOrder(); Calendar date = Calendar.getInstance(); when(defaults.getLastModifiedFor(cacheable)).thenReturn(date); when(defaults.getEtagFor(cacheable)).thenReturn("custom etag"); handler.handle(cacheable); verify(response).addHeader("ETag", "custom etag"); verify(response).setDateHeader("Last-modified", date.getTimeInMillis()); }
@Test public void whenRestfulEntityIsHandledShouldUseItsValues() { CacheableOrderEntity cacheable = new CacheableOrderEntity(); handler.handle(cacheable); verify(response).addHeader("ETag", "MY ETAG"); verify(response).setDateHeader("Last-modified", cacheable.date.getTimeInMillis()); }