Testing API Requests
You have defined your API in swagger.yaml
, provided a detailed schema and wrote flows for API paths. How can we test the flows?
Let's say you have invented an incredibly useful API route that can tell whether a given number is odd.
Example
swagger.yaml
:
Flow in odd.xml
:
Tests
Now let's test that! We could write a sub-flow based test like in Testing Templates. But this time, we want to actually call our API to make sure that validation works, status codes are correct and so on.
The test-request
action does just that: You define the API path
that should be called and all necessary request
parameters.
The test-request
sets these variables so that you can easily inspect the result:
$status
the HTTP status code (number)$response
the response body (string)$headers
the response headers (array)
This is what we will use to assert that everything works as expected:
tests/test-even.xml
:
Note that we have used json-parse()
to check the odd
property.
tests/test-odd.xml
:
tests/test-zero.xml
:
Run the tests with flat test
:
📝 Exercise: Add a test for negative numbers, for numbers with fractional parts (and correct the schema accordingly) and that also verifies that the
see-also
header is not set.
See also
Testing Templates (cookbook)
Testing Upstream Requests (cookbook)
Test Framework (reference)
test-request
(reference)assert
(reference)
Last updated