@Test public void testLastModifiedHeaderExcludedFromResponse() throws Exception { String path = "src/test/resources/default-configuration.properties"; File file = new File(path); Mockito.when(request.getServletPath()).thenReturn(path); Mockito.when(resourceManager.getRequestedResource(null, path)).thenReturn(new Resource(file)); Set<Boolean> doFilterCalled = new HashSet<>(); FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse resp) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) resp; doFilterCalled.add(Boolean.TRUE); response.setHeader("Last-Modified", new Date().toString()); } }; ETagFilter filter = new ETagFilter(resourceManager); filter.doFilter(request, response, filterChain); Assert.assertEquals(1, doFilterCalled.size()); Assert.assertNull(response.getHeader("Last-Modified")); }
@Test public void testIfModifiedSinceRequestHeaderIgnored() throws Exception { String path = "src/test/resources/default-configuration.properties"; File file = new File(path); Mockito.when(request.getServletPath()).thenReturn(path); Mockito.when(resourceManager.getRequestedResource(null, path)).thenReturn(new Resource(file)); Set<Boolean> doFilterCalled = new HashSet<>(); FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException { // The correct test would have made assertions on the resulting response. It would // however require the // behaviour of a full filter chain execution. // We therefore just test, that Last-Modified is not available as request header in the // filter chain. doFilterCalled.add(Boolean.TRUE); HttpServletRequest request = (HttpServletRequest) req; Assert.assertNull(request.getHeader("Last-Modified")); } }; ETagFilter filter = new ETagFilter(resourceManager); filter.doFilter(request, response, filterChain); Assert.assertEquals(1, doFilterCalled.size()); }
@Test public void testEtagAdded() throws Exception { String path = "src/test/resources/default-configuration.properties"; File file = new File(path); Mockito.when(request.getServletPath()).thenReturn(path); Mockito.when(resourceManager.getRequestedResource(null, path)).thenReturn(new Resource(file)); ETagFilter filter = new ETagFilter(resourceManager); filter.doFilter(request, response, filterChain); Mockito.verify(response).setHeader("ETag", file.length() + "_" + file.lastModified()); }