Skip to content

Commit

Permalink
fix: use aborted property on http request (#1134)
Browse files Browse the repository at this point in the history
I had some difficulty with this code:

    httpRequest.on('timeout', function () {
      trackFailure('timeout')
      httpRequest.abort()
    })

    httpRequest.on('error', function (err) {
      // Count error if request wasn't aborted due to timeout
      if (!httpRequest.aborted) {
        trackFailure(err)
      }
      callback(err)
    }

When `nock` was not in use, `trackFailure()` would be called once on
timeout.  With `nock` enabled, `trackFailure()` would be called twice
because `httpRequest.aborted` would be undefined.

This change creates and uses the `aborted` property in the same
fashion as the system `http` module.
  • Loading branch information
allenluce authored and gr2m committed Jun 7, 2018
1 parent e933b3a commit aaa9a56
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/request_overrider.js
Expand Up @@ -88,7 +88,6 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}

var requestBodyBuffers = [],
aborted,
emitError,
end,
ended,
Expand Down Expand Up @@ -130,7 +129,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

req.write = function(buffer, encoding, callback) {
debug('write', arguments);
if (!aborted) {
if (!req.aborted) {
if (buffer) {
if (!Buffer.isBuffer(buffer)) {
buffer = Buffer.from(buffer, encoding);
Expand All @@ -154,7 +153,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

req.end = function(buffer, encoding, callback) {
debug('req.end');
if (!aborted && !ended) {
if (!req.aborted && !ended) {
req.write(buffer, encoding, function () {
if (typeof callback === 'function') {
callback();
Expand All @@ -164,27 +163,27 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
req.emit('end');
});
}
if (aborted) {
if (req.aborted) {
emitError(new Error('Request aborted'));
}
};

req.flushHeaders = function() {
debug('req.flushHeaders');
if (!aborted && !ended) {
if (!req.aborted && !ended) {
end(cb);
}
if (aborted) {
if (req.aborted) {
emitError(new Error('Request aborted'));
}
};

req.abort = function() {
if (aborted) {
if (req.aborted) {
return;
}
debug('req.abort');
aborted = true;
req.aborted = Date.now();
if (!ended) {
end();
}
Expand Down Expand Up @@ -456,7 +455,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
remove(interceptor);
interceptor.discard();

if (aborted) { return; }
if (req.aborted) { return; }

/// response.client.authorized = true
/// fixes https://github.com/pgte/nock/issues/158
Expand Down Expand Up @@ -493,7 +492,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

function respond() {

if (aborted) { return; }
if (req.aborted) { return; }

if (interceptor.socketDelayInMs && interceptor.socketDelayInMs > 0) {
req.socket.applyDelay(interceptor.socketDelayInMs);
Expand All @@ -507,7 +506,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}

function _respond() {
if (aborted) { return; }
if (req.aborted) { return; }

debug('emitting response');

Expand All @@ -516,7 +515,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
cb(response);
}

if (aborted) {
if (req.aborted) {
emitError(new Error('Request aborted'));
}
else {
Expand Down

0 comments on commit aaa9a56

Please sign in to comment.