Skip to content

Commit

Permalink
[9.x] Add new allowMaxRedirects method to PendingRequest (laravel#42902)
Browse files Browse the repository at this point in the history
* Add new allowMaxRedirects method to PendingRequest

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
2 people authored and turanjanin committed Jun 23, 2022
1 parent 558ea9c commit edcc238
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
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());
}
}

0 comments on commit edcc238

Please sign in to comment.