Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add new allowMaxRedirects method to PendingRequest #42902

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @method \Illuminate\Http\Client\PendingRequest contentType(string $contentType)
* @method \Illuminate\Http\Client\PendingRequest dd()
* @method \Illuminate\Http\Client\PendingRequest dump()
* @method \Illuminate\Http\Client\PendingRequest maxRedirects(int $max)
* @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true)
* @method \Illuminate\Http\Client\PendingRequest sink(string|resource $to)
* @method \Illuminate\Http\Client\PendingRequest stub(callable $callback)
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ public function withCookies(array $cookies, string $domain)
});
}

/**
* Specify the maximum number of redirects to allow.
*
* @param int $max
* @return $this
*/
public function maxRedirects(int $max)
{
return tap($this, function () use ($max) {
$this->options['allow_redirects']['max'] = $max;
});
}

/**
* Indicate that redirects should not be followed.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @method static \Illuminate\Http\Client\PendingRequest contentType(string $contentType)
* @method static \Illuminate\Http\Client\PendingRequest dd()
* @method static \Illuminate\Http\Client\PendingRequest dump()
* @method static \Illuminate\Http\Client\PendingRequest maxRedirects(int $max)
* @method static \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleepMilliseconds = 0, ?callable $when = null, bool $throw = true)
* @method static \Illuminate\Http\Client\ResponseSequence sequence(array $responses = [])
* @method static \Illuminate\Http\Client\PendingRequest sink(string|resource $to)
Expand Down
13 changes: 13 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1615,4 +1615,17 @@ public function testItCanAddAuthorizationHeaderIntoRequestUsingBeforeSendingCall
$request->hasHeader('Authorization', 'Bearer GET /json HTTP/1.1');
});
}

public function testItCanSetAllowMaxRedirects(): void
{
$request = new PendingRequest($this->factory);

$request = $request->withOptions(['allow_redirects' => ['max' => 5]]);

$this->assertSame(['connect_timeout' => 10, 'http_errors' => false, 'timeout' => 30, 'allow_redirects' => ['max' => 5]], $request->getOptions());

$request = $request->maxRedirects(10);

$this->assertSame(['connect_timeout' => 10, 'http_errors' => false, 'timeout' => 30, 'allow_redirects' => ['max' => 10]], $request->getOptions());
}
}