Testing with Backend Requests
Usually, our flows contain upstream requests to one or more backends that we use to provide the client response. Sometimes upstream requests use results of other upstream requests. Testing such flows can be a challenge.
In this example flow, we use the helpful tool httpbin.org to reflect a parameter.
The Swagger snippet is
and the flow in dashboard.xml
:
We use the client's count=
parameter (or 1
if missing) to send it as noti=
to httpbin.org
.
A response for that upstream request could look like this:
The parameter is reflected back in the JSON responses args
property. That is what we use in the response template, where it is finally called notifications
.
Mocking the Backend
A naïve approach to writing a flat test would be a test-request
with some assertions.
tests/test-notifications.xml
:
This actually works. However, our flow in fact sends the upstream request to httpbin.org
. The test will fail if their server is down. And if we were mocking a single-sign-on service, we would need real credentials. That's not how we like our automated tests to work.
A backend-flow
test action allows us to create a fake response for the upstream requests. We can use regular actions to set headers, status code, response bodies and so on.
We extend our flat-test
with this backend-flow
call as its first action:
Note that the backend-flow
action only registers that flow for subsequent request
actions. That's why we have to put it first.
You can register multiple backend flows for your requests. Upstream requests and flows are matched using their request
ID. In our case, notifications
.
💡If your mock response is a bit longer than ours here, you can store it in a separate file and use copy
instead:
Asserting Upstream Requests
Injecting a mocked response into your flow only allows to test one direction: downstream. How do we test that our flow has sent the right parameters to the upstream server?
You can use assert
in backend-flow
, too:
This test will fail if we don't pass the count
parameter as expected.
For this assertion we have made use of the pre-defined variable $request
. You can use $body
to inspect the upstream request body. These variables are overridden in backend flows and restored afterwards. All other global variables are shared between flow, backend flow and test flow! This allows for dynamic backend mocks and more assertion possibilities.
See also
Testing API Requests (cookbook)
Testing Templates (cookbook)
Test Framework (reference)
assert
(reference)backend-flow
(reference)test-request
(reference)
Last updated