Skip to content

Commit

Permalink
fix: request overrider checks req.headers to parse body as JSON (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArTiSTiX authored and gr2m committed Jul 2, 2018
1 parent e15820a commit f81fb6d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 23 deletions.
6 changes: 3 additions & 3 deletions lib/request_overrider.js
Expand Up @@ -313,14 +313,14 @@ function RequestOverrider(req, options, interceptors, remove, cb) {


if (typeof interceptor.body === 'function') {
if (requestBody && common.isJSONContent(options.headers)) {
if (requestBody && common.contentEncoding(options.headers, 'gzip')) {
if (requestBody && common.isJSONContent(req.headers)) {
if (requestBody && common.contentEncoding(req.headers, 'gzip')) {
if (typeof zlib.gunzipSync !== 'function') {
emitError(new Error('Gzip encoding is currently not supported in this version of Node.'));
return;
}
requestBody = String(zlib.gunzipSync(new Buffer(requestBody, 'hex')), 'hex')
} else if (requestBody && common.contentEncoding(options.headers, 'deflate')) {
} else if (requestBody && common.contentEncoding(req.headers, 'deflate')) {
if (typeof zlib.deflateSync !== 'function') {
emitError(new Error('Deflate encoding is currently not supported in this version of Node.'));
return;
Expand Down
76 changes: 56 additions & 20 deletions tests/test_intercept.js
Expand Up @@ -82,37 +82,73 @@ test("allow unmocked works after one interceptor is removed", function(t) {
});
});

test("reply callback's requestBody should automatically parse to JSON", function(t) {
test("reply callback's requestBody should automatically parse to JSON when content-type is json", function(t) {
var requestBodyFixture = {
id: 1,
name: 'bob'
id: 1,
name: 'bob'
};

var scope = nock('http://service')
.post('/endpoint')
.reply(200, function(uri, requestBody) {
t.deepEqual(requestBody, requestBodyFixture);
requestBody.id = 'overwrite';
.post('/endpoint')
.reply(200, function(uri, requestBody) {
t.deepEqual(requestBody, requestBodyFixture)

return requestBody;
});
return 'overwrite';
});

var req = http.request({
host: 'service',
method: 'POST',
path: '/endpoint',
port: 80
}, function(res) {
t.equal(res.statusCode, 200);
res.on('end', function() {
scope.done();
t.end();
});
res.on('data', function(data) {
t.equal(data.toString(), 'overwrite', 'response should match mocked value');
});
});

req.setHeader('Content-Type', 'application/json');
req.write(JSON.stringify(requestBodyFixture));
req.end();
});

var options = {
method: "POST",
url: "http://service/endpoint",
body: requestBodyFixture,
json: true
test("reply callback's requestBody should not automatically parse to JSON", function(t) {
var requestBodyFixture = {
id: 1,
name: 'bob'
};

mikealRequest(options, function(err, resp, body) {
var scope = nock('http://service')
.post('/endpoint')
.reply(200, function(uri, requestBody) {
t.deepEqual(requestBody, JSON.stringify(requestBodyFixture))

return 'overwrite';
});

var req = http.request({
host: "service",
method: 'POST',
path: '/endpoint',
port: 80
}, function(res) {
t.equal(res.statusCode, 200);
res.on('end', function() {
scope.done();
t.equal(resp.statusCode, 200);
var expect = _.defaults({
id: 'overwrite'
}, requestBodyFixture);
t.deepEqual(expect, body);
t.end();
});
res.on('data', function(data) {
t.equal(data.toString(), "overwrite", "response should match mocked value");
});
});

req.write(JSON.stringify(requestBodyFixture));
req.end();
});

test("reply can take a callback", function(t) {
Expand Down

0 comments on commit f81fb6d

Please sign in to comment.