diff --git a/README.md b/README.md index 283db90..6c87ea1 100644 --- a/README.md +++ b/README.md @@ -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)}`; +}; +``` + ## Geeking diff --git a/package-lock.json b/package-lock.json index 6c1144e..1190d2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "replay", - "version": "2.3.0", + "version": "2.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -7030,6 +7030,12 @@ "is-fullwidth-code-point": "^2.0.0" } }, + "slugify": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/slugify/-/slugify-1.3.4.tgz", + "integrity": "sha512-KP0ZYk5hJNBS8/eIjGkFDCzGQIoZ1mnfQRYS5WM3273z+fxGWXeN0fkwf2ebEweydv9tioZIHGZKoF21U07/nw==", + "dev": true + }, "snapdragon": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/snapdragon/-/snapdragon-0.8.2.tgz", diff --git a/package.json b/package.json index a97d424..458ac03 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/catalog.js b/src/catalog.js index 48d8116..edff8eb 100644 --- a/src/catalog.js +++ b/src/catalog.js @@ -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(':', '-')}`; diff --git a/src/index.js b/src/index.js index c40a1a4..9b6e85f 100644 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/test/replay_test.js b/test/replay_test.js index 96c89ea..68517d9 100644 --- a/test/replay_test.js +++ b/test/replay_test.js @@ -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'); @@ -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}`;