diff --git a/src/Symfony/Component/HttpClient/CurlHttpClient.php b/src/Symfony/Component/HttpClient/CurlHttpClient.php index 83413a23edf45..471f694b4e89f 100644 --- a/src/Symfony/Component/HttpClient/CurlHttpClient.php +++ b/src/Symfony/Component/HttpClient/CurlHttpClient.php @@ -220,7 +220,7 @@ public function request(string $method, string $url, array $options = []): Respo // Prevent curl from sending its default Accept and Expect headers foreach (['accept', 'expect'] as $header) { - if (!isset($options['normalized_headers'][$header])) { + if (!isset($options['normalized_headers'][$header][0])) { $curlopts[CURLOPT_HTTPHEADER][] = $header.':'; } } @@ -413,7 +413,7 @@ private static function createRedirectResolver(array $options, string $host): \C return 0 !== stripos($h, 'Host:'); }); - if (isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) { + if (isset($options['normalized_headers']['authorization'][0]) || isset($options['normalized_headers']['cookie'][0])) { $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) { return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'); }); diff --git a/src/Symfony/Component/HttpClient/HttpClientTrait.php b/src/Symfony/Component/HttpClient/HttpClientTrait.php index 3fd9814df68f0..ab5f3502f7c00 100644 --- a/src/Symfony/Component/HttpClient/HttpClientTrait.php +++ b/src/Symfony/Component/HttpClient/HttpClientTrait.php @@ -57,7 +57,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt } if (!isset($options['normalized_headers']['accept'])) { - $options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: *']; + $options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: */*']; } if (isset($options['body'])) { diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php index 3d5a70a0a96e5..bc836a7fade1b 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php @@ -19,4 +19,32 @@ public function testMaxDuration() { $this->markTestSkipped('Implemented as of version 4.4'); } + + public function testAcceptHeader() + { + $client = $this->getHttpClient(__FUNCTION__); + + $response = $client->request('GET', 'http://localhost:8057'); + $requestHeaders = $response->toArray(); + + $this->assertSame('*/*', $requestHeaders['HTTP_ACCEPT']); + + $response = $client->request('GET', 'http://localhost:8057', [ + 'headers' => [ + 'Accept' => 'foo/bar', + ], + ]); + $requestHeaders = $response->toArray(); + + $this->assertSame('foo/bar', $requestHeaders['HTTP_ACCEPT']); + + $response = $client->request('GET', 'http://localhost:8057', [ + 'headers' => [ + 'Accept' => null, + ], + ]); + $requestHeaders = $response->toArray(); + + $this->assertArrayNotHasKey('HTTP_ACCEPT', $requestHeaders); + } } diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php index 559f2b62270df..e1636a5d5f84d 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php @@ -172,7 +172,7 @@ public function provideRemoveDotSegments() public function testAuthBearerOption() { [, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS); - $this->assertSame(['Accept: *', 'Authorization: Bearer foobar'], $options['headers']); + $this->assertSame(['Accept: */*', 'Authorization: Bearer foobar'], $options['headers']); $this->assertSame(['Authorization: Bearer foobar'], $options['normalized_headers']['authorization']); } diff --git a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php index 8a3936b442f7a..32b4ebe4485a1 100644 --- a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php @@ -36,6 +36,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface "SERVER_NAME": "127.0.0.1", "REQUEST_URI": "/", "REQUEST_METHOD": "GET", + "HTTP_ACCEPT": "*/*", "HTTP_FOO": "baR", "HTTP_HOST": "localhost:8057" }'; @@ -113,6 +114,12 @@ protected function getHttpClient(string $testCase): HttpClientInterface $responses[] = $mock; break; + case 'testAcceptHeader': + $responses[] = new MockResponse($body, ['response_headers' => $headers]); + $responses[] = new MockResponse(str_replace('*/*', 'foo/bar', $body), ['response_headers' => $headers]); + $responses[] = new MockResponse(str_replace('"HTTP_ACCEPT": "*/*",', '', $body), ['response_headers' => $headers]); + break; + case 'testResolve': $responses[] = new MockResponse($body, ['response_headers' => $headers]); $responses[] = new MockResponse($body, ['response_headers' => $headers]);