In this post, we will have a look at some basic assertions to add an extra layer of confidence on your API.
Firstly, we need to install Upssert:
$ npm install upssert
The simplest assertion is a ping. We can run a ping assertion against the
$ upssert --url https://httpbin.org/get
And your output would be somewhere along these lines:
Executing 1 test suites (2 assertions)… Ping ✓ https://httpbin.org/get 1 passing (405.000ms)
We’re now pretty confident that httpbin is online! The
--url flag simply
generates a default test suite for you:
This asserts that the servers response code is either in the succes range or the redirection range.
But we don’t always want to just know that the status code is OK. As an example,
we might need to check that a URL which returns a 302 status code is actually
returning the correct
$ upssert redirect.json
Executing 1 test suites (3 assertions)… Httpbin.org ✓ /redirect 1 passing (411.000ms)
Here, we’ve asserted that the status code is 302, the Content-Type header is HTML,
and that there exists a Location header with the value of
An interesting feature: if the content type is
application/json, Upssert will
parse the body (thank you
JSON.parse!), and allow assertions against the
For this test case, we could use the TAP reporter:
$ upssert foobar.json --reporter tap 1..1 ok 1 /foobar # tests 1 # pass 1 # fail 0 # assertions 2
Hopefully I have succeeded in illustrating the ease at which you can create and run test cases against an API.
In the next post, I’ll discuss how a complex API workflow can be tested.