Skip to content

Commit

Permalink
fix(uri-util): refactor uri scheme checker (WPB-3554) (#2090)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8f8ff33)
  • Loading branch information
mchenani committed Aug 16, 2023
1 parent aa3bc1d commit a984c0b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
9 changes: 6 additions & 3 deletions app/src/main/kotlin/com/wire/android/util/UriUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/
package com.wire.android.util

import java.net.URI
import java.net.URLDecoder

fun containsSchema(url: String): Boolean {
val regexPattern = Regex(".+://.+")

return regexPattern.matches(url)
return try {
URI.create(url).scheme != null
} catch (iae: IllegalArgumentException) {
false // invalid URI
}
}

fun normalizeLink(url: String): String {
Expand Down
7 changes: 6 additions & 1 deletion app/src/test/kotlin/com/wire/android/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ package com.wire.android

import kotlin.random.Random

val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
val charPoolWithNumbers: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
val charPool: List<Char> = ('a'..'z') + ('A'..'Z')

fun Random.stringWithNumbers(length: Int) = (1..length)
.map { Random.nextInt(0, charPoolWithNumbers.size).let { charPoolWithNumbers[it] } }
.joinToString("")

fun Random.string(length: Int) = (1..length)
.map { Random.nextInt(0, charPool.size).let { charPool[it] } }
Expand Down
39 changes: 31 additions & 8 deletions app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.wire.android.util

import com.wire.android.string
import org.junit.Ignore
import org.amshove.kluent.internal.assertEquals
import org.junit.jupiter.api.Test
import kotlin.random.Random

Expand All @@ -28,32 +28,55 @@ class UriUtilTest {
val input = "https://google.com"
val expected = "https://google.com"
val actual = normalizeLink(input)
assert(expected == actual)
assertEquals(expected, actual)
}

@Test
fun givenLink_whenTheLinkStartsWithHttp_thenReturnsTheSameLink() {
val input = "http://google.com"
val expected = "http://google.com"
val actual = normalizeLink(input)
assert(expected == actual)
assertEquals(expected, actual)
}

@Test
fun givenLink_whenTheLinkStartsWithMailTo_thenReturnsTheSameLink() {
val input = "mailto:[email protected]"
val expected = "mailto:[email protected]"
val actual = normalizeLink(input)
assertEquals(expected, actual)
}

@Test
fun givenLink_whenTheLinkIsWireDeepLink_thenReturnsTheSameLink() {
val input = "wire://access/?config=https://nginz-https.elna.wire.link/deeplink.json"
val expected = "wire://access/?config=https://nginz-https.elna.wire.link/deeplink.json"
val actual = normalizeLink(input)
assertEquals(expected, actual)
}

@Test
@Ignore
fun givenLink_whenTheLinkStartsWithRandomSchema_thenReturnsTheSameLink() {
val randomString = Random.string(Random.nextInt(5))
val randomString = Random.string(Random.nextInt(1, 5))
val input = "$randomString://google.com"
val expected = "$randomString://google.com"
val actual = normalizeLink(input)
assert(expected == actual)
assertEquals(expected, actual)
}

@Test
fun givenLink_whenTheLinkWithoutSchema_thenReturnsTheLinkWithHttps() {
val input = Random.string(Random.nextInt(20))
val input = Random.string(Random.nextInt(1, 20))
val expected = "https://$input"
val actual = normalizeLink(input)
assertEquals(expected, actual)
}

@Test
fun givenLink_whenTheLinkIsValidWithoutSchema_thenReturnsTheLinkWithHttps() {
val input = "google.com"
val expected = "https://$input"
val actual = normalizeLink(input)
assert(expected == actual)
assertEquals(expected, actual)
}
}

0 comments on commit a984c0b

Please sign in to comment.