Skip to content

Commit

Permalink
fix(message): refactor uri scheme checker
Browse files Browse the repository at this point in the history
  • Loading branch information
mchenani committed Aug 11, 2023
1 parent ac0aff3 commit c90ce2b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 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
17 changes: 17 additions & 0 deletions app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ class UriUtilTest {
assert(expected == actual)
}

@Test
fun givenLink_whenTheLinkStartsWithMailTo_thenReturnsTheSameLink() {
val input = "mailto:[email protected]"
val expected = "mailto:[email protected]"
val actual = normalizeLink(input)
assert(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)
assert(expected == actual)
}


@Test
fun givenLink_whenTheLinkStartsWithRandomSchema_thenReturnsTheSameLink() {
val randomString = Random.string(Random.nextInt(5))
Expand Down

0 comments on commit c90ce2b

Please sign in to comment.