diff options
Diffstat (limited to 'core/src/test/scala/com')
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala | 13 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/UriTests.scala | 82 |
2 files changed, 90 insertions, 5 deletions
diff --git a/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala index 85348e6..9f0f081 100644 --- a/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/UriInterpolatorTests.scala @@ -1,7 +1,5 @@ package com.softwaremill.sttp -import java.net.URI - import org.scalatest.{FunSuite, Matchers} class UriInterpolatorTests extends FunSuite with Matchers { @@ -15,7 +13,7 @@ class UriInterpolatorTests extends FunSuite with Matchers { val v4encoded = "f%2Fg" val secure = true - val testData: List[(String, List[(URI, String)])] = List( + val testData: List[(String, List[(Uri, String)])] = List( "basic" -> List( (uri"http://example.com", "http://example.com"), (uri"http://example.com/", "http://example.com/"), @@ -30,7 +28,7 @@ class UriInterpolatorTests extends FunSuite with Matchers { s"https://example.com"), (uri"${if (secure) "https" else "http"}://example.com", s"https://example.com"), - (uri"example.com?a=$v2", s"example.com?a=$v2queryEncoded") + (uri"example.com?a=$v2", s"http://example.com?a=$v2queryEncoded") ), "authority" -> List( (uri"http://$v1.com", s"http://$v1.com"), @@ -74,6 +72,11 @@ class UriInterpolatorTests extends FunSuite with Matchers { (uri"http://example.com?x=$v2", s"http://example.com?x=$v2queryEncoded"), (uri"http://example.com?x=$v3", s"http://example.com?x=$v3encoded") ), + "query parameter without value" -> List( + (uri"http://example.com?$v1", s"http://example.com?$v1"), + (uri"http://example.com?$v1&$v2", + s"http://example.com?$v1&$v2queryEncoded") + ), "optional query parameters" -> List( (uri"http://example.com?a=$None", s"http://example.com"), (uri"http://example.com?a=b&c=$None", s"http://example.com?a=b"), @@ -115,7 +118,7 @@ class UriInterpolatorTests extends FunSuite with Matchers { ((interpolated, expected), i) <- testCases.zipWithIndex } { test(s"[$groupName] interpolate to $expected (${i + 1})") { - interpolated should be(new URI(expected)) + interpolated.toString should be(expected) } } } diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala new file mode 100644 index 0000000..70396a6 --- /dev/null +++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala @@ -0,0 +1,82 @@ +package com.softwaremill.sttp + +import org.scalatest.{FunSuite, Matchers} + +class UriTests extends FunSuite with Matchers { + + val QF = QueryFragment + + val wholeUriTestData = List( + Uri("http", "example.com", None, Nil, Nil, None) -> "http://example.com", + Uri("https", + "sub.example.com", + Some(8080), + List("a", "b", "xyz"), + List(QF.KeyValue("p1", "v1"), QF.KeyValue("p2", "v2")), + Some("f")) -> + "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f", + Uri("http", + "example.com", + None, + List(""), + List(QF.KeyValue("p", "v"), QF.KeyValue("p", "v")), + None) -> "http://example.com/?p=v&p=v", + Uri("http", + "exa mple.com", + None, + List("a b", "z", "ą:ę"), + List(QF.KeyValue("p:1", "v&v"), QF.KeyValue("p2", "v v")), + None) -> + "http://exa%20mple.com/a%20b/z/%C4%85%3A%C4%99?p%3A1=v%26v&p2=v+v" + ) + + for { + (uri, expected) <- wholeUriTestData + } { + test(s"$uri should serialize to $expected") { + uri.toString should be(expected) + } + } + + val testUri = Uri("http", "example.com", None, Nil, Nil, None) + + val pathTestData = List( + "a/b/c" -> List("a", "b", "c"), + "/a/b/c" -> List("a", "b", "c"), + "/" -> List(""), + "" -> List("") + ) + + for { + (path, expected) <- pathTestData + } { + test(s"$path should parse as $expected") { + testUri.path(path).path.toList should be(expected) + } + } + + val queryFragmentsTestData = List( + List(QF.KeyValue("k1", "v1"), + QF.KeyValue("k2", "v2"), + QF.KeyValue("k3", "v3"), + QF.KeyValue("k4", "v4")) -> "k1=v1&k2=v2&k3=v3&k4=v4", + List(QF.KeyValue("k1", "v1"), + QF.KeyValue("k2", "v2"), + QF.Plain("-abc-"), + QF.KeyValue("k3", "v3"), + QF.KeyValue("k4", "v4")) -> "k1=v1&k2=v2-abc-k3=v3&k4=v4", + List(QF.KeyValue("k1", "v1"), QF.Plain("&abc&"), QF.KeyValue("k2", "v2")) -> "k1=v1%26abc%26k2=v2", + List(QF.KeyValue("k1", "v1"), QF.Plain("&abc&", relaxedEncoding = true)) -> "k1=v1&abc&", + List(QF.KeyValue("k1?", "v1?", keyRelaxedEncoding = true)) -> "k1?=v1%3F", + List(QF.KeyValue("k1?", "v1?", valueRelaxedEncoding = true)) -> "k1%3F=v1?", + List(QF.Plain("ą/ę&+;?", relaxedEncoding = true)) -> "%C4%85/%C4%99&+;?", + ) + + for { + (fragments, expected) <- queryFragmentsTestData + } { + test(s"$fragments should serialize to$expected") { + testUri.copy(queryFragments = fragments).toString should endWith(expected) + } + } +} |