From 12011cd59c256fd6df2718d93ebd9a46a970382c Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 23 Feb 2015 16:48:55 +0100 Subject: [PATCH 1/3] Handle missing abort() response in node 0.12 This fixes 2 failing test cases, and adds a new related non-failing case --- lib/index.js | 25 +++++++++++++++++++++---- test/index.js | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8067b6d..4ef06fd 100755 --- a/lib/index.js +++ b/lib/index.js @@ -80,10 +80,6 @@ exports.request = function (method, url, options, callback, _trace) { var finish = function (err, res) { if (!callback || err) { - if (res) { - res.destroy(); - } - req.abort(); } @@ -177,6 +173,27 @@ exports.request = function (method, url, options, callback, _trace) { req.write(options.payload); } + // Custom abort method to detect early aborts + + var _abort = req.abort; + var aborted = false; + req.abort = function () { + + if (!aborted && !req.res && !req.socket) { + process.nextTick(function () { + + // Fake an ECONNRESET error + + var error = new Error('socket hang up'); + error.code = 'ECONNRESET'; + finish(error); + }); + } + + aborted = true; + return _abort.call(req); + }; + // Finalize request req.end(); diff --git a/test/index.js b/test/index.js index c9065a6..975bd36 100755 --- a/test/index.js +++ b/test/index.js @@ -667,6 +667,28 @@ describe('request()', function () { }); }); + it('in-progress requests can be aborted', function (done) { + + var wreck; + var server = Http.createServer(function (req, res) { + + res.writeHead(200); + res.end(); + + wreck.abort(); + }); + + server.listen(0, function () { + + wreck = Wreck.request('get', 'http://localhost:' + server.address().port, {}, function (err) { + + expect(err).to.exist(); + expect(err.code).to.equal('ECONNRESET'); + done(); + }); + }); + }); + it('uses agent option', function (done) { var agent = new Http.Agent(); From 3e13456faee5a97ec6cece06fad1529f44e780b0 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 23 Feb 2015 17:22:29 +0100 Subject: [PATCH 2/3] Use more compatible SSL method --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 975bd36..c382959 100755 --- a/test/index.js +++ b/test/index.js @@ -211,7 +211,7 @@ describe('request()', function () { it('requests an https resource with secure protocol set', function (done) { - Wreck.request('get', 'https://google.com', { rejectUnauthorized: true, secureProtocol: 'SSLv3_method' }, function (err, res) { + Wreck.request('get', 'https://google.com', { rejectUnauthorized: true, secureProtocol: 'SSLv23_method' }, function (err, res) { expect(err).to.not.exist(); Wreck.read(res, null, function (err, body) { From 865900cb4bf606c83138c6c34aeaa20dcfbdf555 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 23 Feb 2015 17:23:26 +0100 Subject: [PATCH 3/3] Test on 0.12 & io.js --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 047f7e3..2963818 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: node_js node_js: - - 0.10 + - "0.10" + - "0.12" + - iojs