From 54b12f6989b7f65874cba140ee54c24c6ac9e920 Mon Sep 17 00:00:00 2001 From: Geoffrey Goodman Date: Tue, 24 Jul 2018 11:33:52 -0400 Subject: [PATCH 1/2] Handle stream payload errors before socket connection --- lib/index.js | 6 ++++++ test/index.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/index.js b/lib/index.js index 893ace9..8a42fd9 100755 --- a/lib/index.js +++ b/lib/index.js @@ -336,10 +336,16 @@ internals.deferPipeUntilSocketConnects = function (req, stream) { }; const onSocketConnect = () => { + stream.removeListener('error', onStreamError); stream.pipe(req); }; + const onStreamError = (err) => { + + req.emit('error', err); + }; req.on('socket', onSocket); + stream.on('error', onStreamError); }; diff --git a/test/index.js b/test/index.js index 53b6ce9..b6bdbc7 100755 --- a/test/index.js +++ b/test/index.js @@ -1162,6 +1162,23 @@ describe('read()', () => { server.close(); }); + it('will handle stream payload errors between request creation and connection establishment', async () => { + + const agent = new internals.SlowAgent(); + const stream = new Stream.Readable(); + const promiseA = Wreck.request('post', 'http://localhost:0', { + agent, + payload: stream + }); + + process.nextTick(() => { + + stream.emit('error', new Error('Asynchronous stream error')); + }); + + await expect(promiseA).to.reject(Error, /Asynchronous stream error/); + }); + it('times out when stream read takes too long', async () => { const TestStream = function () { From 87c9ff6a4a16c098bcbff83d50d86f47454eba32 Mon Sep 17 00:00:00 2001 From: Geoffrey Goodman Date: Tue, 24 Jul 2018 11:34:30 -0400 Subject: [PATCH 2/2] Clean up deferred piping stest --- test/index.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/test/index.js b/test/index.js index b6bdbc7..fa6c4a4 100755 --- a/test/index.js +++ b/test/index.js @@ -1127,17 +1127,11 @@ describe('read()', () => { const stream = new Stream.Readable({ read() { - piped = true; + read = true; this.push(null); } }); - const onPiped = () => { - - piped = true; - }; - let piped = false; - - stream.on('pipe', onPiped); + let read = false; const promiseA = Wreck.request('post', 'http://localhost:0', { agent, @@ -1145,7 +1139,7 @@ describe('read()', () => { }); await expect(promiseA).to.reject(Error, /Unable to obtain socket/); - expect(piped).to.equal(false); + expect(read).to.equal(false); const handler = (req, res) => { @@ -1158,7 +1152,7 @@ describe('read()', () => { payload: stream }); expect(res.statusCode).to.equal(200); - expect(piped).to.equal(true); + expect(read).to.equal(true); server.close(); });