Skip to content

Commit

Permalink
Issue ktorio#1903. Added code to parse the url if valid else return null
Browse files Browse the repository at this point in the history
  • Loading branch information
chetankokil authored and chetankokil committed Aug 28, 2022
1 parent ecd1421 commit 886c276
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ktor-http/common/src/io/ktor/http/Url.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ public class Url internal constructor(
return urlString.hashCode()
}

public companion object
public companion object {
public fun parse(urlString: String) : Url? {
return Url(urlString)
}
}
}

@Suppress("UNUSED_PARAMETER")
Expand Down
14 changes: 14 additions & 0 deletions ktor-http/common/test/io/ktor/tests/http/UrlTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,18 @@ class UrlTest {
val url = Url(urlString)
assertEquals(urlString, "$url")
}


@Test
fun testUrlWithGivenString() {
val validUrl = "https://localhost:8080/validUrl"
assertNotNull(Url.parse(validUrl), "Invalid Url")
}


@Test
fun testInvalidUrlReturnsNull() {
val bogusUrl = "bogus"
assertNull(Url.parse(bogusUrl))
}
}

0 comments on commit 886c276

Please sign in to comment.