Skip to content

Commit

Permalink
Merge 86c8dba into 814bcac
Browse files Browse the repository at this point in the history
  • Loading branch information
RoobinGood committed Mar 30, 2016
2 parents 814bcac + 86c8dba commit 63b355f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function requestAsEventEmitter(opts) {
const req = fn.request(opts, res => {
const 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 @@ -268,6 +268,10 @@ function normalizeArguments(url, opts) {
};
}

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

return opts;
}

Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ Type: `number`

Milliseconds to wait for a server to send response headers before aborting request with `ETIMEDOUT` error.

###### followRedirect

Type: `boolean`
Default: `true`

Follow HTTP 3xx responses as redirects.

###### retries

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

test('do not follows redirect', async t => {
try {
await got(`${http.url}/finite`, {followRedirect: false});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.statusCode, 302);
}
});

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

0 comments on commit 63b355f

Please sign in to comment.