From 9dd3762b0869032a9d77d1da8dfb84257bd5e5c1 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 6 Jun 2018 03:22:43 +0530 Subject: [PATCH] Add adapter instance type check. --- src/Http/Client.php | 15 +++++++++++---- tests/TestCase/Http/ClientTest.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Http/Client.php b/src/Http/Client.php index 16dc8a23ff4..030d0c1a01f 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -16,6 +16,8 @@ use Cake\Core\App; use Cake\Core\Exception\Exception; use Cake\Core\InstanceConfigTrait; +use Cake\Http\Client\AdapterInterface; +use Cake\Http\Client\Adapter\Stream; use Cake\Http\Client\Request; use Cake\Http\Cookie\CookieCollection; use Cake\Http\Cookie\CookieInterface; @@ -104,7 +106,7 @@ class Client * @var array */ protected $_defaultConfig = [ - 'adapter' => 'Cake\Http\Client\Adapter\Stream', + 'adapter' => Stream::class, 'host' => null, 'port' => null, 'scheme' => 'http', @@ -127,10 +129,9 @@ class Client protected $_cookies; /** - * Adapter for sending requests. Defaults to - * Cake\Http\Client\Adapter\Stream + * Adapter for sending requests. * - * @var \Cake\Http\Client\Adapter\Stream + * @var \Cake\Http\Client\AdapterInterface */ protected $_adapter; @@ -154,6 +155,8 @@ class Client * - ssl_verify_host - Verify that the certificate and hostname match. * Defaults to true. * - redirect - Number of redirects to follow. Defaults to false. + * - adapter - The adapter class name or instance. Defaults to + * \Cake\Http\Client\Adapter\Stream * * @param array $config Config options for scoped clients. */ @@ -166,6 +169,10 @@ public function __construct($config = []) if (is_string($adapter)) { $adapter = new $adapter(); } + + if (!$adapter instanceof AdapterInterface) { + throw new InvalidArgumentException('Adapter must be an instance of Cake\Http\Client\AdapterInterface'); + } $this->_adapter = $adapter; if (!empty($this->_config['cookieJar'])) { diff --git a/tests/TestCase/Http/ClientTest.php b/tests/TestCase/Http/ClientTest.php index ac452ea3144..7db65462e8e 100644 --- a/tests/TestCase/Http/ClientTest.php +++ b/tests/TestCase/Http/ClientTest.php @@ -19,6 +19,7 @@ use Cake\Http\Cookie\Cookie; use Cake\Http\Cookie\CookieCollection; use Cake\TestSuite\TestCase; +use InvalidArgumentException; /** * HTTP client test. @@ -59,6 +60,19 @@ public function testConstructConfig() } } + /** + * testAdapterInstanceCheck + * + * @return void + */ + public function testAdapterInstanceCheck() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Adapter must be an instance of Cake\Http\Client\AdapterInterface'); + + new Client(['adapter' => 'stdClass']); + } + /** * Data provider for buildUrl() tests *