Skip to content

[HttpKernel] Do not override max_redirects option in HttpClientKernel #38212

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

Merged
merged 1 commit into from
Sep 17, 2020
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
3 changes: 1 addition & 2 deletions src/Symfony/Component/HttpKernel/HttpClientKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__));
}

Expand All @@ -53,7 +53,6 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
$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));
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpClientKernelTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down