From 8d9145a04cdd21a471fba61fc19203759b95514d Mon Sep 17 00:00:00 2001 From: adamw Date: Sat, 15 Jul 2017 11:59:10 +0200 Subject: Cookie parsing --- .../scala/com/softwaremill/sttp/BasicTests.scala | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tests/src/test/scala/com') diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 86d28a9..5d21d6f 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -2,10 +2,12 @@ package com.softwaremill.sttp import java.io.ByteArrayInputStream import java.net.URI +import java.time.{ZoneId, ZonedDateTime} import akka.stream.ActorMaterializer import akka.actor.ActorSystem import akka.http.scaladsl.Http +import akka.http.scaladsl.model.DateTime import akka.http.scaladsl.model.headers._ import akka.http.scaladsl.model.headers.CacheDirectives._ import akka.http.scaladsl.server.Directives._ @@ -56,6 +58,32 @@ class BasicTests } } } + } ~ pathPrefix("set_cookies") { + path("with_expires") { + setCookie( + HttpCookie("c", + "v", + expires = Some(DateTime(1997, 12, 8, 12, 49, 12)))) { + complete("ok") + } + } ~ get { + setCookie( + HttpCookie("cookie1", + "value1", + secure = true, + httpOnly = true, + maxAge = Some(123L))) { + setCookie(HttpCookie("cookie2", "value2")) { + setCookie( + HttpCookie("cookie3", + "", + domain = Some("xyz"), + path = Some("a/b/c"))) { + complete("ok") + } + } + } + } } private implicit val actorSystem: ActorSystem = ActorSystem("sttp-test") @@ -102,6 +130,7 @@ class BasicTests bodyTests() headerTests() errorsTests() + cookiesTests() def parseResponseTests(): Unit = { name should "parse response as string" in { @@ -197,5 +226,43 @@ class BasicTests resp.isClientError should be(true) } } + + def cookiesTests(): Unit = { + name should "read response cookies" in { + val wrappedResponse = + sttp.get(new URI(endpoint + "/set_cookies")).send(ignoreResponse) + val response = forceResponse.force(wrappedResponse) + response.cookies should have length (3) + response.cookies.toSet should be( + Set( + Cookie("cookie1", + "value1", + secure = true, + httpOnly = true, + maxAge = Some(123L)), + Cookie("cookie2", "value2"), + Cookie("cookie3", "", domain = Some("xyz"), path = Some("a/b/c")) + )) + } + + name should "read response cookies with the expires attribute" in { + val wrappedResponse = sttp + .get(new URI(endpoint + "/set_cookies/with_expires")) + .send(ignoreResponse) + val response = forceResponse.force(wrappedResponse) + response.cookies should have length (1) + val c = response.cookies(0) + + c.name should be("c") + c.value should be("v") + c.expires.map(_.toInstant.toEpochMilli) should be( + Some( + ZonedDateTime + .of(1997, 12, 8, 12, 49, 12, 0, ZoneId.of("GMT")) + .toInstant + .toEpochMilli + )) + } + } } } -- cgit v1.2.3