diff options
author | adamw <adam@warski.org> | 2017-07-15 18:40:50 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-15 18:40:50 +0200 |
commit | b053c91551b924100fa8261e4f405bc40ce53147 (patch) | |
tree | fc79e2f9152fa0f9e4e1d4d50d4a00e69445fe3d /tests | |
parent | a3b03abe52b383488a3d8349d5b851d80050c4f2 (diff) | |
download | sttp-b053c91551b924100fa8261e4f405bc40ce53147.tar.gz sttp-b053c91551b924100fa8261e4f405bc40ce53147.tar.bz2 sttp-b053c91551b924100fa8261e4f405bc40ce53147.zip |
Basic auth
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala index 28a5ac0..1ad5fc0 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/BasicTests.scala @@ -10,6 +10,7 @@ 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._ +import akka.http.scaladsl.server.directives.Credentials import com.softwaremill.sttp.akkahttp.AkkaHttpSttpHandler import com.typesafe.scalalogging.StrictLogging import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures} @@ -83,6 +84,15 @@ class BasicTests } } } + } ~ path("secure_basic") { + authenticateBasic("test realm", { + case c @ Credentials.Provided(un) + if un == "adam" && c.verify("1234") => + Some(un) + case _ => None + }) { userName => + complete(s"Hello, $userName!") + } } private implicit val actorSystem: ActorSystem = ActorSystem("sttp-test") @@ -130,6 +140,7 @@ class BasicTests headerTests() errorsTests() cookiesTests() + authTests() def parseResponseTests(): Unit = { name should "parse response as string" in { @@ -263,5 +274,24 @@ class BasicTests )) } } + + def authTests(): Unit = { + val secureBasic = sttp.get(uri"$endpoint/secure_basic") + + name should "return a 401 when authorization fails" in { + val req = secureBasic + val resp = forceResponse.force(req.send(responseAsString)) + resp.code should be(401) + resp.header("WWW-Authenticate") should be( + Some("""Basic realm="test realm",charset=UTF-8""")) + } + + name should "perform basic authorization" in { + val req = secureBasic.auth.basic("adam", "1234") + val resp = forceResponse.force(req.send(responseAsString)) + resp.code should be(200) + resp.body should be("Hello, adam!") + } + } } } |