Skip to content

Commit

Permalink
Add maxRedirects option (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Nov 2, 2019
1 parent efcdb25 commit 0c505b0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -368,6 +368,13 @@ Defines if redirect responses should be followed automatically.

Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`. This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).

###### maxRedirects

Type: `number`<br>
Default: `10`

If exceeded, the request will be aborted and a `MaxRedirectsError` will be thrown.

###### decompress

Type: `boolean`<br>
Expand Down
4 changes: 2 additions & 2 deletions source/errors.ts
Expand Up @@ -76,8 +76,8 @@ export class HTTPError extends GotError {
export class MaxRedirectsError extends GotError {
readonly response!: Response;

constructor(response: Response, options: NormalizedOptions) {
super('Redirected 10 times. Aborting.', {}, options);
constructor(response: Response, maxRedirects: number, options: NormalizedOptions) {
super(`Redirected ${maxRedirects} times. Aborting.`, {}, options);
this.name = 'MaxRedirectsError';

Object.defineProperty(this, 'response', {
Expand Down
3 changes: 2 additions & 1 deletion source/index.ts
Expand Up @@ -55,7 +55,8 @@ const defaults: Partial<Defaults> = {
dnsCache: false,
useElectronNet: false,
responseType: 'text',
resolveBodyOnly: false
resolveBodyOnly: false,
maxRedirects: 10
},
mutableDefaults: false
};
Expand Down
4 changes: 2 additions & 2 deletions source/request-as-event-emitter.ts
Expand Up @@ -150,8 +150,8 @@ export default (options: NormalizedOptions, input?: TransformStream) => {
options.method = 'GET';
}

if (redirects.length >= 10) {
throw new MaxRedirectsError(typedResponse, options);
if (redirects.length >= options.maxRedirects) {
throw new MaxRedirectsError(typedResponse, options.maxRedirects, options);
}

// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
Expand Down
1 change: 1 addition & 0 deletions source/utils/types.ts
Expand Up @@ -161,6 +161,7 @@ export interface Options extends Except<https.RequestOptions, 'agent' | 'timeout
form?: Record<string, any>;
json?: Record<string, any>;
context?: {[key: string]: unknown};
maxRedirects?: number;
}

export interface NormalizedOptions extends Except<Required<Options>, 'timeout' | 'dnsCache' | 'retry' | 'auth' | 'body' | 'port'> {
Expand Down
14 changes: 14 additions & 0 deletions test/redirects.ts
Expand Up @@ -81,6 +81,20 @@ test('throws on endless redirects', withServer, async (t, server, got) => {
t.deepEqual(error.response.redirectUrls, new Array(10).fill(`${server.url}/`));
});

test('custom `maxRedirects` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: server.url
});
response.end();
});

const error = await t.throwsAsync(got('', {maxRedirects: 5}), 'Redirected 5 times. Aborting.');

// @ts-ignore
t.deepEqual(error.response.redirectUrls, new Array(5).fill(`${server.url}/`));
});

test('searchParams are not breaking redirects', withServer, async (t, server, got) => {
server.get('/', reachedHandler);

Expand Down

0 comments on commit 0c505b0

Please sign in to comment.