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(); }); });