Skip to content

Commit 0629b6a

Browse files
Merge branch '4.3' into 4.4
* 4.3: [travis] Fix build-packages script [HttpClient] bugfix exploding values of headers Remove useless testCanCheckIfTerminalIsInteractive test case [Validator] Add the missing translations for the Thai (\"th\") locale [Routing] gracefully handle docref_root ini setting [Validator] Fix ValidValidator group cascading usage
2 parents 02b5495 + 69d4382 commit 0629b6a

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

CachingHttpClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,20 @@ public function request(string $method, string $url, array $options = []): Respo
7676
$request = Request::create($url, $method);
7777
$request->attributes->set('http_client_options', $options);
7878

79-
foreach ($options['headers'] as $name => $values) {
79+
foreach ($options['normalized_headers'] as $name => $values) {
8080
if ('cookie' !== $name) {
81-
$request->headers->set($name, $values);
81+
foreach ($values as $value) {
82+
$request->headers->set($name, substr($value, 2 + \strlen($name)), false);
83+
}
84+
8285
continue;
8386
}
8487

8588
foreach ($values as $cookies) {
86-
foreach (explode('; ', $cookies) as $cookie) {
89+
foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) {
8790
if ('' !== $cookie) {
8891
$cookie = explode('=', $cookie, 2);
89-
$request->cookies->set($cookie[0], $cookie[1] ?? null);
92+
$request->cookies->set($cookie[0], $cookie[1] ?? '');
9093
}
9194
}
9295
}

HttpClientTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,21 @@ private static function normalizeHeaders(array $headers): array
199199
$normalizedHeaders = [];
200200

201201
foreach ($headers as $name => $values) {
202+
if (\is_object($values) && method_exists('__toString')) {
203+
$values = (string) $values;
204+
}
205+
202206
if (\is_int($name)) {
207+
if (!\is_string($values)) {
208+
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \gettype($values)));
209+
}
203210
[$name, $values] = explode(':', $values, 2);
204211
$values = [ltrim($values)];
205212
} elseif (!is_iterable($values)) {
213+
if (\is_object($values)) {
214+
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, %s given.', $name, \get_class($values)));
215+
}
216+
206217
$values = (array) $values;
207218
}
208219

Tests/CachingHttpClientTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\CachingHttpClient;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
use Symfony\Component\HttpKernel\HttpCache\Store;
19+
20+
class CachingHttpClientTest extends TestCase
21+
{
22+
public function testRequestHeaders()
23+
{
24+
$options = [
25+
'headers' => [
26+
'Application-Name' => 'test1234',
27+
'Test-Name-Header' => 'test12345',
28+
],
29+
];
30+
31+
$mockClient = new MockHttpClient();
32+
$store = new Store(sys_get_temp_dir().'/sf_http_cache');
33+
$client = new CachingHttpClient($mockClient, $store, $options);
34+
35+
$response = $client->request('GET', 'http://example.com/foo-bar');
36+
37+
rmdir(sys_get_temp_dir().'/sf_http_cache');
38+
self::assertInstanceOf(MockResponse::class, $response);
39+
self::assertSame($response->getRequestOptions()['normalized_headers']['application-name'][0], 'Application-Name: test1234');
40+
self::assertSame($response->getRequestOptions()['normalized_headers']['test-name-header'][0], 'Test-Name-Header: test12345');
41+
}
42+
}

0 commit comments

Comments
 (0)