mirror of
https://github.com/guzzle/guzzle.git
synced 2025-01-17 21:38:16 +01:00
Throw exception if you are using all wrong headers (#2916)
This commit is contained in:
parent
3f7640eb36
commit
a2b8dd1ad7
@ -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']);
|
||||
}
|
||||
|
@ -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)]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user