From 451f664544ee2abbdb78ebe0db50c4049f3e5b76 Mon Sep 17 00:00:00 2001 From: Anton Erofeev Date: Sun, 27 Oct 2024 19:52:54 +0300 Subject: [PATCH] KTOR-7625: Url.segments parsing fix --- ktor-http/common/src/io/ktor/http/Url.kt | 2 +- ktor-http/common/test/io/ktor/tests/http/UrlTest.kt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ktor-http/common/src/io/ktor/http/Url.kt b/ktor-http/common/src/io/ktor/http/Url.kt index 81f3bfbf9e0..d50c96d8c4a 100644 --- a/ktor-http/common/src/io/ktor/http/Url.kt +++ b/ktor-http/common/src/io/ktor/http/Url.kt @@ -137,7 +137,7 @@ public class Url internal constructor( **/ public val segments: List by lazy { if (pathSegments.isEmpty()) return@lazy emptyList() - val start = if (pathSegments.first().isEmpty()) 1 else 0 + val start = if (pathSegments.first().isEmpty() && pathSegments.size > 1) 1 else 0 val end = if (pathSegments.last().isEmpty()) pathSegments.lastIndex else pathSegments.lastIndex + 1 pathSegments.subList(start, end) } diff --git a/ktor-http/common/test/io/ktor/tests/http/UrlTest.kt b/ktor-http/common/test/io/ktor/tests/http/UrlTest.kt index 6c9141e1d5e..58256fa6c43 100644 --- a/ktor-http/common/test/io/ktor/tests/http/UrlTest.kt +++ b/ktor-http/common/test/io/ktor/tests/http/UrlTest.kt @@ -34,6 +34,8 @@ class UrlTest { val relative = Url("docs") val relativeWithTrailing = Url("docs/") val empty = Url("https://ktor.io") + val emptyWithTrailing = Url("http://ktor.io/") + val expected = listOf("docs") assertContentEquals(expected, full.segments) @@ -42,6 +44,7 @@ class UrlTest { assertContentEquals(expected, relative.segments) assertContentEquals(expected, relativeWithTrailing.segments) assertContentEquals(emptyList(), empty.segments) + assertContentEquals(emptyList(), emptyWithTrailing.segments) } @Test