aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-11-27 12:22:29 +0100
committeradamw <adam@warski.org>2017-11-27 12:22:55 +0100
commit8cf5bdc708a46fa9e842481db54caaf59123e4e6 (patch)
treed55a376b2b27a355e0d9f69377cdcfda2c669616 /core
parentfc07b4ac1c67c9a096fe1d63bf5049eabff8b6c1 (diff)
downloadsttp-8cf5bdc708a46fa9e842481db54caaf59123e4e6.tar.gz
sttp-8cf5bdc708a46fa9e842481db54caaf59123e4e6.tar.bz2
sttp-8cf5bdc708a46fa9e842481db54caaf59123e4e6.zip
Basic validation of a constructed URI
Diffstat (limited to 'core')
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/Uri.scala6
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala2
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/UriTests.scala17
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)
+ }
+ }
}