Skip to content

Commit

Permalink
feat: support replay of recorded binary responses
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 24, 2018
1 parent bc9fb01 commit 904d1b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
56 changes: 37 additions & 19 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ var getBodyFromChunks = function(chunks, headers) {
// the body shouldn't be merged but instead persisted as an array
// of hex strings so that the responses can be mocked one by one.
if(common.isContentEncoded(headers)) {
return _.map(chunks, function(chunk) {
if(!Buffer.isBuffer(chunk)) {
if (typeof chunk === 'string') {
chunk = new Buffer(chunk);
} else {
throw new Error('content-encoded responses must all be binary buffers');
return {
body: _.map(chunks, function(chunk) {
if(!Buffer.isBuffer(chunk)) {
if (typeof chunk === 'string') {
chunk = new Buffer(chunk);
} else {
throw new Error('content-encoded responses must all be binary buffers');
}
}
}

return chunk.toString('hex');
});
return chunk.toString('hex');
})
};
}

var mergedBuffer = common.mergeChunks(chunks);
Expand All @@ -72,39 +74,55 @@ var getBodyFromChunks = function(chunks, headers) {
// 2. A string buffer which represents a JSON object.
// 3. A string buffer which doesn't represent a JSON object.

if(common.isBinaryBuffer(mergedBuffer)) {
return mergedBuffer.toString('hex');
var isBinary = common.isBinaryBuffer(mergedBuffer)
if(isBinary) {
return {
body: mergedBuffer.toString('hex'),
isBinary
}
} else {
var maybeStringifiedJson = mergedBuffer.toString('utf8');
try {
return JSON.parse(maybeStringifiedJson);
return {
body: JSON.parse(maybeStringifiedJson),
isBinary: false
};
} catch(err) {
return maybeStringifiedJson;
return {
body: maybeStringifiedJson,
isBinary: false
};
}
}

};

function generateRequestAndResponseObject(req, bodyChunks, options, res, dataChunks) {

var response = getBodyFromChunks(dataChunks, res.headers)
options.path = req.path;
return {

var nockDef = {
scope: getScope(options),
method: getMethod(options),
path: options.path,
body: getBodyFromChunks(bodyChunks),
body: getBodyFromChunks(bodyChunks).body,
status: res.statusCode,
response: getBodyFromChunks(dataChunks, res.headers),
response: response.body,
rawHeaders: res.rawHeaders || res.headers,
reqheaders: req._headers
};

if (response.isBinary) {
nockDef.responseIsBinary = true
}

return nockDef;
}

function generateRequestAndResponse(req, bodyChunks, options, res, dataChunks) {

var requestBody = getBodyFromChunks(bodyChunks);
var responseBody = getBodyFromChunks(dataChunks, res.headers);
var requestBody = getBodyFromChunks(bodyChunks).body;
var responseBody = getBodyFromChunks(dataChunks, res.headers).body;

// Remove any query params from options.path so they can be added in the query() function
var path = options.path;
Expand Down
2 changes: 2 additions & 0 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ function define(nockDefs) {
var response;
if (!nockDef.response) {
response = '';
} else if (nockDef.responseIsBinary) {
response = Buffer.from(nockDef.response, 'hex')
} else {
response = _.isString(nockDef.response) ? tryJsonParse(nockDef.response) : nockDef.response;
}
Expand Down

0 comments on commit 904d1b3

Please sign in to comment.