Browse Source

Emit either error or reponse event in stream mode

Closes #110
http2
Vsevolod Strukchinsky 9 years ago
parent
commit
066e6125e0
  1. 1
      index.js
  2. 12
      test/stream.js

1
index.js

@ -165,6 +165,7 @@ function asStream(opts) {
var statusCode = res.statusCode; var statusCode = res.statusCode;
if (statusCode < 200 || statusCode > 299) { if (statusCode < 200 || statusCode > 299) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res); proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return;
} }
proxy.emit('response', res); proxy.emit('response', res);

12
test/stream.js

@ -98,18 +98,26 @@ test('stream - response event', t => {
}); });
test('stream - error event', t => { test('stream - error event', t => {
t.plan(4);
got.stream(`${s.url}/error`) got.stream(`${s.url}/error`)
.on('response', () => {
t.fail('response event should not be emitted');
})
.on('error', (err, data, res) => { .on('error', (err, data, res) => {
t.is(err.message, 'Response code 404 (Not Found)'); t.is(err.message, 'Response code 404 (Not Found)');
t.is(null, data); t.is(null, data);
t.ok(res); t.ok(res);
t.end();
}); });
});
test('stream - error event', t => {
got.stream('.com') got.stream('.com')
.on('response', () => {
t.fail('response event should not be emitted');
})
.on('error', err => { .on('error', err => {
t.regexTest(/getaddrinfo ENOTFOUND/, err.message); t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.end();
}); });
}); });

Loading…
Cancel
Save