AbstractAggregatorImpl extends HttpServlet implements IAggregator { protected void setResourceResponseCacheHeaders(HttpServletRequest req, HttpServletResponse resp, IResource resolved, boolean isNoCache) { if (isNoCache) { resp.addHeader("Cache-Control", "no-cache, no-store"); } else { resp.setDateHeader("Last-Modified", resolved.lastModified()); int expires = getConfig().getExpires(); boolean hasCacheBust = req.getAttribute(IHttpTransport.CACHEBUST_REQATTRNAME) != null; resp.addHeader( "Cache-Control", "public" + (expires > 0 && hasCacheBust ? (", max-age=" + expires) : "") ); resp.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } }  @Override void init(ServletConfig servletConfig); @Override void destroy(); @Override boolean reloadConfig(); @Override IDependencies getDependencies(); @Override String getName(); @Override IConfig getConfig(); @Override ICacheManager getCacheManager(); @Override InitParams getInitParams(); @Override HttpServlet asServlet(); @Override IHttpTransport getTransport(); @Override IResourceFactory getResourceFactory(Mutable<URI> uriRef); @Override IResource newResource(URI uri); @Override IResource runConverters(IResource resource); @Override IModuleBuilder getModuleBuilder(String mid, IResource res); @Override Iterable<IAggregatorExtension> getExtensions(String extensionPointId); @Override IModule newModule(String mid, URI uri); @Override ILayerCache newLayerCache(); @Override IModuleCache newModuleCache(); @Override IGzipCache newGzipCache(); @Override String substituteProps(String str); @Override String substituteProps(String str, SubstitutionTransformer transformer); String getPropValue(String propName); @Override abstract IOptions getOptions(); @Override abstract IExecutors getExecutors(); @Override IPlatformServices getPlatformServices(); void initialize(IConfig config); @Override String setForceError(String forceError); @Override Future<?> buildAsync(final Callable<?> builder, final HttpServletRequest req); @Override HttpServletRequest getCurrentRequest();  }
@Test public void testSetResourceResponseCacheHeaders() { Map<String, String> responseParams = new HashMap<String, String>(); IResource mockResource = EasyMock.createMock(IResource.class); final MutableLong lastModified = new MutableLong(); EasyMock.expect(mockResource.lastModified()).andAnswer(new IAnswer<Long>() { @Override public Long answer() throws Throwable { return lastModified.getValue(); } }).anyTimes(); final IConfig mockConfig = EasyMock.createMock(IConfig.class); final MutableInt expires = new MutableInt(); EasyMock.expect(mockConfig.getExpires()).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { return expires.getValue(); } }).anyTimes(); @SuppressWarnings("serial") AbstractAggregatorImpl testAggregator = new TestAggregatorImpl() { @Override public IConfig getConfig() { return mockConfig; } }; HttpServletRequest req = TestUtils.createMockRequest(testAggregator); HttpServletResponse resp = TestUtils.createMockResponse(responseParams, false); lastModified.setValue(1000); expires.setValue(200); resp.setDateHeader(HttpHeaders.LAST_MODIFIED, 1000); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.CACHE_CONTROL, "public, max-age=200"); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); EasyMock.expectLastCall().once(); EasyMock.replay(req, resp, mockResource, mockConfig); req.setAttribute(IHttpTransport.CACHEBUST_REQATTRNAME, "abc"); testAggregator.setResourceResponseCacheHeaders(req, resp, mockResource, false); EasyMock.verify(resp); EasyMock.reset(resp); expires.setValue(0); resp.setDateHeader(HttpHeaders.LAST_MODIFIED, 1000); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.CACHE_CONTROL, "public"); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); EasyMock.expectLastCall().once(); EasyMock.replay(resp); testAggregator.setResourceResponseCacheHeaders(req, resp, mockResource, false); EasyMock.verify(resp); EasyMock.reset(resp); expires.setValue(200); req.removeAttribute(IHttpTransport.CACHEBUST_REQATTRNAME); resp.setDateHeader(HttpHeaders.LAST_MODIFIED, 1000); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.CACHE_CONTROL, "public"); EasyMock.expectLastCall().once(); resp.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); EasyMock.expectLastCall().once(); EasyMock.replay(resp); testAggregator.setResourceResponseCacheHeaders(req, resp, mockResource, false); EasyMock.verify(resp); EasyMock.reset(resp); resp.addHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store"); EasyMock.expectLastCall().once(); EasyMock.replay(resp); testAggregator.setResourceResponseCacheHeaders(req, resp, mockResource, true); EasyMock.verify(resp); }