Example: RESTful API

victorjonsson edited this page Feb 7, 2013 · 6 revisions
Clone this wiki locally

Examples of tests for a RESTful API

config.json

{
  "host" : "stage.mycompany.com",
  "verbose" : false,
  "testdir" : "tests"
}

tests/test.djs

var dokimon = require('dokimon'),
    assert = require('assert');

module.exports = [

    /**
     * Create user
     */
    new dokimon.Test(
        'RegisterUser',
        {
            url: '/user/create',
            method: 'POST',
            write : {user: 'Wayne Gretzky', password:'abc1234', email : 'wayne@nhl.com'}
        },
        function(response, body) {
            assert.equal(response.statusCode, 201, 'Unexpected status code');
            // and perhaps some other assertions as well...
        },
        true
    ),

    /**
     * Load user data after registration
     */
    new dokimon.Test(
        'LoadUserTest',
        {
            url: '/user/Wayne+Gretzky',
            headers : {
                Authorization : 'Basic '+new Buffer('wayne@nhl.com:abc1234').toString('base64')
            },
            dependsOn : 'RegisterUser' // If running only LoadUserTest the test RegisterUser has to be called first
        },
        function(response, body) {
            assert.equal(response.statusCode, 200, 'Unexpected status code');
            var userData = JSON.parse(body);
            // assert that userData has expected properties
        }
    ),

    /**
     * Expect to fail...
     */
    new dokimon.Test(
        'FailLoadingUser',
        {
            url: '/user/Wayne+Gretzky',
            headers : {
                Authorization : 'Basic '+new Buffer('no-real-user@blaha.net:abc1234').toString('base64')
            }
        },
        function(response, body) {
            assert.equal(response.statusCode, 401, 'Not an invalid authentication? yeah right...');
        }
    )
];