@Test public void testCacheFilesFileDeleted() throws Exception { File webroot = new File(".vertx/webroot"), pageFile = new File(webroot, "deleted.html"); if (!pageFile.exists()) { webroot.mkdirs(); pageFile.createNewFile(); } String page = '/' + pageFile.getName(); stat.setFilesReadOnly(false); stat.setWebRoot(webroot.getPath()); stat.setCacheEntryTimeout(3600 * 1000); long modified = Utils.secondsFactor(pageFile.lastModified()); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); pageFile.delete(); testRequest(HttpMethod.GET, page, 404, "Not Found"); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 404, "Not Found", null); }
/** * Set the server cache entry timeout when caching is enabled * @param timeout the timeout, in ms * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setCacheEntryTimeout(long timeout) { delegate.setCacheEntryTimeout(timeout); return this; }
/** * Set the server cache entry timeout when caching is enabled * @param timeout the timeout, in ms * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setCacheEntryTimeout(long timeout) { delegate.setCacheEntryTimeout(timeout); return this; }
@Test public void testCacheFilesEntryOld() throws Exception { String webroot = "src/test/filesystemwebroot", page = "/fspage.html"; File resource = new File(webroot + page); String html = new String(Files.readAllBytes(resource.toPath())); int cacheEntryTimeout = 100; stat.setFilesReadOnly(false); stat.setWebRoot(webroot); stat.setCacheEntryTimeout(cacheEntryTimeout); long modified = Utils.secondsFactor(resource.lastModified()); testRequest(HttpMethod.GET, page, null, res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified, toDateTime(lastModified)); // Now update the web resource resource.setLastModified(modified + 1000); }, 200, "OK", html); // But it should return a new entry as the entry is now old Thread.sleep(cacheEntryTimeout + 1); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified + 1000, toDateTime(lastModified)); }, 200, "OK", html); // 304 must still work when cacheEntry.isOutOfDate() == true, https://github.com/vert-x3/vertx-web/issues/726 Thread.sleep(cacheEntryTimeout + 1); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified + 1000)), 304, "Not Modified", null); }
@Test public void testCacheFilesFileDeleted() throws Exception { File webroot = new File(".vertx/webroot"), pageFile = new File(webroot, "deleted.html"); if (!pageFile.exists()) { webroot.mkdirs(); pageFile.createNewFile(); } String page = '/' + pageFile.getName(); stat.setFilesReadOnly(false); stat.setWebRoot(webroot.getPath()); stat.setCacheEntryTimeout(3600 * 1000); long modified = Utils.secondsFactor(pageFile.lastModified()); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); pageFile.delete(); testRequest(HttpMethod.GET, page, 404, "Not Found"); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 404, "Not Found", null); }
@Test public void testCacheFilesEntryOld() throws Exception { String webroot = "src/test/filesystemwebroot", page = "/fspage.html"; File resource = new File(webroot + page); String html = new String(Files.readAllBytes(resource.toPath())); int cacheEntryTimeout = 100; stat.setFilesReadOnly(false); stat.setWebRoot(webroot); stat.setCacheEntryTimeout(cacheEntryTimeout); long modified = Utils.secondsFactor(resource.lastModified()); testRequest(HttpMethod.GET, page, null, res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified, toDateTime(lastModified)); // Now update the web resource resource.setLastModified(modified + 1000); }, 200, "OK", html); // But it should return a new entry as the entry is now old Thread.sleep(cacheEntryTimeout + 1); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified + 1000, toDateTime(lastModified)); }, 200, "OK", html); // 304 must still work when cacheEntry.isOutOfDate() == true, https://github.com/vert-x3/vertx-web/issues/726 Thread.sleep(cacheEntryTimeout + 1); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified + 1000)), 304, "Not Modified", null); }