Skip to content

Commit

Permalink
Added option to toggle automatic following redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno authored and floatdrop committed Apr 6, 2016
1 parent db9f86d commit fc57c70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function requestAsEventEmitter(opts) {
var req = fn.request(opts, function (res) {
var statusCode = res.statusCode;

if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();

if (++redirectCount > 10) {
Expand Down Expand Up @@ -96,13 +96,14 @@ function asCallback(opts, cb) {
ee.on('response', function (res) {
readAllStream(res, opts.encoding, function (err, data) {
var statusCode = res.statusCode;
var limitStatusCode = opts.followRedirect ? 299 : 399;

if (err) {
cb(new got.ReadError(err, opts), null, res);
return;
}

if (statusCode < 200 || statusCode > 299) {
if (statusCode < 200 || statusCode > limitStatusCode) {
err = new got.HTTPError(statusCode, opts);
}

Expand Down Expand Up @@ -183,10 +184,11 @@ function asStream(opts) {

ee.on('response', function (res) {
var statusCode = res.statusCode;
var limitStatusCode = opts.followRedirect ? 299 : 399;

res.pipe(output);

if (statusCode < 200 || statusCode > 299) {
if (statusCode < 200 || statusCode > limitStatusCode) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return;
}
Expand Down Expand Up @@ -286,6 +288,10 @@ function normalizeArguments(url, opts) {
};
}

if (opts.followRedirect === undefined) {
opts.followRedirect = true;
}

return opts;
}

Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,17 @@ Function to be called when error or data are received. If omitted, a promise wil

`Error` object with HTTP status code as `statusCode` property.

<<<<<<< HEAD
###### data
=======
###### followRedirect

Type: `boolean`
Default: `true`

Defines if redirect responses should be followed automatically.

>>>>>>> b3cd961... Added option to toggle automatic following redirects
The data you requested.

Expand Down
4 changes: 4 additions & 0 deletions test/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ test('follows redirect', async t => {
t.is((await got(`${s.url}/finite`)).body, 'reached');
});

test('does not follow redirect when disabled', async t => {
t.is((await got(`${s.url}/finite`, {followRedirect: false})).statusCode, 302);
});

test('relative redirect works', async t => {
t.is((await got(`${s.url}/relative`)).body, 'reached');
});
Expand Down

0 comments on commit fc57c70

Please sign in to comment.