From 2cd4df4de975c45d566bc1304abc699b80023533 Mon Sep 17 00:00:00 2001 From: Paweł Stawicki Date: Sun, 28 Jan 2018 09:42:58 +0100 Subject: Added back the thenRespond(resp: => Response) method --- .../scala/com/softwaremill/sttp/testing/SttpBackendStub.scala | 11 ++++++++--- .../com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala index 9e52505..a603894 100644 --- a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala +++ b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala @@ -103,11 +103,16 @@ class SttpBackendStub[R[_], S] private ( def thenRespondWithCode(code: Int, msg: String = ""): SttpBackendStub[R, S] = { val body = if (code >= 200 && code < 300) Right(msg) else Left(msg) - thenRespondWithMonad(rm.unit(Response(body, code, msg, Nil, Nil))) + thenRespond(Response(body, code, msg, Nil, Nil)) } def thenRespond[T](body: T): SttpBackendStub[R, S] = - thenRespondWithMonad( - rm.unit(Response[T](Right(body), 200, "OK", Nil, Nil))) + thenRespond(Response[T](Right(body), 200, "OK", Nil, Nil)) + def thenRespond[T](resp: => Response[T]): SttpBackendStub[R, S] = { + val m: PartialFunction[Request[_, _], R[Response[_]]] = { + case r if p(r) => rm.unit(resp) + } + new SttpBackendStub(rm, matchers.orElse(m), fallback) + } def thenRespondWithMonad(resp: => R[Response[_]]): SttpBackendStub[R, S] = { val m: PartialFunction[Request[_, _], R[Response[_]]] = { case r if p(r) => resp diff --git a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala index fbf7a0b..9a435b2 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -79,7 +79,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { it should "handle exceptions thrown instead of a response (synchronous)" in { implicit val s = SttpBackendStub(HttpURLConnectionBackend()) .whenRequestMatches(_ => true) - .thenRespondWithMonad(throw new TimeoutException()) + .thenRespond(throw new TimeoutException()) a[TimeoutException] should be thrownBy { sttp.get(uri"http://example.org").send() @@ -89,7 +89,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { it should "handle exceptions thrown instead of a response (asynchronous)" in { implicit val s = SttpBackendStub(new FutureMonad()) .whenRequestMatches(_ => true) - .thenRespondWithMonad(throw new TimeoutException()) + .thenRespond(throw new TimeoutException()) val result = sttp.get(uri"http://example.org").send() result.failed.futureValue shouldBe a[TimeoutException] -- cgit v1.2.3