Skip to content

Commit

Permalink
Merge pull request #215 from amrita-shrestha/remove-header-overwrite-…
Browse files Browse the repository at this point in the history
…with-null

Improve CURLOPT_HTTPHEADER Setting Assignment
  • Loading branch information
phil-davis authored Aug 17, 2023
2 parents 591a497 + 39bb6b0 commit 6a88b28
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ protected function createCurlSettingsArray(RequestInterface $request): array
$nHeaders[] = $key.': '.$value;
}
}
$settings[CURLOPT_HTTPHEADER] = $nHeaders;

if ([] !== $nHeaders) {
$settings[CURLOPT_HTTPHEADER] = $nHeaders;
}
$settings[CURLOPT_URL] = $request->getUrl();
// FIXME: CURLOPT_PROTOCOLS is currently unsupported by HHVM
if (defined('CURLOPT_PROTOCOLS')) {
Expand Down
27 changes: 27 additions & 0 deletions tests/HTTP/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ public function testCreateCurlSettingsArrayGET(): void
self::assertEquals($settings, $client->createCurlSettingsArray($request));
}

public function testCreateCurlSettingsHTTPHeader(): void
{
$client = new ClientMock();
$header = [
'Authorization: Bearer 12345',
];
$client->addCurlSetting(CURLOPT_POSTREDIR, 0);
$client->addCurlSetting(CURLOPT_HTTPHEADER, $header);

$request = new Request('GET', 'http://example.org/');

$settings = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_POSTREDIR => 0,
CURLOPT_HTTPHEADER => ['Authorization: Bearer 12345'],
CURLOPT_NOBODY => false,
CURLOPT_URL => 'http://example.org/',
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_USERAGENT => 'sabre-http/'.Version::VERSION.' (http://sabre.io/)',
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
];

self::assertEquals($settings, $client->createCurlSettingsArray($request));
}

public function testCreateCurlSettingsArrayHEAD(): void
{
$client = new ClientMock();
Expand Down

0 comments on commit 6a88b28

Please sign in to comment.