Create a gist now

Instantly share code, notes, and snippets.

node_modules/
// Load in dependencies
var expect = require('chai').expect;
var FixedServer = require('fixed-server');
var httpUtils = require('request-mocha')(require('request'));
// Create a test server factory
// DEV: For logical consistency, we use keys which represent the route
var FixedApi = new FixedServer({port: 1337});
FixedApi.addFixtures({
'GET 200 /': {
method: 'get',
route: '/',
response: function (req, res) {
res.send('Hello World');
}
},
'GET 500 /': {
method: 'get',
route: '/',
response: function (req, res) {
res.status(500).send('An error has occurred');
}
}
});
// Start tests
describe('A request to a proxy server', function () {
describe('with an operating backend', function () {
// Launch a FixedApi hosting a 200 route
FixedApi.run(['GET 200 /']);
httpUtils.save('http://localhost:1337/');
// Verify against our 200 route
it('receives a 200 response', function () {
expect(this.res).to.have.property('statusCode', 200);
});
});
describe('with an non-functional backend', function () {
// Launch a FixedApi hosting a 500 route
FixedApi.run(['GET 500 /']);
httpUtils.save('http://localhost:1337/');
// Verify against our 500 route
it('receives a 500 response', function () {
expect(this.res).to.have.property('statusCode', 500);
});
});
});
{
"name": "gist-eight-track-article-2",
"version": "1.0.0",
"description": "Example from http://twolfson.com/2015-01-11-testing-with-other-services",
"main": "index.js",
"author": "Todd Wolfson <todd@twolfson.com> (http://twolfson.com/)",
"license": "UNLICENSE",
"scripts": {
"test": "mocha index.js"
},
"dependencies": {
"chai": "~1.10.0",
"fixed-server": "~0.4.0",
"mocha": "~2.1.0",
"request": "~2.51.0",
"request-mocha": "~0.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment