Comparing version 0.22.0 to 0.22.1
@@ -6,2 +6,3 @@ const _ = require('lodash'); | ||
const pathToRegExp = require('path-to-regexp'); | ||
const JSONparseSafe = require('json-parse-safe'); | ||
const { | ||
@@ -55,12 +56,5 @@ decodedPortRegex, | ||
if (contentType && contentType.includes('/json')) { | ||
/* eslint-disable no-empty */ | ||
try { | ||
const json = JSON.parse(body); | ||
return { | ||
json | ||
}; | ||
} catch (err) { | ||
// silence any errors, invalid JSON is ok | ||
} | ||
/* eslint-enable no-empty */ | ||
return { | ||
json: JSONparseSafe(body).value | ||
}; | ||
} | ||
@@ -67,0 +61,0 @@ |
@@ -16,2 +16,4 @@ const { isEmpty } = require('lodash'); | ||
group = groups.find(g => g.test(reqUrl)); | ||
if (!group) return; | ||
} | ||
@@ -18,0 +20,0 @@ |
const makeRecord = app => { | ||
const record = (name, options = {}) => { | ||
let groups; | ||
app.locals.recording = true; | ||
@@ -26,5 +24,8 @@ | ||
// array of strings | ||
if (options.groups) { | ||
groups = options.groups | ||
let groups = options.groups || options.group; // support alias | ||
if (groups) { | ||
groups = Array.isArray(groups) ? groups : groups.split(',').map(v => v.trim()); | ||
groups = groups | ||
.map(groupName => { | ||
@@ -31,0 +32,0 @@ // map like `{"myGroup": "/some.*/regex/"}` |
@@ -13,17 +13,40 @@ const fs = require('fs'); | ||
const makeRecordStop = app => { | ||
const recordStop = cb => { | ||
const recordStop = (options = {}, cb) => { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
const { noWrite } = options; | ||
app.locals.recording = false; | ||
if (!app.locals.recordMeta) { | ||
return; | ||
} | ||
const { recordToFixtures, recordToFixturesMode, formatScript } = app.config; | ||
const { | ||
recordMeta: { name, set } | ||
recordMeta: { name, set } = {} | ||
} = app.locals; | ||
if (!name) throw new Error('Not recording.'); | ||
if (!name) { | ||
const error = new Error('Not recording.'); | ||
delete app.locals.recordMeta; | ||
if (cb) { | ||
cb(error); | ||
return; | ||
} | ||
throw error; | ||
} | ||
if (noWrite) { | ||
delete app.locals.recordMeta; | ||
if (cb) cb(); | ||
return; | ||
} | ||
const { suitesDir, fixturesDir } = app.config; | ||
@@ -43,5 +66,5 @@ | ||
const newSet = set.map(suite => { | ||
const [match, responseOptions, options = {}] = suite; | ||
const [match, responseOptions, suiteMeta = {}] = suite; | ||
const { group } = options; | ||
const { group } = suiteMeta; | ||
@@ -121,3 +144,3 @@ const { directory } = group || {}; | ||
cb(err); | ||
if (cb) cb(err); | ||
@@ -124,0 +147,0 @@ return; |
@@ -8,2 +8,4 @@ const makeUnwatch = app => { | ||
watcher.close(); | ||
app.log('watch', 'stopped watching'); | ||
}; | ||
@@ -10,0 +12,0 @@ |
@@ -43,2 +43,3 @@ const _ = require('lodash'); | ||
const watcher = chokidar.watch([suitesDir, fixturesDir]).on('all', debounced); | ||
app.log('watch', 'started watching'); | ||
app.locals.watcher = watcher; | ||
@@ -45,0 +46,0 @@ }; |
{ | ||
"name": "mockyeah", | ||
"version": "0.22.0", | ||
"version": "0.22.1", | ||
"description": "A powerful service mocking, recording, and playback utility.", | ||
@@ -85,2 +85,3 @@ "main": "index.js", | ||
"is-absolute-url": "^2.1.0", | ||
"json-parse-safe": "^1.0.5", | ||
"lodash": "^4.17.5", | ||
@@ -92,4 +93,3 @@ "mkdirp": "^0.5.1", | ||
}, | ||
"private": false, | ||
"gitHead": "b861712ba39c50866aacf384eec4808873382dc1" | ||
"private": false | ||
} |
'use strict'; | ||
const express = require('express'); | ||
const JSONparseSafe = require('json-parse-safe'); | ||
@@ -21,3 +22,3 @@ // TODO: Implement support for HTTPS admin server protocol. | ||
try { | ||
app.record(name, options ? JSON.parse(options) : undefined); | ||
app.record(name, JSONparseSafe(options).value); | ||
} catch (err) { | ||
@@ -33,3 +34,5 @@ next(err); | ||
try { | ||
app.recordStop(err => { | ||
const { options } = req.query; | ||
app.recordStop(JSONparseSafe(options).value, err => { | ||
if (err) { | ||
@@ -36,0 +39,0 @@ next(err); |
@@ -14,3 +14,3 @@ 'use strict'; | ||
describe('Record and Playback Admin Server', function() { | ||
describe('Record and Playback Admin Server', function () { | ||
let proxy; | ||
@@ -25,3 +25,3 @@ let remote; | ||
[ | ||
function(cb) { | ||
function (cb) { | ||
// Instantiate proxy server for recording | ||
@@ -38,3 +38,3 @@ proxy = MockYeahServer( | ||
}, | ||
function(cb) { | ||
function (cb) { | ||
// Instantiate remote server | ||
@@ -75,3 +75,3 @@ remote = MockYeahServer( | ||
it('should record and playback suite over admin server', function(done) { | ||
it('should record and playback suite over admin server', function (done) { | ||
this.timeout = 10000; | ||
@@ -164,3 +164,3 @@ | ||
it('should record and playback calls matching `headers` option over admin server', function(done) { | ||
it('should record and playback calls matching `headers` option over admin server', function (done) { | ||
this.timeout = 10000; | ||
@@ -254,3 +254,3 @@ | ||
it('should record and playback suite with playAll over admin server', function(done) { | ||
it('should record and playback suite with playAll over admin server', function (done) { | ||
this.timeout = 10000; | ||
@@ -332,2 +332,59 @@ | ||
}); | ||
it('record stop should support no write', function (done) { | ||
this.timeout = 10000; | ||
const suiteName = 'test-some-fancy-admin-server-suite-record-stop-no-write'; | ||
// Construct remote service urls | ||
// e.g. http://localhost:4041/http://example.com/some/service | ||
const path1 = '/some/service/one'; | ||
// Mount remote service end points | ||
remote.get('/some/service/one', { text: 'first' }); | ||
// Initiate recording and playback series | ||
async.series( | ||
[ | ||
// Initiate recording | ||
cb => { | ||
proxyAdminReq.get(`/record?name=${suiteName}`).expect(204, cb); | ||
}, | ||
// Invoke requests to remote services through proxy | ||
// e.g. http://localhost:4041/http://example.com/some/service | ||
cb => proxyReq.get(path1).expect(200, 'first', cb), | ||
// Stop recording | ||
cb => { | ||
proxyAdminReq.get('/record-stop?options={"noWrite":true}').expect(204, cb); | ||
}, | ||
// Assert suite file does not exists | ||
cb => { | ||
try { | ||
fs.statSync(getSuiteFilePath(suiteName)); | ||
cb(new Error('should fail')); | ||
} catch (error) { | ||
cb(); | ||
} | ||
} | ||
], | ||
done | ||
); | ||
}); | ||
it('should fail when calling record stop when not recording', function (done) { | ||
this.timeout = 10000; | ||
// Initiate recording and playback series | ||
async.series( | ||
[ | ||
cb => { | ||
proxyAdminReq.get(`/record-stop`).expect(500, cb); | ||
} | ||
], | ||
done | ||
); | ||
}) | ||
}); |
@@ -15,3 +15,3 @@ 'use strict'; | ||
describe('Record Groups Test', function() { | ||
describe('Record Groups Test', function () { | ||
let proxy; | ||
@@ -25,3 +25,3 @@ let remote; | ||
[ | ||
function(cb) { | ||
function (cb) { | ||
// Instantiate proxy server for recording | ||
@@ -50,3 +50,3 @@ proxy = MockYeahServer( | ||
}, | ||
function(cb) { | ||
function (cb) { | ||
// Instantiate remote server | ||
@@ -90,6 +90,6 @@ remote = MockYeahServer( | ||
it('should record fixture to group subdirectory', function(done) { | ||
it('should record fixture to group subdirectory', function (done) { | ||
this.timeout = 10000; | ||
const suiteName = 'test-some-fancy-suite-group-file'; | ||
const suiteName = 'test-some-fancy-suite-groups-file'; | ||
@@ -101,2 +101,3 @@ // Construct remote service urls | ||
const path3 = '/some/named-dir/service/three'; | ||
const path4 = '/some/other'; | ||
@@ -107,2 +108,3 @@ // Mount remote service end points | ||
remote.get(path3, { text: 'hey named dir there' }); | ||
remote.get(path4, { text: 'hey other' }); | ||
@@ -130,2 +132,3 @@ // Initiate recording and playback series | ||
cb => proxyReq.get(path3).expect(200, cb), | ||
cb => proxyReq.get(path4).expect(200, cb), | ||
@@ -165,8 +168,9 @@ // Stop recording | ||
expect(contents).to.contain( | ||
'"fixture": "someService/test-some-fancy-suite-group-file/0.txt"' | ||
'"fixture": "someService/test-some-fancy-suite-groups-file/0.txt"' | ||
); | ||
expect(contents).to.contain( | ||
'"fixture": "someServiceDirectoryForNamedDir/test-some-fancy-suite-group-file/0.txt"' | ||
'"fixture": "someServiceDirectoryForNamedDir/test-some-fancy-suite-groups-file/0.txt"' | ||
); | ||
expect(contents).to.contain('"fixture": "test-some-fancy-suite-group-file/0.txt"'); | ||
expect(contents).to.contain('"fixture": "test-some-fancy-suite-groups-file/0.txt"'); | ||
expect(contents).not.to.contain('"fixture": "test-some-fancy-suite-groups-file/1.txt"'); | ||
cb(); | ||
@@ -173,0 +177,0 @@ }, |
702830
171
7543
17
22
+ Addedjson-parse-safe@^1.0.5
+ Addedjson-parse-safe@1.0.5(transitive)