[HttpKernel] Do not override max_redirects option in HttpClientKernel

This commit is contained in:
David Molineus 2020-09-16 17:27:48 +02:00 committed by Fabien Potencier
parent cae08a0279
commit 981a11beed
3 changed files with 48 additions and 2 deletions

View File

@ -35,7 +35,7 @@ final class HttpClientKernel implements HttpKernelInterface
public function __construct(HttpClientInterface $client = null)
{
if (!class_exists(HttpClient::class)) {
if (null === $client && !class_exists(HttpClient::class)) {
throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
}
@ -53,7 +53,6 @@ final class HttpClientKernel implements HttpKernelInterface
$response = $this->client->request($request->getMethod(), $request->getUri(), [
'headers' => $headers,
'body' => $body,
'max_redirects' => 0,
] + $request->attributes->get('http_client_options', []));
$response = new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch));

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpClientKernel;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class HttpClientKernelTest extends TestCase
{
public function testHandlePassesMaxRedirectsHttpClientOption()
{
$request = new Request();
$request->attributes->set('http_client_options', ['max_redirects' => 50]);
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();
$response->expects($this->once())->method('getStatusCode')->willReturn(200);
$client = $this->getMockBuilder(HttpClientInterface::class)->getMock();
$client
->expects($this->once())
->method('request')
->willReturnCallback(function (string $method, string $uri, array $options) use ($request, $response) {
$this->assertSame($request->getMethod(), $method);
$this->assertSame($request->getUri(), $uri);
$this->assertArrayHasKey('max_redirects', $options);
$this->assertSame(50, $options['max_redirects']);
return $response;
});
$kernel = new HttpClientKernel($client);
$kernel->handle($request);
}
}

View File

@ -19,6 +19,7 @@
"php": ">=7.1.3",
"symfony/error-handler": "^4.4",
"symfony/event-dispatcher": "^4.4",
"symfony/http-client-contracts": "^1.1|^2",
"symfony/http-foundation": "^4.4|^5.0",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-php73": "^1.9",