Add UriMatcher.isValidUri()

This commit is contained in:
cketti 2023-08-07 19:29:47 +02:00
parent 2985c67098
commit e5e2a9bafb
2 changed files with 48 additions and 0 deletions

View file

@ -31,4 +31,20 @@ object UriMatcher {
parser.parseUri(text, startIndex)
}.filterNotNull().toList()
}
@Suppress("ReturnCount")
fun isValidUri(text: CharSequence): Boolean {
val matchResult = URI_SCHEME.matchAt(text, 0) ?: return false
val matchGroup = matchResult.groups[1]!!
if (matchGroup.range.first != 0) {
return false
}
val scheme = matchGroup.value.lowercase()
val parser = SUPPORTED_URIS[scheme] ?: throw AssertionError("Scheme not found: $scheme")
val uriMatch = parser.parseUri(text, startPos = 0) ?: return false
return uriMatch.startIndex == 0 && uriMatch.endIndex == text.length
}
}

View file

@ -1,10 +1,13 @@
package com.fsck.k9.message.html
import assertk.Assert
import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNotEqualTo
import assertk.assertions.isTrue
import org.junit.Test
class UriMatcherTest {
@ -82,6 +85,27 @@ class UriMatcherTest {
)
}
@Test
fun `valid URIs`() {
assertThat("http://domain.example").isValidUri()
assertThat("https://domain.example/").isValidUri()
assertThat("mailto:user@domain.example").isValidUri()
}
@Test
fun `not URIs`() {
assertThat("some text here").isNotValidUri()
assertThat("some text including the string http:").isNotValidUri()
assertThat("some text including an email address without URI scheme: user@domain.example").isNotValidUri()
}
@Test
fun `valid URIs surrounded by other characters`() {
assertThat("text https://domain.example/").isNotValidUri()
assertThat("https://domain.example/ text").isNotValidUri()
assertThat("<https://domain.example/>").isNotValidUri()
}
private fun assertNoMatch(text: String) {
val uriMatches = UriMatcher.findUris(text)
assertThat(uriMatches).isEmpty()
@ -104,3 +128,11 @@ class UriMatcherTest {
}
}
}
private fun Assert<String>.isValidUri() = given { actual ->
assertThat(UriMatcher.isValidUri(actual)).isTrue()
}
private fun Assert<String>.isNotValidUri() = given { actual ->
assertThat(UriMatcher.isValidUri(actual)).isFalse()
}