From 10620d432ff20d1bfed7cf184d8042acf95eb9d7 Mon Sep 17 00:00:00 2001 From: adamw Date: Mon, 29 Jan 2018 14:18:56 +0100 Subject: Renaming the new stub method --- .../scala/com/softwaremill/sttp/testing/SttpBackendStub.scala | 9 +++------ .../com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 6 +++--- docs/testing.rst | 11 +++++------ 3 files changed, 11 insertions(+), 15 deletions(-) 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 a603894..2206a8f 100644 --- a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala +++ b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala @@ -66,8 +66,8 @@ class SttpBackendStub[R[_], S] private ( override def send[T](request: Request[T, S]): R[Response[T]] = { Try(matchers.lift(request)) match { - case Success(Some(responseMonad)) => - tryAdjustResponseType(rm, request.response, wrapResponse(responseMonad)) + case Success(Some(response)) => + tryAdjustResponseType(rm, request.response, response.asInstanceOf[R[Response[T]]]) case Success(None) => fallback match { case None => @@ -86,9 +86,6 @@ class SttpBackendStub[R[_], S] private ( private def wrapResponse[T](r: Response[_]): R[Response[T]] = rm.unit(r.asInstanceOf[Response[T]]) - private def wrapResponse[T](r: R[Response[_]]): R[Response[T]] = - rm.map(r)(_.asInstanceOf[Response[T]]) - override def close(): Unit = {} override def responseMonad: MonadError[R] = rm @@ -113,7 +110,7 @@ class SttpBackendStub[R[_], S] private ( } new SttpBackendStub(rm, matchers.orElse(m), fallback) } - def thenRespondWithMonad(resp: => R[Response[_]]): SttpBackendStub[R, S] = { + def thenRespondWrapped(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 8a4eeb2..92c62f7 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -3,11 +3,11 @@ package com.softwaremill.sttp.testing import java.io.ByteArrayInputStream import java.util.concurrent.TimeoutException -import scala.concurrent.ExecutionContext.Implicits.global import com.softwaremill.sttp._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.{FlatSpec, Matchers} +import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { @@ -29,7 +29,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { Response(Right("Ada"), 200, "OK", Nil, Nil) }) .whenRequestMatches(_.uri.port.exists(_ == 8080)) - .thenRespondWithMonad(Response(Right("OK from monad"), 200, "OK", Nil, Nil)) + .thenRespondWrapped(Response(Right("OK from monad"), 200, "OK", Nil, Nil)) "backend stub" should "use the first rule if it matches" in { implicit val b = testingStub @@ -172,7 +172,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { val before = System.currentTimeMillis() implicit val s = SttpBackendStub(new FutureMonad()).whenAnyRequest - .thenRespondWithMonad(Future { + .thenRespondWrapped(Future { Thread.sleep(LongTimeMillis) Response(Right("OK"), 200, "", Nil, Nil) }) diff --git a/docs/testing.rst b/docs/testing.rst index ead722b..9afcd0e 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -49,17 +49,16 @@ It is also possible to match requests by partial function, returning a response. This approach to testing has one caveat: the responses are not type-safe. That is, the stub backend cannot match on or verify that the type of the response body matches the response body type requested. -Another way to specify the behaviour is passing a response monad to Stub. It is useful if you need to test scenario with slow server, when response should be not returned immediately but after some time. -Example with Futures: :: +Another way to specify the behaviour is passing response wrapped in the result monad to the stub. It is useful if you need to test a scenario with a slow server, when the response should be not returned immediately, but after some time. Example with Futures: :: - implicit val testingBackend = SttpBackendStub(new FutureMonad()).whenAnyRequest - .thenRespondWithMonad(Future { + implicit val testingBackend = SttpBackendStub.asynchronousFuture.whenAnyRequest + .thenRespondWrapped(Future { Thread.sleep(5000) Response(Right("OK"), 200, "", Nil, Nil) }) - val respFuture = sttp.get(uri"http://example.org").send() - // responseFuture will complete after 10 seconds with "OK" response + val responseFuture = sttp.get(uri"http://example.org").send() + // responseFuture will complete after 5 seconds with "OK" response Simulating exceptions --------------------- -- cgit v1.2.3