Skip to content

Commit

Permalink
Renaming the new stub method
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Jan 29, 2018
1 parent 88a3a82 commit 10620d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down
11 changes: 5 additions & 6 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------
Expand Down

0 comments on commit 10620d4

Please sign in to comment.