GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 3
  • Stars 5
  • Forks 1
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — A quick sample of how to use api-easy with `npm test`

Instantly share code, notes, and snippets.

@indexzero

indexzero/helpers.js

Created June 22, 2011 03:05
Show Gist options
  • Download ZIP
  • Star 6 (6) You must be signed in to star a gist
  • Fork 1 (1) You must be signed in to fork a gist
  • Clone this repository at <script src="https://gist.github.com/indexzero/1039425.js"></script>
  • Save indexzero/1039425 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/indexzero/1039425.js"></script>
Save indexzero/1039425 to your computer and use it in GitHub Desktop.
Download ZIP
A quick sample of how to use api-easy with `npm test`
Raw
helpers.js
/*
* 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);
};
Raw
package.json
{
"name": "api-easy-tests",
"description": "A quick sample of how to use api-easy with `npm test`",
"version": "0.1.0",
"author": "Charlie Robbins <[email protected]>",
"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" }
}
Raw
run-test.js
/*
* 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);
Raw
usage.sh
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

silverbucket commented Sep 26, 2012

Copy link
Copy Markdown

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CodingFu

CodingFu commented Oct 30, 2012

Copy link
Copy Markdown

helpers.js:14

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CodingFu

CodingFu commented Oct 30, 2012

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.