public
Last active

A quick sample of how to use api-easy with `npm test`

  • Download Gist
helpers.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
* helpers.js: Test macros for APIeasy.
*
* (C) 2011, Charlie Robbins
*
*/
var assert = require('assert'),
http = require('http'),
journey = require('journey');
var helpers = exports;
 
helpers.startServer = function (port) {
var token, router = new journey.Router({
strict: false,
strictUrls: false,
api: 'basic'
});
function isAuthorized (req, body, next) {
return parseInt(req.headers['x-test-authorized'], 10) === token ? next() : next(new journey.NotAuthorized());
}
router.get('/tests').bind(function (res) {
res.send(200, {}, { ok: true });
});
router.post('/tests').bind(function (res, data) {
res.send(200, {}, data);
});
router.post('/redirect').bind(function (res, data) {
res.send(302, { 'Location': 'http://localhost:8000/login' }, data);
});
router.get('/login').bind(function (res) {
if (!token) {
token = Math.floor(Math.random() * 100);
}
res.send(200, {}, { token: token });
});
router.filter(isAuthorized, function () {
this.get('/restricted').bind(function (res) {
res.send(200, {}, { authorized: true });
});
});
http.createServer(function (request, response) {
var body = "";
 
request.addListener('data', function (chunk) { body += chunk });
request.addListener('end', function () {
//
// Dispatch the request to the router
//
router.handle(request, body, function (result) {
response.writeHead(result.status, result.headers);
response.end(result.body);
});
});
}).listen(8000);
};
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
"name": "api-easy-tests",
"description": "A quick sample of how to use api-easy with `npm test`",
"version": "0.1.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
"url": "http://github.com/indexzero/api-easy.git"
},
"dependencies": {
"api-easy": "0.2.x"
},
"devDependencies": {
"journey": ">= 0.4.0-pre-2"
},
"scripts": { "test": "vows run-test.js --spec" },
"engines": { "node": ">= 0.4.3" }
}
run-test.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
* core-test.js: Tests for core functionality of APIeasy.
*
* (C) 2011, Charlie Robbins
*
*/
 
var vows = require('vows'),
assert = require('assert'),
APIeasy = require('api-easy'),
helpers = require('./helpers');
 
var scopes = ['When using the Test API', 'the Test Resource'];
 
helpers.startServer(8000);
 
var suite = APIeasy.describe('api/test');
 
scopes.forEach(function (text) {
suite.discuss(text);
});
 
suite.use('localhost', 8000)
.setHeader('Content-Type', 'application/json')
.followRedirect(false)
.get('/tests')
.expect(200, { ok: true })
.post('/tests', { dynamic: true })
.expect(200, { dynamic: true })
.post('/redirect', { dynamic: true })
.expect(302, { dynamic: true })
.get('/login')
.expect(200)
.expect('should respond with the authorize token', function (err, res, body) {
var result = JSON.parse(body);
assert.isNotNull(result.token);
 
suite.before('setAuth', function (outgoing) {
outgoing.headers['x-test-authorized'] = result.token;
return outgoing;
});
})
.next()
.get('/restricted')
.expect(200, { authorized: true })
.export(module);
usage.sh
Shell
1 2 3 4 5
curl -o api-easy-tests.tar.gz https://gist.github.com/gists/1039425/download
tar -xvf api-easy-tests.tar.gz
cd gist1039425*
npm install
npm test

I've been unable to get this to work, when I use the usage.sh I get an error about cannot find module 'vows', then if i install that module I get an error about not finding 'helpers' but I'm not sure what to do in that case.

helpers.js:14

There is a little error. You pass port, but start server on hardcoded 8000.

@silverbucket
npm install vows not just globally, but also in the directory of the gist. did work for me.

P.S. api-easy is awesome. will use it in my project. (Small geotracking API)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.