Skip to content

Commit 260884a

Browse files
authored
[routing] Add special consideration for root path (javalin#1808)
Refs javalin#1803
1 parent fc7e7de commit 260884a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

javalin/src/main/java/io/javalin/routing/PathParser.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ class PathParser(private val rawPath: String, routingConfig: RoutingConfig) {
3030
// when multiple slashes are accepted we have to allow 0-n slashes when using ignoreTrailingSlashes
3131
// otherwise we also have to allow multiple slashes when only one slash is specified
3232
when {
33+
// the root path is special: we already add a leading slash during regex construction,
34+
// so we do not need to add an extra slash with the suffix
35+
rawPath == "/" -> ""
3336
routingConfig.ignoreTrailingSlashes -> "/*"
3437
rawPath.endsWith("/") -> "/+"
3538
else -> ""
3639
}
3740
} else {
3841
// if ignoreTrailingSlashes config is set we keep /?, else we use the true path trailing slash : present or absent
3942
when {
43+
// the root path is special: we already add a leading slash during regex construction,
44+
// so we do not need to add an extra slash with the suffix
45+
rawPath == "/" -> ""
4046
routingConfig.ignoreTrailingSlashes -> "/?"
4147
rawPath.endsWith("/") -> "/"
4248
else -> ""

javalin/src/test/java/io/javalin/TestRouting.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,26 @@ class TestRouting {
283283
}
284284
}
285285
}
286+
287+
@Test
288+
fun `root path works with ignoreTrailingSlashes set to false`() = TestUtil.test(Javalin.create {
289+
it.routing.ignoreTrailingSlashes = false
290+
}) { app, http ->
291+
app.get("/") { it.result("root") }
292+
app.get("/home") { it.result("home") }
293+
assertThat(http.getBody("/")).isEqualTo("root")
294+
assertThat(http.getBody("/home")).isEqualTo("home")
295+
}
296+
297+
@Test
298+
fun `root path works with ApiBuilder and ignoreTrailingSlashes set to false`() = TestUtil.test(Javalin.create {
299+
it.routing.ignoreTrailingSlashes = false
300+
}) { app, http ->
301+
app.routes {
302+
get("/") { it.result("root") }
303+
get("/home") { it.result("home") }
304+
}
305+
assertThat(http.getBody("/")).isEqualTo("root")
306+
assertThat(http.getBody("/home")).isEqualTo("home")
307+
}
286308
}

0 commit comments

Comments
 (0)