diff --git a/lib/request_overrider.js b/lib/request_overrider.js index bd454794b..d0029a3ba 100644 --- a/lib/request_overrider.js +++ b/lib/request_overrider.js @@ -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; diff --git a/tests/test_intercept.js b/tests/test_intercept.js index fde88bfd5..c54481071 100644 --- a/tests/test_intercept.js +++ b/tests/test_intercept.js @@ -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) {