diff options
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/package.scala | 17 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/RequestTests.scala | 43 |
2 files changed, 57 insertions, 3 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index dc551b6..903a2fd 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -133,6 +133,16 @@ package object sttp { else headers this.copy(headers = current :+ (k -> v)) } + def headers(hs: Map[String, String]): RequestTemplate[U] = + this.copy(headers = headers ++ hs.toSeq) + def headers(hs: (String, String)*): RequestTemplate[U] = + this.copy(headers = headers ++ hs) + def cookie(nv: (String, String)): RequestTemplate[U] = cookies(nv) + def cookie(n: String, v: String): RequestTemplate[U] = cookies((n, v)) + def cookies(r: Response[_]): RequestTemplate[U] = + cookies(r.cookies.map(c => (c.name, c.value)): _*) + def cookies(nvs: (String, String)*): RequestTemplate[U] = + header(CookieHeader, nvs.map(p => p._1 + "=" + p._2).mkString("; ")) /** * If content type is not yet specified, will be set to `text/plain` with `utf-8` encoding. @@ -226,9 +236,10 @@ package object sttp { val sttp: RequestTemplate[Empty] = RequestTemplate.empty - private[sttp] val ContentTypeHeader = "content-type" - private[sttp] val ContentLengthHeader = "content-length" - private[sttp] val SetCookieHeader = "set-cookie" + private[sttp] val ContentTypeHeader = "Content-Type" + private[sttp] val ContentLengthHeader = "Content-Length" + private[sttp] val SetCookieHeader = "Set-Cookie" + private[sttp] val CookieHeader = "Cookie" private val Utf8 = "utf-8" private val ApplicationOctetStreamContentType = "application/octet-stream" diff --git a/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala b/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala new file mode 100644 index 0000000..e6db3d7 --- /dev/null +++ b/core/src/test/scala/com/softwaremill/sttp/RequestTests.scala @@ -0,0 +1,43 @@ +package com.softwaremill.sttp + +import org.scalatest.{FlatSpec, Matchers} + +class RequestTests extends FlatSpec with Matchers { + "request cookies" should "be set from a name-value pair" in { + sttp + .cookie("k", "v") + .headers + .find(_._1 == CookieHeader) + .map(_._2) should be(Some("k=v")) + } + + it should "be set from multiple name-value pairs" in { + sttp + .cookies("k1" -> "v1", "k2" -> "v2") + .headers + .find(_._1 == CookieHeader) + .map(_._2) should be(Some("k1=v1; k2=v2")) + } + + it should "add multiple headers if invoked multiple times" in { + sttp + .cookie("k1", "v1") + .cookie("k2" -> "v2") + .headers + .filter(_._1 == CookieHeader) + .map(_._2) + .toSet should be(Set("k1=v1", "k2=v2")) + } + + it should "set cookies from a response" in { + val response = + Response((), + 0, + List((SetCookieHeader, "k1=v1"), (SetCookieHeader, "k2=v2"))) + sttp + .cookies(response) + .headers + .find(_._1 == CookieHeader) + .map(_._2) should be(Some("k1=v1; k2=v2")) + } +} |