8000 [HttpClient] send `Accept: */*` by default, fix removing it when needed by nicolas-grekas · Pull Request #33935 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] send Accept: */* by default, fix removing it when needed #33935

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
Oct 10, 2019
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.':';
}
}
Expand Down Expand Up @@ -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:');
});
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}';
Expand Down Expand Up @@ -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]);
Expand Down
0