aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala/com
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-10-09 12:01:33 +0200
committeradamw <adam@warski.org>2017-10-09 12:01:33 +0200
commitbb4f2d27fa56ced1f565d9326a08fd5a87e2202b (patch)
treebc5c72029bb4757bcb0f92ca02a3e6820c81cec9 /core/src/test/scala/com
parentc426790198c32dd7ee181c46cf1a9be96978397d (diff)
downloadsttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.tar.gz
sttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.tar.bz2
sttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.zip
Testing backend
Diffstat (limited to 'core/src/test/scala/com')
-rw-r--r--core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala51
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)
+ }
+}