Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/9402232 to your computer and use it in GitHub Desktop.
Save twolfson/9402232 to your computer and use it in GitHub Desktop.
Example gist for fixed-server

To get example working, run the following:

git clone https://gist.github.com/9402232.git gist-fixed-server-example
cd gist-fixed-server-example
npm install
npm test
{
"name": "gist-fixed-server-example",
"version": "0.1.0",
"description": "Example gist for fixed-server",
"scripts": {
"test": "mocha test.js"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/9402232.git"
},
"author": "Todd Wolfson <todd@twolfson.com>",
"license": "MIT",
"homepage": "https://gist.github.com/9402232",
"dependencies": {
"chai": "~1.9.0",
"request-mocha": "~0.2.0",
"http-proxy": "~0.10.4",
"request": "~2.34.0",
"fixed-server": "~0.3.0"
},
"private": true
}
// Load in test dependencies
var expect = require('chai').expect;
var FixedServer = require('fixed-server');
var httpProxy = require('http-proxy');
var httpUtils = require('request-mocha')(require('request'));
// Create a FixedApi factory
// DEV: For logical continuity, we use keys which represent the route
var FixedApi = new FixedServer({port: 1338});
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 () {
// Create temporary proxy server
before(function startProxyServer () {
this.proxyServer = httpProxy.createServer(1338, 'localhost');
this.proxyServer.listen(1337);
});
after(function stopProxyServer (done) {
this.proxyServer.close(done);
});
describe('with an operating backend', function () {
// Launch a FixedServer hosting a 200 route
FixedApi.run(['GET 200 /']);
httpUtils.save('http://localhost:1337/');
// Make assertions about request
it('receives a 200 response', function () {
expect(this.res).to.have.property('statusCode', 200);
});
});
describe('with an non-functional backend', function () {
// Launch a FixedServer hosting a 500 route
FixedApi.run(['GET 500 /']);
httpUtils.save('http://localhost:1337/');
// Make assertions about request
it('receives a 500 response', function () {
expect(this.res).to.have.property('statusCode', 500);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment