diff options
Diffstat (limited to 'core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala')
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala new file mode 100644 index 0000000..538dd35 --- /dev/null +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -0,0 +1,51 @@ +package com.softwaremill.sttp.testing + +import com.softwaremill.sttp._ +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.{FlatSpec, Matchers} + +class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { + val testingStub = SttpBackendStub(HttpURLConnectionBackend()) + .whenRequestMatches(_.uri.path.startsWith(List("a", "b"))) + .thenRespondOk() + .whenRequestMatches(_.uri.paramsMap.get("p").contains("v")) + .thenRespond(10) + .whenRequestMatches(_.method == Method.GET) + .thenRespondServerError() + + it should "use the first rule if it matches" in { + implicit val b = testingStub + val r = sttp.get(uri"http://example.org/a/b/c").send() + r.is200 should be(true) + r.body should be('left) + } + + it should "use subsequent rules if the first doesn't match" in { + implicit val b = testingStub + val r = sttp + .get(uri"http://example.org/d?p=v") + .response(asString.map(_.toInt)) + .send() + r.body should be(Right(10)) + } + + it should "use the first specified rule if multiple match" in { + implicit val b = testingStub + val r = sttp.get(uri"http://example.org/a/b/c?p=v").send() + r.is200 should be(true) + r.body should be('left) + } + + it should "use the default response if no rule matches" in { + implicit val b = testingStub + val r = sttp.post(uri"http://example.org/d").send() + r.code should be(404) + } + + it should "wrap responses in the desired monad" in { + import scala.concurrent.ExecutionContext.Implicits.global + implicit val b = SttpBackendStub(new FutureMonad()) + val r = sttp.post(uri"http://example.org").send() + r.futureValue.code should be(404) + } +} |