diff options
author | Paweł Stawicki <pawelstawicki@gmail.com> | 2017-10-25 09:57:34 +0200 |
---|---|---|
committer | Paweł Stawicki <pawelstawicki@gmail.com> | 2017-10-25 09:57:34 +0200 |
commit | 4d010738c7b39b6a3e0844c6831283842d3f220d (patch) | |
tree | 01604e538e0e1664b3fccbfe4b324870578669b0 /core/src/test/scala | |
parent | 06bd5c95d04dd57e1b6c2572b94336b8fdb68bfa (diff) | |
download | sttp-4d010738c7b39b6a3e0844c6831283842d3f220d.tar.gz sttp-4d010738c7b39b6a3e0844c6831283842d3f220d.tar.bz2 sttp-4d010738c7b39b6a3e0844c6831283842d3f220d.zip |
Test stub matching partial function mapping request to response
Diffstat (limited to 'core/src/test/scala')
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 17 |
1 files changed, 16 insertions, 1 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 index b80ae76..d10d1b6 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -12,6 +12,10 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { .thenRespond(10) .whenRequestMatches(_.method == Method.GET) .thenRespondServerError() + .whenRequestMatchesPartial({ + case r if r.method == Method.POST && r.uri.path.endsWith(List("partial10")) => Response(Right(10), 200, Nil, Nil) + case r if r.method == Method.POST && r.uri.path.endsWith(List("partialAda")) => Response(Right("Ada"), 200, Nil, Nil) + }) "backend stub" should "use the first rule if it matches" in { implicit val b = testingStub @@ -38,7 +42,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { 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() + val r = sttp.put(uri"http://example.org/d").send() r.code should be(404) } @@ -49,6 +53,17 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { r.futureValue.code should be(404) } + it should "use rules in partial function" in { + implicit val s = testingStub + val r = sttp.post(uri"http://example.org/partial10").send() + r.is200 should be(true) + r.body should be(Right(10)) + + val ada = sttp.post(uri"http://example.org/partialAda").send() + ada.is200 should be(true) + ada.body should be(Right("Ada")) + } + val testingStubWithFallback = SttpBackendStub .withFallback(testingStub) .whenRequestMatches(_.uri.path.startsWith(List("c"))) |