mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-10-31 16:22:49 +00:00
Make ensureProtocol case-insensitive
This commit is contained in:
@@ -20,12 +20,13 @@ fun String.isValidUrl(): Boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure string starts with "http". If it is not the case, "https://" is added, only if the String is not empty
|
||||
* Ensure string starts with "http" (case-insensitively, so "HTTP://" / "HTTPS://" are respected). If it is
|
||||
* not the case, "https://" is added, only if the String is not empty.
|
||||
*/
|
||||
fun String.ensureProtocol(): String {
|
||||
return when {
|
||||
isEmpty() -> this
|
||||
!startsWith("http") -> "https://$this"
|
||||
!startsWith("http", ignoreCase = true) -> "https://$this"
|
||||
else -> this
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.core.uri
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
class UrlUtilsTest {
|
||||
@Test
|
||||
fun `ensureProtocol keeps an empty string unchanged`() {
|
||||
assertThat("".ensureProtocol()).isEqualTo("")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensureProtocol prepends https when there is no scheme`() {
|
||||
assertThat("matrix.org".ensureProtocol()).isEqualTo("https://matrix.org")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensureProtocol keeps an existing http or https scheme`() {
|
||||
assertThat("https://matrix.org".ensureProtocol()).isEqualTo("https://matrix.org")
|
||||
assertThat("http://matrix.org".ensureProtocol()).isEqualTo("http://matrix.org")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensureProtocol keeps an uppercase scheme rather than double-prepending`() {
|
||||
assertThat("HTTPS://matrix.org".ensureProtocol()).isEqualTo("HTTPS://matrix.org")
|
||||
assertThat("Http://matrix.org".ensureProtocol()).isEqualTo("Http://matrix.org")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user