Skip to content

Commit

Permalink
Merge pull request #1 from store2be/feature/add-option-to-exclude-hea…
Browse files Browse the repository at this point in the history
…ders-in-tapename

Feature/add option to exclude headers in tapename
  • Loading branch information
Peter Gundel authored Dec 2, 2016
2 parents 27eab62 + af85b03 commit bd05967
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/

Expand All @@ -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);
Expand Down Expand Up @@ -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';
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/yakbak.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit bd05967

Please sign in to comment.