From a306b651d8a706b7d3165aa1a825fe39a3dc405e Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 15 Nov 2024 20:23:39 +0000 Subject: [PATCH] Fix SelectorHelpers.computeAbsoluteUrl part of #1117 --- .../mediasource/web/format/SelectorFormat.kt | 15 ++-- .../web/format/SelectorHelpersTest.kt | 85 +++++++++++++++++++ 2 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 app/shared/app-data/src/commonTest/kotlin/domain/mediasource/web/format/SelectorHelpersTest.kt diff --git a/app/shared/app-data/src/commonMain/kotlin/domain/mediasource/web/format/SelectorFormat.kt b/app/shared/app-data/src/commonMain/kotlin/domain/mediasource/web/format/SelectorFormat.kt index e0a4020233..396bd80e84 100644 --- a/app/shared/app-data/src/commonMain/kotlin/domain/mediasource/web/format/SelectorFormat.kt +++ b/app/shared/app-data/src/commonMain/kotlin/domain/mediasource/web/format/SelectorFormat.kt @@ -10,6 +10,8 @@ package me.him188.ani.app.domain.mediasource.web.format import androidx.compose.runtime.Immutable +import io.ktor.http.URLBuilder +import io.ktor.http.appendPathSegments import kotlinx.serialization.Serializable import kotlin.jvm.JvmInline @@ -48,15 +50,8 @@ fun Regex.Companion.parseOrNull(regex: String): Regex? { object SelectorHelpers { fun computeAbsoluteUrl(baseUrl: String, relativeUrl: String): String { - @Suppress("NAME_SHADOWING") - var baseUrl = baseUrl - if (baseUrl.endsWith('/')) { - baseUrl = baseUrl.dropLast(1) - } - return when { - relativeUrl.startsWith("http") -> relativeUrl - relativeUrl.startsWith('/') -> baseUrl + relativeUrl - else -> "$baseUrl/$relativeUrl" - } + return URLBuilder(baseUrl) + .appendPathSegments(relativeUrl, encodeSlash = false) + .buildString() } } \ No newline at end of file diff --git a/app/shared/app-data/src/commonTest/kotlin/domain/mediasource/web/format/SelectorHelpersTest.kt b/app/shared/app-data/src/commonTest/kotlin/domain/mediasource/web/format/SelectorHelpersTest.kt new file mode 100644 index 0000000000..81368dd1c0 --- /dev/null +++ b/app/shared/app-data/src/commonTest/kotlin/domain/mediasource/web/format/SelectorHelpersTest.kt @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2024 OpenAni and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. + * + * https://github.com/open-ani/ani/blob/main/LICENSE + */ + +package me.him188.ani.app.domain.mediasource.web.format + +import me.him188.ani.test.TestContainer +import me.him188.ani.test.TestFactory +import me.him188.ani.test.runDynamicTests +import kotlin.test.assertEquals + +/** + * @see SelectorHelpers.computeAbsoluteUrl + */ +@TestContainer +class ComputeAbsoluteUrlTest { + @TestFactory + fun computeAbsoluteUrlTest() = runDynamicTests { + fun case( + expected: String, + baseUrl: String, + relativeUrl: String, + ) = add("$baseUrl + $relativeUrl = $expected") { + assertEquals( + expected, + SelectorHelpers.computeAbsoluteUrl(baseUrl, relativeUrl), + ) + } + + case( + "https://example.com/relative", + "https://example.com", "relative", + ) + + case( + "https://example.com/relative", + "https://example.com", "/relative", + ) + + case( + "https://example.com/relative", + "https://example.com/", "/relative", + ) + + case( + "https://example.com/relative/", + "https://example.com/", "/relative/", + ) + + case( + "https://example.com/relative/foo", + "https://example.com/", "/relative/foo", + ) + + case( + "https://example.com/relative/foo/../bar", + "https://example.com/", "/relative/foo/../bar", + ) + + case( + "https://example.com/", + "https://example.com/", "", + ) + + case( + "http://localhost/", + "", "", + ) + + case( + "http://localhost/test", + "", "/test", + ) + + case( + "http://localhost/test", + "", "test", + ) + } +} \ No newline at end of file