aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Stawicki <pawelstawicki@gmail.com>2017-10-25 10:48:28 +0200
committerPaweł Stawicki <pawelstawicki@gmail.com>2017-10-25 10:48:28 +0200
commit8f2cc79bfdb6bbea0950f69c47218b434449e4cd (patch)
tree8cbe6d07af17aa24f78cd7d3e9bae86e35cf1b87
parent6aa97fad45999ee95fd42a3c726106b2031bce33 (diff)
downloadsttp-stub-match-partial.tar.gz
sttp-stub-match-partial.tar.bz2
sttp-stub-match-partial.zip
Documentation for test stub matching requests to responses with partial functionstub-match-partial
-rw-r--r--docs/backends/testing.rst14
-rw-r--r--docs/credits.rst2
2 files changed, 15 insertions, 1 deletions
diff --git a/docs/backends/testing.rst b/docs/backends/testing.rst
index c0cfc6f..dbf01aa 100644
--- a/docs/backends/testing.rst
+++ b/docs/backends/testing.rst
@@ -19,6 +19,20 @@ For example::
val response2 = sttp.post(uri"http://example.org/d/e").send()
// response2.code will be 500
+It is also possible to match request by partial function, returning a response. E.g.:
+
+ implicit val testingBackend = SttpBackendStub(HttpURLConnectionBackend())
+ .whenRequestMatchesPartial({
+ case r if r.uri.path.endsWith(List("partial10")) => Response(Right(10), 200, Nil, Nil)
+ case r if r.uri.path.endsWith(List("partialAda")) => Response(Right("Ada"), 200, Nil, Nil)
+ })
+
+ val response1 = sttp.get(uri"http://example.org/partial10").send()
+ // response1.body will be Right(10)
+
+ val response2 = sttp.post(uri"http://example.org/partialAda").send()
+ // response2.body will be Right("Ada")
+
However, this approach has one caveat: the responses are not type-safe. That is, the backend cannot match on or verify that the type included in the response matches the response type requested.
It is also possible to create a stub backend which delegates calls to another (possibly "real") backend if none of the specified predicates match a request. This can be useful during development, to partially stub a yet incomplete API with which we integrate::
diff --git a/docs/credits.rst b/docs/credits.rst
index 4826afe..29d733c 100644
--- a/docs/credits.rst
+++ b/docs/credits.rst
@@ -8,4 +8,4 @@ Credits
* `Piotr Buda <https://github.com/pbuda>`_
* `Piotr Gabara <https://github.com/bhop>`_
* `Gabriele Petronella <https://github.com/gabro>`_
-
+* `Paweł Stawicki <https://github.com/amorfis>`_