fix test and move check to be specific to host check

This commit is contained in:
Dave Severns 2024-10-30 15:29:45 -04:00
parent c18f3ebeb9
commit 94eeaf5992
2 changed files with 42 additions and 22 deletions

View file

@ -15,16 +15,7 @@ private const val ANDROID_APP_PROTOCOL: String = "androidapp://"
*/
fun String.toUriOrNull(): URI? =
try {
val uri = URI(this)
if (
uri.host == null &&
this.contains(".") &&
!this.hasHttpProtocol()
) {
URI("https://$this")
} else {
uri
}
URI(this)
} catch (e: URISyntaxException) {
null
}
@ -80,7 +71,18 @@ fun String.hasPort(): Boolean {
* Extract the host from this [String] if possible, otherwise return null.
*/
@OmitFromCoverage
fun String.getHostOrNull(): String? = this.toUriOrNull()?.host
fun String.getHostOrNull(): String? = this.toUriOrNull()
?.let { uri ->
if (
uri.host == null &&
this.contains(".") &&
!this.hasHttpProtocol()
) {
URI("https://$this")
} else {
uri
}
}?.host
/**
* Extract the host with optional port from this [String] if possible, otherwise return null.

View file

@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.util
import com.x8bit.bitwarden.data.platform.manager.ResourceCacheManager
import com.x8bit.bitwarden.data.platform.util.findLastSubstringIndicesOrNull
import com.x8bit.bitwarden.data.platform.util.getDomainOrNull
import com.x8bit.bitwarden.data.platform.util.getHostOrNull
import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
import com.x8bit.bitwarden.data.platform.util.hasHttpProtocol
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
@ -39,17 +40,6 @@ class StringExtensionsTest {
assertNotNull("www.google.com".toUriOrNull())
}
@Suppress("MaxLineLength")
@Test
fun `toUriOrNull should return URI with accurate host name when a scheme is not present but a port is`() {
val expectedHost = "www.google.com"
val hostWithPort = "$expectedHost:8080"
// control
assertNull(URI(hostWithPort).host)
val uri = hostWithPort.toUriOrNull()
assertEquals(expectedHost, uri?.host)
}
@Test
fun `isAndroidApp should return true when string starts with android app protocol`() {
assertTrue("androidapp://com.x8bit.bitwarden".isAndroidApp())
@ -165,4 +155,32 @@ class StringExtensionsTest {
// Verify
assertEquals(expected, actual)
}
@Test
fun `getHostOrNull should return host when one is present`() {
val expectedHost = "www.google.com"
assertEquals(expectedHost, expectedHost.getHostOrNull())
}
@Test
fun `getHostOrNull should return null when no host is present`() {
assertNull("boo".getHostOrNull())
}
@Test
fun `getHostOrNull should return host from URI string when present and custom URI scheme`() {
val expectedHost = "www.google.com"
val hostWithScheme = "androidapp://$expectedHost"
assertEquals(expectedHost, hostWithScheme.getHostOrNull())
}
@Suppress("MaxLineLength")
@Test
fun `getHostOrNull should return host from URI string when present and has port but no scheme`() {
val expectedHost = "www.google.com"
val hostWithPort = "$expectedHost:8080"
// control
assertNull(URI(hostWithPort).host)
assertEquals(expectedHost, hostWithPort.toUriOrNull())
}
}