Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #68 from nicsnoek/retry
Browse files Browse the repository at this point in the history
control over which responses are recorded
  • Loading branch information
assaf committed Oct 19, 2017
2 parents 158d871 + 44e0570 commit b5ec3bf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 21 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion src/recorder.js
Expand Up @@ -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;
}
Expand Down
92 changes: 72 additions & 20 deletions test/replay_test.js
Expand Up @@ -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,
Expand All @@ -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);
}
});
});

Expand Down

0 comments on commit b5ec3bf

Please sign in to comment.