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

Commit 21fab7b

Browse files
author
Michael Weibel
committed
feat: allow custom file name generator function
fixes #59
1 parent 9319382 commit 21fab7b

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ Replay.recordResponseControl = {
286286
};
287287
```
288288

289+
The generated fixtures by default are named using a uid generator function and it's reasonably safe for them to be unique.
290+
However because they're only a bunch of numbers, it can be sometimes painful to find which fixture belongs to what request.
291+
292+
You can supply a custom filename generator function if you wish to change that:
293+
294+
```javascript
295+
Replay.filenameGenerator = function(request) {
296+
// just an example, you may want to improve that
297+
return `${slugify(`${request.method.toUpperCase()}_${request.url.path}`)}_${Date.now()}${Math.floor(Math.random() * 100000)}`;
298+
};
299+
```
300+
289301

290302
## Geeking
291303

package-lock.json

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@
5050
"gulp-notify": "^3.2.0",
5151
"gulp-sourcemaps": "^2.6.4",
5252
"gulp-util": "^3.0.7",
53-
"mocha": "^5.2.0",
54-
"request": "^2.88.0"
53+
"mocha": "^5.1.1",
54+
"request": "^2.85.0",
55+
"slugify": "^1.3.4"
5556
},
5657
"repository": {
5758
"type": "git",

src/catalog.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports = class Catalog {
209209
matchers.push(matcher);
210210
const requestHeaders = this.settings.headers;
211211

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

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class Replay extends EventEmitter {
7171
// Dropp connections to these servers
7272
this._dropped = new Set();
7373

74+
this.filenameGenerator = function() {
75+
return `${Date.now()}${Math.floor(Math.random() * 100000)}`
76+
};
7477
this.catalog = new Catalog(this);
7578
this.headers = MATCH_HEADERS;
7679

test/replay_test.js

+60
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const HTTP = require('http');
55
const HTTPS = require('https');
66
const Async = require('async');
77
const Request = require('request');
8+
const slugify = require('slugify');
89
const Replay = require('../src');
910

1011

@@ -372,6 +373,65 @@ describe('Replay', function() {
372373

373374
});
374375

376+
describe('custom filenameGenerator', function() {
377+
const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`;
378+
379+
before(setup);
380+
381+
before(function() {
382+
Replay.mode = 'record';
383+
Replay.reset('127.0.0.1');
384+
Replay.filenameGenerator = function(request) {
385+
return slugify(`${request.method.toUpperCase()}_${request.url.path}`);
386+
};
387+
});
388+
389+
it('should create fixture files with custom names', function(done) {
390+
const requests = [
391+
{ name: 'Lorem', extra: 'Ipsum'},
392+
{ name: 'Dolor', extra: 'Sit'}
393+
].map(function(query) {
394+
return function(callback) {
395+
Request.get({
396+
url: `http://127.0.0.1:${HTTP_PORT}/query`,
397+
qs: query,
398+
headers: {
399+
'X-Some-Header': 'Test'
400+
},
401+
json: true
402+
}, function(error, response, body) {
403+
if (error)
404+
callback(error);
405+
else
406+
try {
407+
assert.deepEqual(body, query);
408+
callback(null, query);
409+
} catch (error) {
410+
callback(error);
411+
}
412+
});
413+
};
414+
});
415+
416+
Async.series(requests, function(error) {
417+
if (error)
418+
done(error);
419+
else {
420+
// fixtures should be written now
421+
Replay.mode = 'replay';
422+
Async.series(requests, done);
423+
}
424+
});
425+
});
426+
427+
after(function() {
428+
if (File.existsSync(fixturesDir)) {
429+
for (let file of File.readdirSync(fixturesDir))
430+
File.unlinkSync(`${fixturesDir}/${file}`);
431+
File.rmdirSync(fixturesDir);
432+
}
433+
});
434+
});
375435

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

0 commit comments

Comments
 (0)