aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-11-27 12:50:52 +0100
committeradamw <adam@warski.org>2017-11-27 12:50:52 +0100
commitb6d01181152ca11747ec5d2feabfa3621d61f73c (patch)
treeafd6b0bd6a10a4d4e13008437a1bf1897eba2943 /core
parent8cf5bdc708a46fa9e842481db54caaf59123e4e6 (diff)
downloadsttp-b6d01181152ca11747ec5d2feabfa3621d61f73c.tar.gz
sttp-b6d01181152ca11747ec5d2feabfa3621d61f73c.tar.bz2
sttp-b6d01181152ca11747ec5d2feabfa3621d61f73c.zip
Validating that the port is specified only once when interpolating a URI
Diffstat (limited to 'core')
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala4
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala27
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/UriTests.scala2
3 files changed, 29 insertions, 4 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
index 4bf6986..f77951c 100644
--- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
@@ -344,6 +344,10 @@ object UriInterpolator {
case t => Vector(t)
}
+ if (hpTokens.count(_ == ColonInAuthority) > 1) {
+ throw new IllegalArgumentException("port specified multiple times")
+ }
+
split(hpTokens, Set[Token](ColonInAuthority)) match {
case Left(tt) => hostFromTokens(u, tt)
case Right((hostTokens, _, portTokens)) =>
diff --git a/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala
index 92c90c3..f8d32ed 100644
--- a/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala
+++ b/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala
@@ -71,8 +71,7 @@ class UriInterpolatorTests extends FunSuite with Matchers {
(uri"http://example.com:${8080}/x", s"http://example.com:8080/x"),
(uri"http://example.com:${Some(8080)}/x", s"http://example.com:8080/x"),
(uri"http://example.com:$None/x", s"http://example.com/x"),
- (uri"http://${"example.com:8080"}", s"http://example.com:8080"),
- (uri"http://${"example.com:8080"}:$None", s"http://example.com:8080")
+ (uri"http://${"example.com:8080"}", s"http://example.com:8080")
),
"path" -> List(
(uri"http://example.com/$v1", s"http://example.com/$v1"),
@@ -145,8 +144,30 @@ class UriInterpolatorTests extends FunSuite with Matchers {
(groupName, testCases) <- testData
((interpolated, expected), i) <- testCases.zipWithIndex
} {
- test(s"[$groupName] interpolate to $expected (${i + 1})") {
+ test(s"[$groupName] should interpolate to $expected (${i + 1})") {
interpolated.toString should be(expected)
}
}
+
+ val validationTestData = List(
+ ("uri with two ports",
+ () => uri"http://example.com:80:80",
+ "port specified multiple times"),
+ ("uri with embedded host+port and port",
+ () => uri"http://${"example.com:80"}:80",
+ "port specified multiple times")
+ )
+
+ for {
+ (name, createUri, expectedException) <- validationTestData
+ } {
+ test(
+ s"""$name should validate and throw "$expectedException" if not valid""") {
+ val caught = intercept[IllegalArgumentException] {
+ createUri()
+ }
+
+ caught.getMessage.toLowerCase() should include(expectedException)
+ }
+ }
}
diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
index 73cb05d..b94ac72 100644
--- a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
+++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
@@ -145,7 +145,7 @@ class UriTests extends FunSuite with Matchers {
for {
(createUri, expectedException) <- validationTestData
} {
- test(s"""should validate URI and throw "$expectedException" if not valid""") {
+ test(s"""should validate and throw "$expectedException" if not valid""") {
val caught = intercept[IllegalArgumentException] {
createUri()
}