Create a gist now

Instantly share code, notes, and snippets.

data/
node_modules/
// Load in dependencies
var eightTrack = require('eight-track');
var expect = require('chai').expect;
var FixedServer = require('fixed-server');
var httpUtils = require('request-mocha')(require('request'));
// Create a test server factory with modifying endpoints
var githubEightTrack = eightTrack({
url: 'https://api.github.com',
fixtureDir: 'data/github-eight-track'
});
var FixedGithub = new FixedServer({port: 1337});
FixedGithub.addFixtures({
'GET 200 /repos/uber/eight-track': {
method: 'get',
route: '/repos/uber/eight-track',
// Playback directly with eight-track
response: githubEightTrack
},
'GET 200 /repos/uber/eight-track#no-watchers': {
method: 'get',
route: '/repos/uber/eight-track',
response: function (localReq, localRes) {
// Forward request via eight-track
githubEightTrack.forwardRequest(localReq, handleResponse);
function handleResponse(err, externalRes, externalBody) {
// If there was an error, throw it
if (err) {
throw err;
}
// Otherwise, extract the JSON and modify the watchers
var repoInfo = JSON.parse(externalBody);
repoInfo.watchers = 0;
localRes.send(repoInfo);
}
}
}
// We can add the 500 response from the previous example
// for entirely fake data
});
// Start our tests
describe('A GitHub repo', function () {
describe('is very popular', function () {
// Launch a FixedServer with real data
FixedGithub.run(['GET 200 /repos/uber/eight-track']);
httpUtils.save({
url: 'http://localhost:1337/repos/uber/eight-track',
headers: {
'User-Agent': 'node-request'
}
});
// Make assertions about request
it('has some stargazers', function () {
expect(this.err).to.equal(null);
expect(this.res.statusCode).to.equal(200);
expect(JSON.parse(this.body).watchers).to.be.at.least(1);
});
});
describe('is very young', function () {
// Launch a FixedServer hosting with a modified response
FixedGithub.run(['GET 200 /repos/uber/eight-track#no-watchers']);
httpUtils.save({
url: 'http://localhost:1337/repos/uber/eight-track',
headers: {
'User-Agent': 'node-request'
}
});
// Make assertions about request
it('has no watchers', function () {
expect(this.err).to.equal(null);
expect(this.res.statusCode).to.equal(200);
expect(JSON.parse(this.body).watchers).to.equal(0);
});
});
});
{
"name": "gist-eight-track-article-3",
"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",
"eight-track": "~2.1.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