Skip to content

Commit

Permalink
Throw InvalidArgumentException when an incorrect headers array is…
Browse files Browse the repository at this point in the history
… provided (#2942)

* Throw `InvalidArgumentException` when an incorrect `headers` array is provided

As discussed in PR #2916 after it was merged an `InvalidArgumentException` is
more fitting, as passing an invalid `headers` array is a clear programming
error that needs to be fixed and not caught.

This is consistent with the validation of the other options, e.g. when using
`multipart` and `form_params` at the same time.

see #2916
see a2b8dd1

* Remove obsolete import in Client.php / ClientTest.php
  • Loading branch information
TimWolla committed Oct 17, 2021
1 parent eeac96d commit 399c0ea
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
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)) {
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

0 comments on commit 399c0ea

Please sign in to comment.