@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 testCacheFilesEntryCached() throws Exception { stat.setFilesReadOnly(false); stat.setWebRoot("src/test/filesystemwebroot"); File resource = new File("src/test/filesystemwebroot", "fspage.html"); long modified = resource.lastModified(); testRequest(HttpMethod.GET, "/fspage.html", 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><body>File system page</body></html>"); // But it should still return not modified as the entry is cached testRequest(HttpMethod.GET, "/fspage.html", req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); }
/** * Set whether files are read-only and will never change * @param readOnly whether files are read-only * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setFilesReadOnly(boolean readOnly) { delegate.setFilesReadOnly(readOnly); return this; }
/** * Set whether files are read-only and will never change * @param readOnly whether files are read-only * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setFilesReadOnly(boolean readOnly) { delegate.setFilesReadOnly(readOnly); return this; }
@Test public void testCacheFilesNotReadOnly() throws Exception { stat.setFilesReadOnly(false); stat.setWebRoot("src/test/filesystemwebroot"); long modified = Utils.secondsFactor(new File("src/test/filesystemwebroot", "fspage.html").lastModified()); testRequest(HttpMethod.GET, "/fspage.html", null, res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified, toDateTime(lastModified)); }, 200, "OK", "<html><body>File system page</body></html>"); testRequest(HttpMethod.GET, "/fspage.html", req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", 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); }
@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 testCacheFilesEntryCached() throws Exception { stat.setFilesReadOnly(false); stat.setWebRoot("src/test/filesystemwebroot"); File resource = new File("src/test/filesystemwebroot", "fspage.html"); long modified = resource.lastModified(); testRequest(HttpMethod.GET, "/fspage.html", 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><body>File system page</body></html>"); // But it should still return not modified as the entry is cached testRequest(HttpMethod.GET, "/fspage.html", req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); }
@Test public void testCacheFilesNotReadOnly() throws Exception { stat.setFilesReadOnly(false); stat.setWebRoot("src/test/filesystemwebroot"); long modified = Utils.secondsFactor(new File("src/test/filesystemwebroot", "fspage.html").lastModified()); testRequest(HttpMethod.GET, "/fspage.html", null, res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified, toDateTime(lastModified)); }, 200, "OK", "<html><body>File system page</body></html>"); testRequest(HttpMethod.GET, "/fspage.html", req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", 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); }