Skip to content

Commit

Permalink
Merge pull request #929 from martinkuba/node8
Browse files Browse the repository at this point in the history
Updates to unblock Node 8
  • Loading branch information
ianwsperber authored Jul 12, 2017
2 parents 39c7e32 + aac5c34 commit 35c76e3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_js:
- 4
- 6
- 7
- 8
script: "npm run coverage && npm run integration"
sudo: false
after_success: "npm run coveralls"
16 changes: 14 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var debug = require('debug')('nock.common');
var semver = require('semver')

/**
* Normalizes the request options so that it always has `host` property.
Expand Down Expand Up @@ -120,7 +121,8 @@ var overrideRequests = function(newRequest) {
http: require('http'),
https: require('https')
}[moduleName],
overriddenRequest = module.request;
overriddenRequest = module.request,
overriddenGet = module.get;

if(requestOverride[moduleName]) {
throw new Error('Module\'s request already overridden for ' + moduleName + ' protocol.');
Expand All @@ -129,14 +131,23 @@ var overrideRequests = function(newRequest) {
// Store the properties of the overridden request so that it can be restored later on.
requestOverride[moduleName] = {
module: module,
request: overriddenRequest
request: overriddenRequest,
get: overriddenGet
};

module.request = function(options, callback) {
// debug('request options:', options);
return newRequest(proto, overriddenRequest.bind(module), options, callback);
};

if (semver.satisfies(process.version, '>=8')) {
module.get = function(options, callback) {
var req = newRequest(proto, overriddenRequest.bind(module), options, callback);
req.end();
return req;
}
}

debug('- overridden request for', proto);
});
};
Expand All @@ -155,6 +166,7 @@ var restoreOverriddenRequests = function() {
var override = requestOverride[proto];
if(override) {
override.module.request = override.request;
override.module.get = override.get;
debug('- restored request for', proto);
}
});
Expand Down
18 changes: 17 additions & 1 deletion lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var debug = require('debug')('nock.recorder');
var _ = require('lodash');
var Stream = require('stream');
var URL = require('url');
var semver = require('semver')

var SEPARATOR = '\n<<<<<<-- cut here -->>>>>>\n';
var recordingInProgress = false;
Expand Down Expand Up @@ -336,10 +337,25 @@ function record(rec_options) {
}
bodyChunks.push(data);
}
oldWrite.call(req, data);
oldWrite.apply(req, arguments);
}
};

// in Node 8, res.end() does not call res.write() directly
if (semver.satisfies(process.version, '>=8')) {
var oldEnd = req.end;
req.end = function(data, encoding) {
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk');
if (! Buffer.isBuffer(data)) {
data = new Buffer(data, encoding);
}
bodyChunks.push(data);
}
oldEnd.apply(req, arguments);
};
}

return req;
});
}
Expand Down
15 changes: 10 additions & 5 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getHeader(request, name) {

var key = name.toLowerCase();

return request._headers[key];
return request.getHeader ? request.getHeader(key) : request._headers[key];
}

function setHeader(request, name, value) {
Expand All @@ -34,8 +34,12 @@ function setHeader(request, name, value) {
request._headerNames = request._headerNames || {};
request._removedHeader = request._removedHeader || {};

request._headers[key] = value;
request._headerNames[key] = name;
if (request.setHeader) {
request.setHeader(key, value);
} else {
request._headers[key] = value;
request._headerNames[key] = name;
}

if (name == 'expect' && value == '100-continue') {
timers.setImmediate(function() {
Expand All @@ -52,7 +56,8 @@ function setRequestHeaders(req, options, interceptor) {
// We mock request headers if these were specified.
if (interceptor.reqheaders) {
_.forOwn(interceptor.reqheaders, function(val, key) {
setHeader(req, key, val);
if (!_.isFunction(val))
setHeader(req, key, val);
});
}

Expand Down Expand Up @@ -268,7 +273,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
// We again set request headers, now for our matched interceptor.
setRequestHeaders(req, options, interceptor);
interceptor.req = req;
req.headers = req._headers;
req.headers = req.getHeaders ? req.getHeaders() : req._headers;

interceptor.scope.emit('request', req, interceptor);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@
"lodash": "~4.17.2",
"mkdirp": "^0.5.0",
"propagate": "0.4.0",
"qs": "^6.0.2"
"qs": "^6.0.2",
"semver": "^5.3.0"
},
"devDependencies": {
"async": "^2.1.1",
Expand Down
6 changes: 2 additions & 4 deletions tests/test_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ test('records nonstandard ports', function(t) {

// Create test http server and perform the tests while it's up.
var testServer = http.createServer(function(req, res) {
res.write(RESPONSE_BODY);
res.end();
res.end(RESPONSE_BODY);
}).listen(8081, function(err) {

t.equal(err, undefined);
Expand Down Expand Up @@ -731,8 +730,7 @@ test('works with clients listening for readable', {skip: process.env.AIRPLANE},

// Create test http server and perform the tests while it's up.
var testServer = http.createServer(function(req, res) {
res.write(RESPONSE_BODY);
res.end();
res.end(RESPONSE_BODY);
}).listen(8081, function(err) {

// t.equal(err, undefined);
Expand Down

0 comments on commit 35c76e3

Please sign in to comment.