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

feat: allow custom file name generator function #162

Open
wants to merge 1 commit into
base: master
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ Replay.recordResponseControl = {
};
```

The generated fixtures by default are named using a uid generator function and it's reasonably safe for them to be unique.
However because they're only a bunch of numbers, it can be sometimes painful to find which fixture belongs to what request.

You can supply a custom filename generator function if you wish to change that:

```javascript
Replay.filenameGenerator = function(request) {
// just an example, you may want to improve that
return `${slugify(`${request.method.toUpperCase()}_${request.url.path}`)}_${Date.now()}${Math.floor(Math.random() * 100000)}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this should be the default, not the one with just the date time stamp

};
```


## Geeking

Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
"gulp-notify": "^3.2.0",
"gulp-sourcemaps": "^2.6.4",
"gulp-util": "^3.0.7",
"mocha": "^5.2.0",
"request": "^2.88.0"
"mocha": "^5.1.1",
"request": "^2.85.0",
"slugify": "^1.3.4"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ module.exports = class Catalog {
matchers.push(matcher);
const requestHeaders = this.settings.headers;

const uid = `${Date.now()}${Math.floor(Math.random() * 100000)}`;
const uid = this.settings.filenameGenerator(request);
const tmpfile = `${this.getFixturesDir()}/node-replay.${uid}`;
const pathname = `${this.getFixturesDir()}/${host.replace(':', '-')}`;

Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Replay extends EventEmitter {
// Dropp connections to these servers
this._dropped = new Set();

this.filenameGenerator = function() {
return `${Date.now()}${Math.floor(Math.random() * 100000)}`
};
this.catalog = new Catalog(this);
this.headers = MATCH_HEADERS;

Expand Down
60 changes: 60 additions & 0 deletions test/replay_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const HTTP = require('http');
const HTTPS = require('https');
const Async = require('async');
const Request = require('request');
const slugify = require('slugify');
const Replay = require('../src');


Expand Down Expand Up @@ -372,6 +373,65 @@ describe('Replay', function() {

});

describe('custom filenameGenerator', function() {
const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`;

before(setup);

before(function() {
Replay.mode = 'record';
Replay.reset('127.0.0.1');
Replay.filenameGenerator = function(request) {
return slugify(`${request.method.toUpperCase()}_${request.url.path}`);
};
});

it('should create fixture files with custom names', function(done) {
const requests = [
{ name: 'Lorem', extra: 'Ipsum'},
{ name: 'Dolor', extra: 'Sit'}
].map(function(query) {
return function(callback) {
Request.get({
url: `http://127.0.0.1:${HTTP_PORT}/query`,
qs: query,
headers: {
'X-Some-Header': 'Test'
},
json: true
}, function(error, response, body) {
if (error)
callback(error);
else
try {
assert.deepEqual(body, query);
callback(null, query);
} catch (error) {
callback(error);
}
});
};
});

Async.series(requests, function(error) {
if (error)
done(error);
else {
// fixtures should be written now
Replay.mode = 'replay';
Async.series(requests, done);
}
});
});

after(function() {
if (File.existsSync(fixturesDir)) {
for (let file of File.readdirSync(fixturesDir))
File.unlinkSync(`${fixturesDir}/${file}`);
File.rmdirSync(fixturesDir);
}
});
});

describe('recording gzipped replay', function() {
const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`;
Expand Down