From 016ddea1e7e7e6065f7cab3ae9a16dfedfebe2e7 Mon Sep 17 00:00:00 2001 From: Fehmi Can Saglam Date: Sun, 21 Oct 2018 13:16:58 +0200 Subject: [PATCH] Allow to set scheme in as keyword --- core/src/main/scala/requests.scala | 4 ++-- core/src/test/scala/basic.scala | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/requests.scala b/core/src/main/scala/requests.scala index 7091e22b..ce577274 100644 --- a/core/src/main/scala/requests.scala +++ b/core/src/main/scala/requests.scala @@ -271,8 +271,8 @@ trait ParamVerbs extends RequestVerbs { } trait AuthVerbs extends RequestVerbs { - def as(user: String, password: String): Req = - this.as(new Realm.Builder(user, password).build()) + def as(user: String, password: String, scheme: AuthScheme): Req = + this.as(new Realm.Builder(user, password).setScheme(scheme).build()) /** Basic auth, use with care. */ def as_!(user: String, password: String): Req = diff --git a/core/src/test/scala/basic.scala b/core/src/test/scala/basic.scala index dc84f49a..49b3054e 100644 --- a/core/src/test/scala/basic.scala +++ b/core/src/test/scala/basic.scala @@ -2,7 +2,10 @@ package dispatch.spec import java.nio.charset.Charset +import org.asynchttpclient.Realm +import org.asynchttpclient.Realm.AuthScheme import org.scalacheck._ +import org.scalacheck.Prop._ object BasicSpecification extends Properties("Basic") @@ -166,4 +169,19 @@ with DispatchCleanup { req.toRequest.getUrl ?= "http://127.0.0.1:%d/?%s".format(port, expectedParams.mkString("&")) } } + + property("Building a Realm without a scheme should throw NPE") = { + forAll(Gen.zip(Gen.alphaNumStr, Gen.alphaNumStr)) { case (user, password) => + throws(classOf[NullPointerException])(new Realm.Builder(user, password).build()) + } + } + + property("Build a Realm using as") = { + forAll(Gen.zip(Gen.alphaNumStr, Gen.alphaNumStr, Gen.oneOf(AuthScheme.values()))) { case (user, password, scheme) => + val realm = localhost.as(user, password, scheme).toRequest.getRealm + realm.getScheme ?= scheme + realm.getPrincipal ?= user + realm.getPassword ?= password + } + } }