Skip to content

Commit

Permalink
chore: fix followRedirect function option
Browse files Browse the repository at this point in the history
  • Loading branch information
Van Tigranyan committed Oct 26, 2023
1 parent 1845b74 commit 3e20f6f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
return;
}

if (options.followRedirect && response.headers.location && redirectCodes.has(statusCode)) {
if (response.headers.location && redirectCodes.has(statusCode)) {
// We're being redirected, we don't care about the response.
// It'd be best to abort the request, but we can't because
// we would have to sacrifice the TCP connection. We don't want that.
Expand Down Expand Up @@ -814,7 +814,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

// `HTTPError`s always have `error.response.body` defined.
// Therefore we cannot retry if `options.throwHttpErrors` is false.
// Therefore, we cannot retry if `options.throwHttpErrors` is false.
// On the last retry, if `options.throwHttpErrors` is false, we would need to return the body,
// but that wouldn't be possible since the body would be already read in `error.response.body`.
if (options.isStream && options.throwHttpErrors && !isResponseOk(typedResponse)) {
Expand Down
2 changes: 1 addition & 1 deletion source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ export default class Options {
}

set followRedirect(value: boolean | ((response: PlainResponse) => boolean)) {
assert.any([is.boolean, is.function], value);
assert.any([is.boolean, is.function_], value);

this._internals.followRedirect = value;
}
Expand Down
9 changes: 9 additions & 0 deletions test/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ test('follows redirect', withServer, async (t, server, got) => {
t.deepEqual(redirectUrls.map(String), [`${server.url}/`]);
});

test('follows redirect when followRedirect returns true', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/finite', finiteHandler);

const {body, redirectUrls} = await got('finite', {followRedirect: () => true});
t.is(body, 'reached');
t.deepEqual(redirectUrls.map(String), [`${server.url}/`]);
});

test('follows 307, 308 redirect', withServer, async (t, server, got) => {
server.get('/', reachedHandler);

Expand Down

0 comments on commit 3e20f6f

Please sign in to comment.