Skip to content

Commit

Permalink
Added noRecord option to error on unknown requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ijpiantanida committed May 20, 2016
1 parent bc7a2eb commit 1f21800
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 82 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -32,6 +32,7 @@ var handler = yakbak('http://api.flickr.com', {
#### options

- `dirname` the path where recorded responses will be written (required).
- `noRecord` if true, requests will return a 404 error if the tape doesn't exist

### with node's http module

Expand Down
Empty file modified cli.js 100644 → 100755
Empty file.
25 changes: 21 additions & 4 deletions index.js
Expand Up @@ -16,6 +16,7 @@ var debug = require('debug')('yakbak:server');
* @param {String} host The hostname to proxy to
* @param {Object} opts
* @param {String} opts.dirname The tapes directory
* @param {Boolean} opts.noRecord if true, requests will return a 404 error if the tape doesn't exist
* @returns {Function}
*/

Expand All @@ -33,15 +34,21 @@ module.exports = function (host, opts) {
return Promise.try(function () {
return require.resolve(file);
}).catch(ModuleNotFoundError, function (/* err */) {
return proxy(req, body, host).then(function (res) {
return record(res.req, res, file);
});
if(!opts.noRecord) {
return proxy(req, body, host).then(function (res) {
return record(res.req, res, file);
});
} else {
throw {code: 'RECORDING_DISABLED'};
}
});

}).then(function (file) {
return require(file);
}).then(function (tape) {
return tape(req, res);
}).catch(RecordingDisabledError, function (/* err */) {
res.statusCode = 404;
res.end("An HTTP request has been made that yakbak does not know how to handle (" + req.url + ")");
});

};
Expand All @@ -68,3 +75,13 @@ function tapename(req, body) {
function ModuleNotFoundError(err) {
return err.code === 'MODULE_NOT_FOUND';
}

/**
* Bluebird error predicate for matching recording disabled errors.
* @param {Error} err
* @returns {Boolean}
*/

function RecordingDisabledError(err) {
return err.code === 'RECORDING_DISABLED';
}
177 changes: 99 additions & 78 deletions test/yakbak.js
Expand Up @@ -10,7 +10,7 @@ var request = require('supertest');
var assert = require('assert');
var fs = require('fs');

describe('record', function () {
describe('yakbak', function () {
var server, tmpdir, yakbak;

beforeEach(function (done) {
Expand All @@ -29,91 +29,112 @@ describe('record', function () {
tmpdir.teardown(done);
});

beforeEach(function () {
yakbak = subject(server.host, { dirname: tmpdir.dirname });
});

it('proxies the request to the server', function (done) {
request(yakbak)
.get('/record/1')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '1a574e91da6cf00ac18bc97abaed139e')
.expect('Content-Type', 'text/html')
.expect(201, 'OK')
.end(function (err) {
assert.ifError(err);
assert.equal(server.requests.length, 1);
done();
describe('record', function () {
describe("when recording is enabled", function() {
beforeEach(function () {
yakbak = subject(server.host, { dirname: tmpdir.dirname });
});

it('proxies the request to the server', function (done) {
request(yakbak)
.get('/record/1')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '1a574e91da6cf00ac18bc97abaed139e')
.expect('Content-Type', 'text/html')
.expect(201, 'OK')
.end(function (err) {
assert.ifError(err);
assert.equal(server.requests.length, 1);
done();
});
});

it('writes the tape to disk', function (done) {
request(yakbak)
.get('/record/2')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '3234ee470c8605a1837e08f218494326')
.expect('Content-Type', 'text/html')
.expect(201, 'OK')
.end(function (err) {
assert.ifError(err);
assert(fs.existsSync(tmpdir.join('3234ee470c8605a1837e08f218494326.js')));
done();
});
});
});
});

it('writes the tape to disk', function (done) {
request(yakbak)
.get('/record/2')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '3234ee470c8605a1837e08f218494326')
.expect('Content-Type', 'text/html')
.expect(201, 'OK')
.end(function (err) {
assert.ifError(err);
assert(fs.existsSync(tmpdir.join('3234ee470c8605a1837e08f218494326.js')));
done();
describe("when recording is not enabled", function() {
beforeEach(function () {
yakbak = subject(server.host, { dirname: tmpdir.dirname, noRecord: true });
});

it('returns a 404 error', function (done) {
request(yakbak)
.get('/record/2')
.set('host', 'localhost:3001')
.expect(404, "An HTTP request has been made that yakbak does not know how to handle (/record/2)")
.end(done);
});

it('does not make a request to the server', function (done) {
request(yakbak)
.get('/record/2')
.set('host', 'localhost:3001')
.end(function (err) {
assert.ifError(err);
assert.equal(server.requests.length, 0);
done();
});
});

it('does not write the tape to disk', function (done) {
request(yakbak)
.get('/record/2')
.set('host', 'localhost:3001')
.end(function (err) {
assert.ifError(err);
assert(!fs.existsSync(tmpdir.join('3234ee470c8605a1837e08f218494326.js')));
done();
});
});
});
});

});

describe('playback', function () {
var server, tmpdir, yakbak;

beforeEach(function (done) {
server = createServer(done);
});

afterEach(function (done) {
server.teardown(done);
});

beforeEach(function (done) {
tmpdir = createTmpdir(done);
});

afterEach(function (done) {
tmpdir.teardown(done);
});
describe('playback', function () {
beforeEach(function () {
yakbak = subject(server.host, { dirname: tmpdir.dirname });
});

beforeEach(function () {
yakbak = subject(server.host, { dirname: tmpdir.dirname });
});
beforeEach(function (done) {
var file = '305c77b0a3ad7632e51c717408d8be0f.js';
var tape = [
'var path = require("path");',
'module.exports = function (req, res) {',
' res.statusCode = 201;',
' res.setHeader("content-type", "text/html")',
' res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));',
' res.end("YAY");',
'}',
''
].join('\n');

fs.writeFile(tmpdir.join(file), tape, done);
});

beforeEach(function (done) {
var file = '305c77b0a3ad7632e51c717408d8be0f.js';
var tape = [
'var path = require("path");',
'module.exports = function (req, res) {',
' res.statusCode = 201;',
' res.setHeader("content-type", "text/html")',
' res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));',
' res.end("YAY");',
'}',
''
].join('\n');

fs.writeFile(tmpdir.join(file), tape, done);
});
it('does not make a request to the server', function (done) {
request(yakbak)
.get('/playback/1')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '305c77b0a3ad7632e51c717408d8be0f')
.expect('Content-Type', 'text/html')
.expect(201, 'YAY')
.end(function (err) {
assert.ifError(err);
assert.equal(server.requests.length, 0);
done();
});

it('does not make a request to the server', function (done) {
request(yakbak)
.get('/playback/1')
.set('host', 'localhost:3001')
.expect('X-Yakbak-Tape', '305c77b0a3ad7632e51c717408d8be0f')
.expect('Content-Type', 'text/html')
.expect(201, 'YAY')
.end(function (err) {
assert.ifError(err);
assert.equal(server.requests.length, 0);
done();
});

});
});

0 comments on commit 1f21800

Please sign in to comment.