diff --git a/README.md b/README.md index 053ae60..7e7d663 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,18 @@ Replay.headers.push(/^content-length/); Since headers are case insensitive, we always match on the lower case name. +If you want more control over the responses that you record (for example weeding out error responses for +really flaky backends or when you are polling waiting for something to happen), you can use recordReponseControl +to excercise fine grain control over the recording process. For example, to only save the responses from myhostame.com port 8080 if the response was successful: + +```javascript +Replay.recordResponseControl = { + "myhostname.com:8080" : function(request, response) { + return response.statusCode < 400; + } +}; +``` + ## Geeking diff --git a/src/recorder.js b/src/recorder.js index b10fc03..2aa5037 100644 --- a/src/recorder.js +++ b/src/recorder.js @@ -42,10 +42,18 @@ module.exports = function recorded(settings) { capture(request, function(error, response) { if (error) callback(error); - else + else { + if (settings.recordResponseControl && settings.recordResponseControl[host]) { + if (!settings.recordResponseControl[host](request, response)) { + // don't save responses we don't like, eg. errors, + callback(null, response); + return; + } + } catalog.save(host, request, response, function(saveError) { callback(saveError, response); }); + }; }); return; } diff --git a/test/replay_test.js b/test/replay_test.js index f2a5025..f0eb2e9 100644 --- a/test/replay_test.js +++ b/test/replay_test.js @@ -431,16 +431,10 @@ describe('Replay', function() { describe('recording POST data', function() { - const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`; - - before(setup); - before(function() { - Replay.mode = 'record'; - Replay.reset('127.0.0.1'); - }); + const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`; - before(function(done) { + function setupPostRequest(done) { const request = HTTP.request({ hostname: '127.0.0.1', port: HTTP_PORT, @@ -451,22 +445,80 @@ describe('Replay', function() { }); request.write('request data'); request.end(); + } + + function hasSavedPostRequestData() { + if (File.existsSync(fixturesDir)) { + let hasData = false; + const files = File.readdirSync(fixturesDir); + const fixture = File.readFileSync(`${fixturesDir}/${files[0]}`, 'utf8'); + for (let line of fixture.split('\n')) + if (line === 'body: request data') + hasData = true; + return hasData; + } + } + + before(setup); + + before(function() { + Replay.mode = 'record'; + Replay.reset('127.0.0.1'); }); - it('should save POST request data', function() { - let hasData = false; - const files = File.readdirSync(fixturesDir); - const fixture = File.readFileSync(`${fixturesDir}/${files[0]}`, 'utf8'); - for (let line of fixture.split('\n')) - if (line === 'body: request data') - hasData = true; - assert(hasData); + context('without record response control', function() { + before(setupPostRequest); + + it('should save POST request data', function() { + assert(hasSavedPostRequestData()); + }); }); - after(function() { - for (let file of File.readdirSync(fixturesDir)) - File.unlinkSync(`${fixturesDir}/${file}`); - File.rmdirSync(fixturesDir); + describe('with record response control', function() { + context('that indicates response should be recorded', function() { + before(function() { + Replay.recordResponseControl = { + ['127.0.0.1:' + HTTP_PORT] : function() { + return true; + } + }; + }); + + before(setupPostRequest); + + it('should save POST request data', function() { + assert(hasSavedPostRequestData()); + }); + }); + context('that indicates response should not be recorded', function() { + before(function() { + Replay.recordResponseControl = { + ['127.0.0.1:' + HTTP_PORT] : function() { + return false; + } + }; + }); + + before(setupPostRequest); + + it('should not save POST request data', function() { + assert(!hasSavedPostRequestData()); + }); + }); + + after(function() { + Replay.recordResponseControl = null; + }); + }); + + + + afterEach(function() { + if (File.existsSync(fixturesDir)) { + for (let file of File.readdirSync(fixturesDir)) + File.unlinkSync(`${fixturesDir}/${file}`); + File.rmdirSync(fixturesDir); + } }); });