router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
@Test(expected = IllegalArgumentException.class) public void testAccessToRootPath() throws Exception { router.clear(); File file = File.createTempFile("vertx", "tmp"); file.deleteOnExit(); // remap stat to the temp dir stat = StaticHandler.create().setWebRoot(file.getParent()); }
@Test public void testServeFilesFromFilesystem() throws Exception { stat.setWebRoot("src/test/filesystemwebroot"); testRequest(HttpMethod.GET, "/fspage.html", 200, "OK", "<html><body>File system page</body></html>"); }
@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 testLinkPreload() throws Exception { List<Http2PushMapping> mappings = new ArrayList<>(); mappings.add(new Http2PushMapping("style.css", "style", false)); mappings.add(new Http2PushMapping("coin.png", "image", false)); stat.setHttp2PushMapping(mappings) .setWebRoot("webroot/somedir3"); testRequest(HttpMethod.GET, "/testLinkPreload.html", null, res -> { List<String> linkHeaders = res.headers().getAll("Link"); assertTrue(linkHeaders.contains("<style.css>; rel=preload; as=style")); assertTrue(linkHeaders.contains("<coin.png>; rel=preload; as=image")); }, 200, "OK", null); }
@Test public void testNoLinkPreload() throws Exception { stat.setWebRoot("webroot/somedir3"); testRequest(HttpMethod.GET, "/testLinkPreload.html", null, res -> { List<String> linkHeaders = res.headers().getAll("Link"); assertTrue(linkHeaders.isEmpty()); }, 200, "OK", null); }
@Test public void testServerFileSystemPath() throws Exception { router.clear(); File file = File.createTempFile("vertx", "tmp"); file.deleteOnExit(); // remap stat to the temp dir try { stat = StaticHandler.create(file.getParent()); fail(); } catch (IllegalArgumentException e) { // expected } stat = StaticHandler.create().setAllowRootFileSystemAccess(true).setWebRoot(file.getParent()); router.route().handler(stat); testRequest(HttpMethod.GET, "/" + file.getName(), 200, "OK", ""); }
/** * Set the web root * @param webRoot the web root * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setWebRoot(String webRoot) { delegate.setWebRoot(webRoot); return this; }
@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 the web root * @param webRoot the web root * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setWebRoot(String webRoot) { delegate.setWebRoot(webRoot); 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); }
mappings.add(new Http2PushMapping("coin.png", "image", false)); stat.setHttp2PushMapping(mappings) .setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions()
@Test public void testNoHttp2Push() throws Exception { stat.setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))); http2Server.requestHandler(router).listen(8443); HttpClientOptions options = new HttpClientOptions() .setSsl(true) .setUseAlpn(true) .setProtocolVersion(HttpVersion.HTTP_2) .setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem")); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(HttpVersion.HTTP_2, resp.version()); resp.bodyHandler(this::assertNotNull); testComplete(); })); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { fail(); })); request.end(); await(); }
@Test(expected = IllegalArgumentException.class) public void testAccessToRootPath() throws Exception { router.clear(); File file = File.createTempFile("vertx", "tmp"); file.deleteOnExit(); // remap stat to the temp dir stat = StaticHandler.create().setWebRoot(file.getParent()); }
@Test public void testServeFilesFromFilesystem() throws Exception { stat.setWebRoot("src/test/filesystemwebroot"); testRequest(HttpMethod.GET, "/fspage.html", 200, "OK", "<html><body>File system page</body></html>"); }
@Test public void testLinkPreload() throws Exception { List<Http2PushMapping> mappings = new ArrayList<>(); mappings.add(new Http2PushMapping("style.css", "style", false)); mappings.add(new Http2PushMapping("coin.png", "image", false)); stat.setHttp2PushMapping(mappings) .setWebRoot("webroot/somedir3"); testRequest(HttpMethod.GET, "/testLinkPreload.html", null, res -> { List<String> linkHeaders = res.headers().getAll("Link"); assertTrue(linkHeaders.contains("<style.css>; rel=preload; as=style")); assertTrue(linkHeaders.contains("<coin.png>; rel=preload; as=image")); }, 200, "OK", null); }
@Test public void testNoLinkPreload() throws Exception { stat.setWebRoot("webroot/somedir3"); testRequest(HttpMethod.GET, "/testLinkPreload.html", null, res -> { List<String> linkHeaders = res.headers().getAll("Link"); assertTrue(linkHeaders.isEmpty()); }, 200, "OK", 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); }