From 05c8510fa4bcb399bd4528241da5e5c839b24ea5 Mon Sep 17 00:00:00 2001 From: Alessio Coser Date: Tue, 28 Jan 2020 10:19:57 +0100 Subject: [PATCH] serve assets inside a path --- src/main/kotlin/daikon/HttpServer.kt | 2 +- src/test/kotlin/daikon/HttpRoutingTest.kt | 20 +++++++++++++++----- src/test/resources/assets/foo/bar/style.css | 1 + 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/assets/foo/bar/style.css diff --git a/src/main/kotlin/daikon/HttpServer.kt b/src/main/kotlin/daikon/HttpServer.kt index b88ff65..2450061 100644 --- a/src/main/kotlin/daikon/HttpServer.kt +++ b/src/main/kotlin/daikon/HttpServer.kt @@ -174,7 +174,7 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.() fun assets(path: String): HttpServer { val servletHolder = ServletHolder(DefaultServlet()) - handler.addServlet(servletHolder, path) + handler.addServlet(servletHolder, joinPaths(path)) handler.baseResource = Resource.newResource(HttpServer::class.java.getResource("/assets/")) return this } diff --git a/src/test/kotlin/daikon/HttpRoutingTest.kt b/src/test/kotlin/daikon/HttpRoutingTest.kt index 46fcf78..19492ee 100644 --- a/src/test/kotlin/daikon/HttpRoutingTest.kt +++ b/src/test/kotlin/daikon/HttpRoutingTest.kt @@ -43,11 +43,21 @@ class HttpRoutingTest { @Test fun `mix of static file and dynamic routes`() { HttpServer() - .get("/bar/2") { _, res -> res.write("Hello") } - .assets("/foo/*") - .start().use { - assertThat(get("/foo/style.css").text).isEqualTo("body {}") - assertThat(get("/bar/2").text).isEqualTo("Hello") + .get("/bar/2") { _, res -> res.write("Hello") } + .assets("/foo/*") + .start().use { + assertThat(get("/foo/style.css").text).isEqualTo("body {}") + assertThat(get("/bar/2").text).isEqualTo("Hello") + } + } + + @Test + fun `serve assets inside a path`() { + HttpServer() + .path("/foo") { + assets("/bar/*") + }.start().use { + assertThat(get("/foo/bar/style.css").text).isEqualTo("body {}") } } diff --git a/src/test/resources/assets/foo/bar/style.css b/src/test/resources/assets/foo/bar/style.css new file mode 100644 index 0000000..2d91681 --- /dev/null +++ b/src/test/resources/assets/foo/bar/style.css @@ -0,0 +1 @@ +body {} \ No newline at end of file