REST API testing made easy

Quick API endpoint testing using several libraries.

REST API unit testing

Imagine you created a REST API and deployed it somewhere. Do you have a sanity check that can quickly verify that the main parts are configured and respond correctly? I am not talking about the exhaustive low level unit testing, just a quick run through the main parts: login, create a new item, find it.

I hate boilerplate code and always try to find a module that encapsulates irrelevant details, but leaves the features configurable. There are two packages that I used that make REST API testing a breeze: api-easy for vows and frisby for jasmine.

note: when comparing actual value to expected all libraries use deep equality.

Test example

Verify that GET `/api/v0/tasks' returns an array containing string foo, and GET '/api/v0/tasks/id' returns a specific task.

Quick API testing using Jasmine and Frisby

frisby provides chainable interface allowing to query API, check returned types and objects. Tests for the above example written using frisby:

var frisby = require('frisby');
var url = 'http://localhost:3030/api/v0';

frisby.create('get all tasks')
    .get(url + '/tasks')
    .expectJSON(['foo'])
    .toss();

frisby.create('get one task')
    .get(url + '/tasks/4444')
    .expectJSON({name: 't'})
    .toss();

To run the test

npm install -g jasmine-node
jasmine-node frisby.spec.js
// --autotest keeps a watch on your test files

Quick API testing using Vows and Api-Easy

vows is excellent BDD testing framework that works very well with asynchronous code, making it my first choice for API testing. There is even a nifty little testing NPM module api-easy that abstracts all the boilerplate code.

var APIeasy = require('api-easy');
var assert = require('assert');

var suite = APIeasy.describe('simple-example');

suite.discuss('When using simple-example api')
    .use('localhost', 3030)
    .setHeader('Content-Type', 'application/json')
    .get('/api/v0/tasks')
    .expect(200, ['foo']);

suite.undiscuss()
    .discuss('single task')
    .get('/api/v0/tasks/4444')
    .expect(200, {name: 't'});

suite.export(module);

You can install vows globally npm install -g vows and run the tests using vows test-file.js. You can also keep vows running and automatically rerun tests when test file changes vows -w test-file.js.

Conclusion

The choice between frisby and api-easy depends on the testing framework already in place. Both are equivalent, and they do hide a lot of boilerplate. So much, that converting unit tests into coffeescript does not remove a significant amount of code.