From 066e6125e0c5da942650e67bd9b920c949718704 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Wed, 14 Oct 2015 15:46:34 +0500 Subject: [PATCH] Emit either error or reponse event in stream mode Closes #110 --- index.js | 1 + test/stream.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6bcf4b8..c0f3bd0 100644 --- a/index.js +++ b/index.js @@ -165,6 +165,7 @@ function asStream(opts) { var statusCode = res.statusCode; if (statusCode < 200 || statusCode > 299) { proxy.emit('error', new got.HTTPError(statusCode, opts), null, res); + return; } proxy.emit('response', res); diff --git a/test/stream.js b/test/stream.js index 942d79c..c81b275 100644 --- a/test/stream.js +++ b/test/stream.js @@ -98,18 +98,26 @@ test('stream - response event', t => { }); test('stream - error event', t => { - t.plan(4); - got.stream(`${s.url}/error`) + .on('response', () => { + t.fail('response event should not be emitted'); + }) .on('error', (err, data, res) => { t.is(err.message, 'Response code 404 (Not Found)'); t.is(null, data); t.ok(res); + t.end(); }); +}); +test('stream - error event', t => { got.stream('.com') + .on('response', () => { + t.fail('response event should not be emitted'); + }) .on('error', err => { t.regexTest(/getaddrinfo ENOTFOUND/, err.message); + t.end(); }); });