1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-17 13:28:13 +01:00

Throw exception if you are using all wrong headers (#2916)

This commit is contained in:
Tobias Nyholm 2021-07-17 15:23:23 -07:00 committed by GitHub
parent 3f7640eb36
commit a2b8dd1ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace GuzzleHttp;
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;
@ -344,6 +345,9 @@ class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
];
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']);
}

View File

@ -4,6 +4,7 @@ namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
@ -613,6 +614,26 @@ class ClientTest extends TestCase
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)]);