Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

cast to (int) for Curl adapter timeout config #121

Merged
merged 3 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ public function connect($host, $port = 80, $secure = false)
} else {
$connectTimeout = null;
}

if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
throw new AdapterException\InvalidArgumentException(sprintf(
'integer or numeric string expected, got %s',
gettype($connectTimeout)
));
}

if ($connectTimeout !== null) {
$connectTimeout = (int) $connectTimeout;
}

if ($connectTimeout !== null) {
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000);
Expand Down
8 changes: 8 additions & 0 deletions src/Client/Adapter/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ public function connect($host, $port = 80, $secure = false)
} else {
$connectTimeout = $this->config['timeout'];
}

if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
throw new AdapterException\InvalidArgumentException(sprintf(
'integer or numeric string expected, got %s',
gettype($connectTimeout)
));
}

ErrorHandler::start();
$this->socket = stream_socket_client(
$host . ':' . $port,
Expand Down
30 changes: 30 additions & 0 deletions test/Client/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ public function testConfigSetAsZendConfig()
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
}

public function provideValidTimeoutConfig()
{
return [
'integer' => [10],
'numeric' => ['10'],
];
}

/**
* @dataProvider provideValidTimeoutConfig
*/
public function testPassValidTimeout($timeout)
{
$adapter = new Adapter\Curl();
$adapter->setOptions(['timeout' => $timeout]);

$adapter->connect('http://framework.zend.com');
}

public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
{
$adapter = new Adapter\Curl();
$adapter->setOptions(['timeout' => 'timeout']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('integer or numeric string expected, got string');

$adapter->connect('http://framework.zend.com');
}

/**
* Check that an exception is thrown when trying to set invalid config
*
Expand Down
30 changes: 30 additions & 0 deletions test/Client/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ public function testSetConfigInvalidConfig($config)
$this->_adapter->setOptions($config);
}

public function provideValidTimeoutConfig()
{
return [
'integer' => [10],
'numeric' => ['10'],
];
}

/**
* @dataProvider provideValidTimeoutConfig
*/
public function testPassValidTimeout($timeout)
{
$adapter = new Adapter\Socket();
$adapter->setOptions(['timeout' => $timeout]);

$adapter->connect('http://framework.zend.com');
}

public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
{
$adapter = new Adapter\Socket();
$adapter->setOptions(['timeout' => 'timeout']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('integer or numeric string expected, got string');

$adapter->connect('http://framework.zend.com');
}

/**
* Stream context related tests
*/
Expand Down