Browse Source

Merge pull request #31 from floatdrop/discard-before-throw

Discard response before throw
http2
Sindre Sorhus 10 years ago
parent
commit
5bf15e037d
  1. 4
      index.js
  2. 2
      test/test-redirects.js

4
index.js

@ -61,13 +61,13 @@ function got(url, opts, cb) {
// redirect // redirect
if (statusCode >= 300 && statusCode < 400 && 'location' in res.headers) { if (statusCode >= 300 && statusCode < 400 && 'location' in res.headers) {
res.resume(); // Discard response
if (++redirectCount > 10) { if (++redirectCount > 10) {
cb(new Error('Redirected 10 times. Aborting.'), undefined, res); cb(new Error('Redirected 10 times. Aborting.'), undefined, res);
return; return;
} }
res.resume(); // Discard response
get(urlLib.resolve(url, res.headers.location), opts, cb); get(urlLib.resolve(url, res.headers.location), opts, cb);
return; return;
} }

2
test/test-redirects.js

@ -54,7 +54,7 @@ tape('follows relative redirect', function (t) {
}); });
tape('throws on endless redirect', function (t) { tape('throws on endless redirect', function (t) {
got(s.url + '/endless', {agent: false}, function (err) { got(s.url + '/endless', function (err) {
t.ok(err, 'should get error'); t.ok(err, 'should get error');
t.equal(err.message, 'Redirected 10 times. Aborting.'); t.equal(err.message, 'Redirected 10 times. Aborting.');
t.end(); t.end();

Loading…
Cancel
Save