Skip to content
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpClient/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ public function sendRequest(RequestInterface $request): ResponseInterface

foreach ($response->getHeaders(false) as $name => $values) {
foreach ($values as $value) {
$psrResponse = $psrResponse->withAddedHeader($name, $value);
try {
$psrResponse = $psrResponse->withAddedHeader($name, $value);
} catch (\InvalidArgumentException $e) {
// ignore invalid header
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\Psr18NetworkException;
use Symfony\Component\HttpClient\Psr18RequestException;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;

class Psr18ClientTest extends TestCase
Expand Down Expand Up @@ -81,4 +83,22 @@ public function test404()
$response = $client->sendRequest($factory->createRequest('GET', 'http://localhost:8057/404'));
$this->assertSame(404, $response->getStatusCode());
}

public function testInvalidHeaderResponse()
{
$responseHeaders = [
// space in header name not allowed in RFC 7230
' X-XSS-Protection' => '0',
'Cache-Control' => 'no-cache',
];
$response = new MockResponse('body', ['response_headers' => $responseHeaders]);
$this->assertArrayHasKey(' x-xss-protection', $response->getHeaders());

$client = new Psr18Client(new MockHttpClient($response));
$request = $client->createRequest('POST', 'http://localhost:8057/post')
->withBody($client->createStream('foo=0123456789'));

$resultResponse = $client->sendRequest($request);
$this->assertCount(1, $resultResponse->getHeaders());
}
}