aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-11-13 15:39:31 +0100
committeradamw <adam@warski.org>2017-11-13 15:39:31 +0100
commitd7d26fbda8559adad2737c2df73076f002c194f9 (patch)
tree04ff4bfa0b9d0806c32f20033f6c8536cf47a260 /docs
parenta0fdfa6f6114aec289e46c4261522244b160e522 (diff)
downloadsttp-d7d26fbda8559adad2737c2df73076f002c194f9.tar.gz
sttp-d7d26fbda8559adad2737c2df73076f002c194f9.tar.bz2
sttp-d7d26fbda8559adad2737c2df73076f002c194f9.zip
#44: supporting type conversion in the backend stub
Diffstat (limited to 'docs')
-rw-r--r--docs/backends/testing.rst31
1 files changed, 31 insertions, 0 deletions
diff --git a/docs/backends/testing.rst b/docs/backends/testing.rst
index d1e99da..d082f40 100644
--- a/docs/backends/testing.rst
+++ b/docs/backends/testing.rst
@@ -44,6 +44,37 @@ If you want to simulate an exception being thrown by a backend, e.g. a socket ti
.whenRequestMatches(_ => true)
.thenRespond(throw new TimeoutException())
+Adjusting the response body type
+--------------------------------
+
+If the type of the response body returned by the stub's rules (as specified using the ``.when...`` methods) doesn't match what was specified in the request, the stub will attempt to convert the body to the desired type. This might be useful when:
+
+* testing code which maps a basic response body to a custom type, e.g. mapping a raw json string using a decoder to a domain type
+* reading a classpath resource (which results in an ``InputStream``) and requesting a response of e.g. type ``String``
+
+The following conversions are supported:
+
+* anything to ``()`` (unit), when the response is ignored
+* ``InputStream`` and ``Array[Byte]`` to ``String``
+* ``InputStream`` and ``String`` to ``Array[Byte]``
+* ``InputStream``, ``String`` and ``Array[Byte]`` to custom types through mapped response specifications
+
+For example::
+
+ implicit val testingBackend = SttpBackendStub(HttpURLConnectionBackend())
+ .whenRequestMatches(_ => true)
+ .thenRespond(""" {"username": "john", "age": 65 } """)
+
+ def parseUserJson(a: Array[Byte]): User = ...
+
+ val response = sttp.get(uri"http://example.com")
+ .response(asByteArray.map(parseUserJson))
+ .send()
+
+In the example above, the stub's rules specify that a response with a ``String``-body should be returned for any request; the request, on the other hand, specifies that responses should be parsed from a byte array to a custom ``User`` type. These type don't match, so the ``SttpBackendStub`` will in this case convert the body to the desired type.
+
+Note that no conversions will be attempted for streaming response bodies.
+
Delegating to another backend
-----------------------------