Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A quick sample of how to use api-easy with `npm test`
/*
* 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);
};
{
"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" }
}
/*
* 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);
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
@silverbucket

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.

@CodingFu

helpers.js:14

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

@CodingFu

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.