8000 [HttpClient] send `Accept: */*` by default, fix removing it when needed · symfony/symfony@aff4e56 · GitHub
[go: up one dir, main page]

Skip to content

Commit aff4e56

Browse files
[HttpClient] send Accept: */* by default, fix removing it when needed
1 parent 1292d8d commit aff4e56

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function request(string $method, string $url, array $options = []): Respo
220220

221221
// Prevent curl from sending its default Accept and Expect headers
222222
foreach (['accept', 'expect'] as $header) {
223-
if (!isset($options['normalized_headers'][$header])) {
223+
if (!isset($options['normalized_headers'][$header][0])) {
224224
$curlopts[CURLOPT_HTTPHEADER][] = $header.':';
225225
}
226226
}
@@ -413,7 +413,7 @@ private static function createRedirectResolver(array $options, string $host): \C
413413
return 0 !== stripos($h, 'Host:');
414414
});
415415

416-
if (isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) {
416+
if (isset($options['normalized_headers']['authorization'][0]) || isset($options['normalized_headers']['cookie'][0])) {
417417
$redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
418418
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
419419
});

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
5757
}
5858

5959
if (!isset($options['normalized_headers']['accept'])) {
60-
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: *'];
60+
$options['normalized_headers']['accept'] = [$options['headers'][] = 'Accept: */*'];
6161
}
6262

6363
if (isset($options['body'])) {

src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,32 @@ public function testMaxDuration()
1919
{
2020
$this->markTestSkipped('Implemented as of version 4.4');
2121
}
22+
23+
public function testAcceptHeader()
24+
{
25+
$client = $this->getHttpClient(__FUNCTION__);
26+
27+
$response = $client->request('GET', 'http://localhost:8057');
28+
$requestHeaders = $response->toArray();
29+
30+
$this->assertSame('*/*', $requestHeaders['HTTP_ACCEPT']);
31+
32+
$response = $client->request('GET', 'http://localhost:8057', [
33+
'headers' => [
34+
'Accept' => 'foo/bar',
35+
],
36+
]);
37+
$requestHeaders = $response->toArray();
38+
39+
$this->assertSame('foo/bar', $requestHeaders['HTTP_ACCEPT']);
40+
41+
$response = $client->request('GET', 'http://localhost:8057', [
42+
'headers' => [
43+
'Accept' => null,
44+
],
45+
]);
46+
$requestHeaders = $response->toArray();
47+
48+
$this->assertArrayNotHasKey('HTTP_ACCEPT', $requestHeaders);
49+
}
2250
}

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function provideRemoveDotSegments()
172172
public function testAuthBearerOption()
173173
{
174174
[, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
175-
$this->assertSame(['Accept: *', 'Authorization: Bearer foobar'], $options['headers']);
175+
$this->assertSame(['Accept: */*', 'Authorization: Bearer foobar'], $options['headers']);
176176
$this->assertSame(['Authorization: Bearer foobar'], $options['normalized_headers']['authorization']);
177177
}
178178

src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
3636
"SERVER_NAME": "127.0.0.1",
3737
"REQUEST_URI": "/",
3838
"REQUEST_METHOD": "GET",
39+
"HTTP_ACCEPT": "*/*",
3940
"HTTP_FOO": "baR",
4041
"HTTP_HOST": "localhost:8057"
4142
}';
@@ -113,6 +114,12 @@ protected function getHttpClient(string $testCase): HttpClientInterface
113114
$responses[] = $mock;
114115
break;
115116

117+
case 'testAcceptHeader':
118+
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
119+
$responses[] = new MockResponse(str_replace('*/*', 'foo/bar', $body), ['response_headers' => $headers]);
120+
$responses[] = new MockResponse(str_replace('"HTTP_ACCEPT": "*/*",', '', $body), ['response_headers' => $headers]);
121+
break;
122+
116123
case 'testResolve':
117124
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
118125
$responses[] = new MockResponse($body, ['response_headers' => $headers]);

0 commit comments

Comments
 (0)
0