diff options
author | Paweł Stawicki <pawelstawicki@gmail.com> | 2018-03-27 16:42:54 +0200 |
---|---|---|
committer | Paweł Stawicki <pawelstawicki@gmail.com> | 2018-03-27 16:42:54 +0200 |
commit | 09e2a6f1e998bc7f5f0cd2a27dd8c10a37df4602 (patch) | |
tree | bd4e4f30e743ecdc873d7614a74c9329be6b5e99 /core | |
parent | 9081387e3f2134c6cf3e321f64b833f7a0b911a8 (diff) | |
download | sttp-09e2a6f1e998bc7f5f0cd2a27dd8c10a37df4602.tar.gz sttp-09e2a6f1e998bc7f5f0cd2a27dd8c10a37df4602.tar.bz2 sttp-09e2a6f1e998bc7f5f0cd2a27dd8c10a37df4602.zip |
Added overloaded thenRespondWrapped which can return response monad but takes request as parameterstub-response-from-request
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala | 6 | ||||
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 10 |
2 files changed, 16 insertions, 0 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 d7d1034..d6119a9 100644 --- a/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala +++ b/core/src/main/scala/com/softwaremill/sttp/testing/SttpBackendStub.scala @@ -107,6 +107,12 @@ class SttpBackendStub[R[_], S] private (rm: MonadError[R], } new SttpBackendStub(rm, matchers.orElse(m), fallback) } + def thenRespondWrapped(resp: Request[_, _] => R[Response[_]]): SttpBackendStub[R, S] = { + val m: PartialFunction[Request[_, _], R[Response[_]]] = { + case r if p(r) => resp(r) + } + new SttpBackendStub(rm, matchers.orElse(m), fallback) + } } } 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 1299cae..772757e 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -26,6 +26,8 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { }) .whenRequestMatches(_.uri.port.exists(_ == 8080)) .thenRespondWrapped(Response(Right("OK from monad"), 200, "OK", Nil, Nil)) + .whenRequestMatches(_.uri.port.exists(_ == 8081)) + .thenRespondWrapped(r => Response(Right(s"OK from request. Request was sent to host: ${r.uri.host}"), 200, "OK", Nil, Nil)) "backend stub" should "use the first rule if it matches" in { implicit val b = testingStub @@ -58,6 +60,14 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { r.body.right.get should be("OK from monad") } + it should "respond with monad with response created from request" in { + implicit val b = testingStub + val r = sttp.post(uri"http://example.org:8081").send() + r.is200 should be(true) + r.body should be('right) + r.body.right.get should be("OK from request. Request was sent to host: example.org") + } + it should "use the default response if no rule matches" in { implicit val b = testingStub val r = sttp.put(uri"http://example.org/d").send() |