diff options
Diffstat (limited to 'core')
3 files changed, 24 insertions, 1 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/Uri.scala b/core/src/main/scala/com/softwaremill/sttp/Uri.scala index dac16b8..16936cc 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Uri.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Uri.scala @@ -28,6 +28,12 @@ case class Uri(scheme: String, queryFragments: Seq[QueryFragment], fragment: Option[String]) { + private val AllowedSchemeCharacters = "[a-zA-Z][a-zA-Z0-9+-.]*".r + + require(host.nonEmpty, "Host cannot be empty") + require(AllowedSchemeCharacters.unapplySeq(scheme).isDefined, + "Scheme can only contain alphanumeric characters, +, - and .") + def scheme(s: String): Uri = this.copy(scheme = s) def userInfo(username: String): Uri = diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala index fba3eaa..4bf6986 100644 --- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala +++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala @@ -18,7 +18,7 @@ object UriInterpolator { UriBuilder.Fragment ) - val startingUri = Uri("") + val startingUri = Uri("-") val (uri, leftTokens) = builders.foldLeft((startingUri, tokens)) { diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala index 23fe3d5..73cb05d 100644 --- a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala @@ -136,4 +136,21 @@ class UriTests extends FunSuite with Matchers { val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f" uri"$uriAsString".toJavaUri.toString should be(uriAsString) } + + val validationTestData = List( + (() => Uri("")) -> "host cannot be empty", + (() => Uri("h ttp", "example.org")) -> "scheme" + ) + + for { + (createUri, expectedException) <- validationTestData + } { + test(s"""should validate URI and throw "$expectedException" if not valid""") { + val caught = intercept[IllegalArgumentException] { + createUri() + } + + caught.getMessage.toLowerCase() should include(expectedException) + } + } } |