ResourceCacheControl implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest) { if (isCacheableResourceRequest((HttpServletRequest)request)) { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setHeader("Cache-Control", "public, max-age=31536000"); response = new HttpServletResponseWrapper(httpServletResponse) { @Override public void setHeader(String name, String value) { if (!name.equalsIgnoreCase("Expires")) { super.setHeader(name, value); } } }; } } chain.doFilter(request, response); } ResourceCacheControl(); static synchronized void install(); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }
@Test public void test_cache_control_set() throws IOException, ServletException { Mockito.when(servletRequest.getPathInfo()).thenReturn("/a/b/c.js"); resourceCacheControl.doFilter(servletRequest, servletResponse, filterChain); Mockito.verify(servletResponse).setHeader("Cache-Control", "public, max-age=31536000"); }
@Test public void test_cache_control_not_set() throws IOException, ServletException { Mockito.when(servletRequest.getPathInfo()).thenReturn("/a/bc.js"); resourceCacheControl.doFilter(servletRequest, servletResponse, filterChain); Mockito.verify(servletResponse, Mockito.never()).setHeader("Cache-Control", "public, max-age=31536000"); }