Skip to content
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

Throw InvalidArgumentException when an incorrect headers array is provided #2942

Merged
merged 2 commits into from Oct 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Client.php
Expand Up @@ -5,7 +5,6 @@
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\InvalidArgumentException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise as P;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -346,7 +345,7 @@ private function applyOptions(RequestInterface $request, array &$options): Reque

if (isset($options['headers'])) {
if (array_keys($options['headers']) === range(0, count($options['headers']) - 1)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: This could be array_is_list() in PHP 8.1 / by importing Symfony's symfony/polyfill-php81.

throw new RequestException('The headers array must have header name as keys.', $request);
throw new InvalidArgumentException('The headers array must have header name as keys.');
}
$modify['set_headers'] = $options['headers'];
unset($options['headers']);
Expand Down
5 changes: 2 additions & 3 deletions tests/ClientTest.php
Expand Up @@ -4,7 +4,6 @@

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -620,7 +619,7 @@ public function testSendWithInvalidHeader()
$client = new Client(['handler' => $mock]);
$request = new Request('GET', 'http://foo.com');

$this->expectException(RequestException::class);
$this->expectException(\GuzzleHttp\Exception\InvalidArgumentException::class);
$client->send($request, ['headers'=>['X-Foo: Bar']]);
}

Expand All @@ -630,7 +629,7 @@ public function testSendWithInvalidHeaders()
$client = new Client(['handler' => $mock]);
$request = new Request('GET', 'http://foo.com');

$this->expectException(RequestException::class);
$this->expectException(\GuzzleHttp\Exception\InvalidArgumentException::class);
$client->send($request, ['headers'=>['X-Foo: Bar', 'X-Test: Fail']]);
}

Expand Down