aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-07-31 14:26:06 +0200
committeradamw <adam@warski.org>2017-07-31 14:26:06 +0200
commitf34ad777382ca49c70a1dce655bfb6e483995716 (patch)
tree8a80eaa026b1761db3c13e8d05b3915025b5ce93 /core
parent4d23d6fd21317e6bf28cd23b0e149d570b55f5e8 (diff)
downloadsttp-f34ad777382ca49c70a1dce655bfb6e483995716.tar.gz
sttp-f34ad777382ca49c70a1dce655bfb6e483995716.tar.bz2
sttp-f34ad777382ca49c70a1dce655bfb6e483995716.zip
Support for user info in the Uri class
Diffstat (limited to 'core')
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/Uri.scala37
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala2
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/UriTests.scala11
3 files changed, 33 insertions, 17 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/Uri.scala b/core/src/main/scala/com/softwaremill/sttp/Uri.scala
index 1ee4337..6b2e840 100644
--- a/core/src/main/scala/com/softwaremill/sttp/Uri.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/Uri.scala
@@ -15,6 +15,7 @@ import scala.collection.immutable.Seq
* added manually as part of the plain query fragment.
*/
case class Uri(scheme: String,
+ userInfo: Option[String],
host: String,
port: Option[Int],
path: Seq[String],
@@ -22,20 +23,24 @@ case class Uri(scheme: String,
fragment: Option[String]) {
def this(host: String) =
- this("http", host, None, Vector.empty, Vector.empty, None)
+ this("http", None, host, None, Vector.empty, Vector.empty, None)
def this(host: String, port: Int) =
- this("http", host, Some(port), Vector.empty, Vector.empty, None)
+ this("http", None, host, Some(port), Vector.empty, Vector.empty, None)
def this(host: String, port: Int, path: Seq[String]) =
- this("http", host, Some(port), path, Vector.empty, None)
+ this("http", None, host, Some(port), path, Vector.empty, None)
def this(scheme: String, host: String) =
- this(scheme, host, None, Vector.empty, Vector.empty, None)
+ this(scheme, None, host, None, Vector.empty, Vector.empty, None)
def this(scheme: String, host: String, port: Int) =
- this(scheme, host, Some(port), Vector.empty, Vector.empty, None)
+ this(scheme, None, host, Some(port), Vector.empty, Vector.empty, None)
def this(scheme: String, host: String, port: Int, path: Seq[String]) =
- this(scheme, host, Some(port), path, Vector.empty, None)
+ this(scheme, None, host, Some(port), path, Vector.empty, None)
def scheme(s: String): Uri = this.copy(scheme = s)
+ def userInfo(ui: String): Uri = this.copy(userInfo = Some(ui))
+
+ def userInfo(ui: Option[String]): Uri = this.copy(userInfo = ui)
+
def host(h: String): Uri = this.copy(host = h)
def port(p: Int): Uri = this.copy(port = Some(p))
@@ -87,12 +92,10 @@ case class Uri(scheme: String,
def fragment(f: Option[String]): Uri = this.copy(fragment = f)
override def toString: String = {
- val schemeS = encode(scheme)
- val hostS = encode(host)
- val portS = port.fold("")(":" + _)
- val pathPrefixS = if (path.isEmpty) "" else "/"
- val pathS = path.map(encode).mkString("/")
- val queryPrefixS = if (queryFragments.isEmpty) "" else "?"
+ def encodeUserInfo(ui: String): String = ui.split(":", 2) match {
+ case Array(u) => encode(u)
+ case Array(u, p) => encode(u) + ":" + encode(p)
+ }
@tailrec
def encodeQueryFragments(qfs: List[QueryFragment],
@@ -111,11 +114,19 @@ case class Uri(scheme: String,
encodeQueryFragments(t, previousWasKV = true, sb)
}
+ val schemeS = encode(scheme)
+ val userInfoS = userInfo.fold("")(encodeUserInfo(_) + "@")
+ val hostS = encode(host)
+ val portS = port.fold("")(":" + _)
+ val pathPrefixS = if (path.isEmpty) "" else "/"
+ val pathS = path.map(encode).mkString("/")
+ val queryPrefixS = if (queryFragments.isEmpty) "" else "?"
+
val queryS = encodeQueryFragments(queryFragments.toList,
previousWasKV = false,
new StringBuilder())
val fragS = fragment.fold("")("#" + _)
- s"$schemeS://$hostS$portS$pathPrefixS$pathS$queryPrefixS$queryS$fragS"
+ s"$schemeS://$userInfoS$hostS$portS$pathPrefixS$pathS$queryPrefixS$queryS$fragS"
}
private def encode(s: Any): String = {
diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
index 26b9827..025f626 100644
--- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala
@@ -57,7 +57,7 @@ object UriInterpolator {
private def append(x: String): Scheme = Scheme(v + x)
- override def build: Uri = Uri(v, "", None, Nil, Nil, None)
+ override def build: Uri = Uri(v, None, "", None, Nil, Nil, None)
}
case class Authority(s: Scheme, v: String = "") extends UriBuilder {
diff --git a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
index 933afff..e137d5c 100644
--- a/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
+++ b/core/src/test/scala/com/softwaremill/sttp/UriTests.scala
@@ -7,8 +7,9 @@ class UriTests extends FunSuite with Matchers {
val QF = QueryFragment
val wholeUriTestData = List(
- Uri("http", "example.com", None, Nil, Nil, None) -> "http://example.com",
+ Uri("http", None, "example.com", None, Nil, Nil, None) -> "http://example.com",
Uri("https",
+ None,
"sub.example.com",
Some(8080),
List("a", "b", "xyz"),
@@ -16,18 +17,22 @@ class UriTests extends FunSuite with Matchers {
Some("f")) ->
"https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f",
Uri("http",
+ None,
"example.com",
None,
List(""),
List(QF.KeyValue("p", "v"), QF.KeyValue("p", "v")),
None) -> "http://example.com/?p=v&p=v",
Uri("http",
+ None,
"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"
+ "http://exa%20mple.com/a%20b/z/%C4%85%3A%C4%99?p%3A1=v%26v&p2=v+v",
+ Uri("http", Some("us&er:pa ss"), "example.com", None, Nil, Nil, None) ->
+ "http://us%26er:pa%20ss@example.com",
)
for {
@@ -38,7 +43,7 @@ class UriTests extends FunSuite with Matchers {
}
}
- val testUri = Uri("http", "example.com", None, Nil, Nil, None)
+ val testUri = Uri("http", None, "example.com", None, Nil, Nil, None)
val pathTestData = List(
"a/b/c" -> List("a", "b", "c"),