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

Commit 96f2e09

Browse files
author
Michael Weibel
committed
feat: allow custom file name generator function
fixes #59
1 parent b01018e commit 96f2e09

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"gulp-sourcemaps": "^2.6.4",
5252
"gulp-util": "^3.0.7",
5353
"mocha": "^5.1.1",
54-
"request": "^2.85.0"
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

@@ -351,6 +352,65 @@ describe('Replay', function() {
351352

352353
});
353354

355+
describe('custom filenameGenerator', function() {
356+
const fixturesDir = `${__dirname}/fixtures/127.0.0.1-${HTTP_PORT}`;
357+
358+
before(setup);
359+
360+
before(function() {
361+
Replay.mode = 'record';
362+
Replay.reset('127.0.0.1');
363+
Replay.filenameGenerator = function(request) {
364+
return slugify(`${request.method.toUpperCase()}_${request.url.path}`);
365+
};
366+
});
367+
368+
it('should create fixture files with custom names', function(done) {
369+
const requests = [
370+
{ name: 'Lorem', extra: 'Ipsum'},
371+
{ name: 'Dolor', extra: 'Sit'}
372+
].map(function(query) {
373+
return function(callback) {
374+
Request.get({
375+
url: `http://127.0.0.1:${HTTP_PORT}/query`,
376+
qs: query,
377+
headers: {
378+
'X-Some-Header': 'Test'
379+
},
380+
json: true
381+
}, function(error, response, body) {
382+
if (error)
383+
callback(error);
384+
else
385+
try {
386+
assert.deepEqual(body, query);
387+
callback(null, query);
388+
} catch (error) {
389+
callback(error);
390+
}
391+
});
392+
};
393+
});
394+
395+
Async.series(requests, function(error) {
396+
if (error)
397+
done(error);
398+
else {
399+
// fixtures should be written now
400+
Replay.mode = 'replay';
401+
Async.series(requests, done);
402+
}
403+
});
404+
});
405+
406+
after(function() {
407+
if (File.existsSync(fixturesDir)) {
408+
for (let file of File.readdirSync(fixturesDir))
409+
File.unlinkSync(`${fixturesDir}/${file}`);
410+
File.rmdirSync(fixturesDir);
411+
}
412+
});
413+
});
354414

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

0 commit comments

Comments
 (0)