Browse Source

Merge pull request #112 from sindresorhus/either-error-or-response

Emit either error or reponse event in stream mode
http2
Vsevolod Strukchinsky 9 years ago
parent
commit
698f39812a
  1. 1
      index.js
  2. 12
      test/stream.js

1
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);

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

Loading…
Cancel
Save