Skip to content

Commit

Permalink
Add adapter instance type check.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad authored and markstory committed Jun 7, 2018
1 parent 065556f commit 9dd3762
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Http/Client.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -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;

Expand All @@ -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.
*/
Expand All @@ -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'])) {
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Http/ClientTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;

/**
* HTTP client test.
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 9dd3762

Please sign in to comment.