From 1a2bf616723f5774e54f04ecb683923660d3b772 Mon Sep 17 00:00:00 2001 From: Patrick Ma & Ed Bartholomew Date: Thu, 15 Sep 2016 18:17:20 -0700 Subject: [PATCH 1/2] add simpleTapeName yakbak option --- CHANGELOG.md | 6 ++++++ index.js | 12 +++++++++--- test/yakbak.js | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 575b1de..fcb5600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [2.6.0] - 2016-09-15 + +### Added + +- `simpleTapeName` option only includes `req.method` + `req.url` in `tapename`. + ## [2.5.0] - 2016-06-22 ### Added diff --git a/index.js b/index.js index 1022c59..a3a0f5b 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ var debug = require('debug')('yakbak:server'); * @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 + * @param {Boolean} opts.simpleTapeName if true only includes `req.method` + `req.url` in `tapename` * @returns {Function} */ @@ -30,7 +31,7 @@ module.exports = function (host, opts) { debug('req', req.url); return buffer(req).then(function (body) { - var file = path.join(opts.dirname, tapename(req, body)); + var file = path.join(opts.dirname, tapename(req, body, opts.simpleTapeName)); return Promise.try(function () { return require.resolve(file); @@ -69,8 +70,13 @@ module.exports = function (host, opts) { * @returns {String} */ -function tapename(req, body) { - return hash.sync(req, Buffer.concat(body)) + '.js'; +function tapename(req, body, simple = false) { + if (simple) { + return `${req.method}${req.url.replace(/\//g, '_')}.js`; + } else { + console.log(hash.sync(req, Buffer.concat(body)) + '.js'); + return hash.sync(req, Buffer.concat(body)) + '.js'; + } } /** diff --git a/test/yakbak.js b/test/yakbak.js index 28f5edc..7369009 100644 --- a/test/yakbak.js +++ b/test/yakbak.js @@ -62,6 +62,26 @@ describe('yakbak', function () { done(); }); }); + + describe('when simpleTapeName = true', function () { + beforeEach(function () { + yakbak = subject(server.host, { dirname: tmpdir.dirname, simpleTapeName: true }); + }); + + it('can write to disk with a descriptive filename', function(done) { + request(yakbak) + .get('/record/2') + .set('host', 'localhost:3001') + .expect('X-Yakbak-Tape', 'GET_record_2') + .expect('Content-Type', 'text/html') + .expect(201, 'OK') + .end(function (err) { + assert.ifError(err); + assert(fs.existsSync(tmpdir.join('GET_record_2.js'))); + done(); + }); + }); + }); }); describe("when recording is not enabled", function () { From af85b0302e2ac8d106d7ada8320cac52700ccceb Mon Sep 17 00:00:00 2001 From: Patrick Ma & Ed Bartholomew Date: Thu, 15 Sep 2016 18:19:39 -0700 Subject: [PATCH 2/2] update readme to include simpleTapeName --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2049dd0..0ae9a53 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ var handler = yakbak('http://api.flickr.com', { - `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 +- `simpleTapeName` if true only includes `req.method` + `req.url` in `tapename` ### with node's http module