Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new Option to configure yakbak to record only successful requests #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- `recordOnlySuccess` if true, only successful requests (response status code = 2XX) will be recorded
- `hash(req, body)` provide your own IncomingMessage hash function

### with node's http module
Expand Down
16 changes: 13 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.recordOnlySuccess if true, only successful requests will be recorded
* @returns {Function}
*/

Expand All @@ -31,6 +32,7 @@ module.exports = function (host, opts) {

return buffer(req).then(function (body) {
var file = path.join(opts.dirname, tapename(req, body));
var successfulResCodePattern = /^[2][0|2][0-8]$/;

return Promise.try(function () {
return require.resolve(file);
Expand All @@ -40,7 +42,15 @@ module.exports = function (host, opts) {
throw new RecordingDisabledError('Recording Disabled');
} else {
return proxy(req, body, host).then(function (pres) {
return record(pres.req, pres, file);
if (opts.recordOnlySuccess === true) {
if (successfulResCodePattern.test(pres.statusCode)) {
return record(pres.req, pres, file);
} else {
throw new RecordingDisabledError('Only Successful responses will be recorded');
}
} else {
return record(pres.req, pres, file);
}
});
}

Expand All @@ -51,7 +61,7 @@ module.exports = function (host, opts) {
return tape(req, res);
}).catch(RecordingDisabledError, function (err) {
/* eslint-disable no-console */
console.log('An HTTP request has been made that yakbak does not know how to handle');
console.log(err.message);
console.log(curl.request(req));
/* eslint-enable no-console */
res.statusCode = err.status;
Expand Down Expand Up @@ -87,7 +97,7 @@ function ModuleNotFoundError(err) {

/**
* Error class that is thrown when an unmatched request
* is encountered in noRecord mode
* is encountered in noRecord mode or when a request failed in recordOnlySuccess mode
* @constructor
*/

Expand Down
6 changes: 3 additions & 3 deletions test/helpers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var http = require('http');
/**
* Creates a test HTTP server.
* @param {Function} done
* @param {boolean} failRequest - Specifies whether response has to be error or not
* @returns {http.Server}
*/

module.exports = function createServer(cb) {

module.exports = function createServer(cb, failRequest) {
var server = http.createServer(function (req, res) {
res.statusCode = 201;
res.statusCode = failRequest === true ? 404 : 201;

res.setHeader('Content-Type', 'text/html');
res.setHeader('Date', 'Sat, 26 Oct 1985 08:20:00 GMT');
Expand Down
29 changes: 29 additions & 0 deletions test/yakbak.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,35 @@ describe('yakbak', function () {
});
});
});

describe("when onlySuccessResponse is enabled", function () {
beforeEach(function (done) {
/* tear down the server created in global scope as we
need different server object which can send response with failed status code*/
server.teardown(done);
});

beforeEach(function (done) {
/* Send the failed response for the requests this server handles */
server = createServer(done, true);
});

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

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

describe('playback', function () {
Expand Down