Skip to content

Commit

Permalink
abort behaves. somehow addresses #439
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Jan 7, 2016
1 parent beb9bc6 commit 28f6b29
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
43 changes: 30 additions & 13 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
response.emit('close', err);

req.socket.destroy();

req.emit('abort');
};

// restify listens for a 'socket' event to
Expand Down Expand Up @@ -219,6 +221,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
responseBuffers,
interceptor;

var continued = false;

// When request body is a binary buffer we internally use in its hexadecimal representation.
var requestBodyBuffer = common.mergeChunks(requestBodyBuffers);
var isBinaryRequestBodyBuffer = common.isBinaryBuffer(requestBodyBuffer);
Expand Down Expand Up @@ -348,10 +352,15 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return continueWithResponseBody(null, responseBody);

function continueWithResponseBody(err, responseBody) {

if (continued) {
return;
}
continued = true;

if (err) {
response.statusCode = 500;
responseBody = err.stack;

}

// Transform the response body if it exists (it may not exist
Expand Down Expand Up @@ -430,8 +439,25 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}


process.nextTick(function() {
var respond = function() {
process.nextTick(respond);

function respond() {

if (aborted) { return; }

if (interceptor.socketDelayInMs && interceptor.socketDelayInMs > 0) {
req.socket._checkTimeout(interceptor.socketDelayInMs);
}

if (interceptor.delayConnectionInMs && interceptor.delayConnectionInMs > 0) {
setTimeout(_respond, interceptor.delayConnectionInMs);
} else {
_respond();
}

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

debug('emitting response');

if (typeof cb === 'function') {
Expand Down Expand Up @@ -474,16 +500,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
};

if (interceptor.socketDelayInMs && interceptor.socketDelayInMs > 0) {
req.socket._checkTimeout(interceptor.socketDelayInMs);
}

if (interceptor.delayConnectionInMs && interceptor.delayConnectionInMs > 0) {
setTimeout(respond, interceptor.delayConnectionInMs);
} else {
respond();
}
});
}
}
};

Expand Down
43 changes: 30 additions & 13 deletions tests/browserify-public/browserify-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
response.emit('close', err);

req.socket.destroy();

req.emit('abort');
};

// restify listens for a 'socket' event to
Expand Down Expand Up @@ -1841,6 +1843,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
responseBuffers,
interceptor;

var continued = false;

// When request body is a binary buffer we internally use in its hexadecimal representation.
var requestBodyBuffer = common.mergeChunks(requestBodyBuffers);
var isBinaryRequestBodyBuffer = common.isBinaryBuffer(requestBodyBuffer);
Expand Down Expand Up @@ -1970,10 +1974,15 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return continueWithResponseBody(null, responseBody);

function continueWithResponseBody(err, responseBody) {

if (continued) {
return;
}
continued = true;

if (err) {
response.statusCode = 500;
responseBody = err.stack;

}

// Transform the response body if it exists (it may not exist
Expand Down Expand Up @@ -2052,8 +2061,25 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}


process.nextTick(function() {
var respond = function() {
process.nextTick(respond);

function respond() {

if (aborted) { return; }

if (interceptor.socketDelayInMs && interceptor.socketDelayInMs > 0) {
req.socket._checkTimeout(interceptor.socketDelayInMs);
}

if (interceptor.delayConnectionInMs && interceptor.delayConnectionInMs > 0) {
setTimeout(_respond, interceptor.delayConnectionInMs);
} else {
_respond();
}

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

debug('emitting response');

if (typeof cb === 'function') {
Expand Down Expand Up @@ -2096,16 +2122,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
};

if (interceptor.socketDelayInMs && interceptor.socketDelayInMs > 0) {
req.socket._checkTimeout(interceptor.socketDelayInMs);
}

if (interceptor.delayConnectionInMs && interceptor.delayConnectionInMs > 0) {
setTimeout(respond, interceptor.delayConnectionInMs);
} else {
respond();
}
});
}
}
};

Expand Down
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ if (process.versions.node >= '0.11' ) {
}
require('./test_encode_querystring');
require('./test_body_match');
require('./test_abort');
22 changes: 22 additions & 0 deletions tests/test_abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

var nock = require('../.');
var http = require('http');
var test = require('tap').test;

test("double activation throws exception", function(t) {
nock('http://test.example.com')
.get('/status')
.delayConnection(500)
.reply(204)

var tstart = Date.now()
var req = http.get('http://test.example.com/status')
// Don't bother with the response
.once('abort', function () {
var actual = Date.now() - tstart;
t.ok(actual < 250, 'abort took only ' + actual + ' ms');
t.end();
});
setTimeout(function(){ req.abort() }, 10);
});

0 comments on commit 28f6b29

Please sign in to comment.