aboutsummaryrefslogtreecommitdiff
path: root/README.md
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 /README.md
parentc426790198c32dd7ee181c46cf1a9be96978397d (diff)
downloadsttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.tar.gz
sttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.tar.bz2
sttp-bb4f2d27fa56ced1f565d9326a08fd5a87e2202b.zip
Testing backend
Diffstat (limited to 'README.md')
-rw-r--r--README.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/README.md b/README.md
index f795e53..ae61a44 100644
--- a/README.md
+++ b/README.md
@@ -530,6 +530,36 @@ parameter. See [akka-http docs](http://doc.akka.io/docs/akka-http/current/scala/
* OkHttp: create a custom client modifying the SSL settings as described
[on the wiki](https://github.com/square/okhttp/wiki/HTTPS)
+## Testing
+
+If you need a stub backend for use in tests instead of a "real" backend (you
+probably don't want to make HTTP calls during unit tests), you can use the
+`SttpBackendStub` class. It allows specifying how the backend should respond
+to requests matching given predicates.
+
+A backend stub can be created using an instance of a "real" backend, or by
+explicitly giving the response wrapper monad and supported streams type.
+
+For example:
+
+```scala
+implicit val testingBackend = SttpBackendStub(HttpURLConnectionBackend())
+ .whenRequestMatches(_.uri.path.startsWith(List("a", "b")))
+ .thenRespond("Hello there!")
+ .whenRequestMatches(_.method == Method.POST)
+ .thenRespondServerError()
+
+val response1 = sttp.get(uri"http://example.org/a/b/c").send()
+// response1.body will be Right("Hello there")
+
+val response2 = sttp.post(uri"http://example.org/d/e").send()
+// response2.code will be 500
+```
+
+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.
+
## Notes
* the encoding for `String`s defaults to `utf-8`.