Skip to content

Commit

Permalink
Throw exception if you are using all wrong headers (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Jul 17, 2021
1 parent 3f7640e commit a2b8dd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Client.php
Expand Up @@ -5,6 +5,7 @@
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 @@ -344,6 +345,9 @@ 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);
}
$modify['set_headers'] = $options['headers'];
unset($options['headers']);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ClientTest.php
Expand Up @@ -4,6 +4,7 @@

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 @@ -613,6 +614,26 @@ public function testSendSendsWithSync()
self::assertTrue($mock->getLastOptions()['synchronous']);
}

public function testSendWithInvalidHeader()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('GET', 'http://foo.com');

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

public function testSendWithInvalidHeaders()
{
$mock = new MockHandler([new Response()]);
$client = new Client(['handler' => $mock]);
$request = new Request('GET', 'http://foo.com');

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

public function testCanSetCustomHandler()
{
$mock = new MockHandler([new Response(500)]);
Expand Down

0 comments on commit a2b8dd1

Please sign in to comment.