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

Commit

Permalink
Merge branch 'hotfix/121' into develop
Browse files Browse the repository at this point in the history
Forward port #121
  • Loading branch information
weierophinney committed Jan 8, 2019
2 parents 89188e5 + 7786e3f commit 80500df
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#121](https://github.com/zendframework/zend-http/pull/121) adds detection for non-numeric connection timeout values as well as
integer casting to ensure the timeout is set properly in both the Curl and
Socket adapters.

## 2.8.2 - 2018-08-13

Expand Down
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

0 comments on commit 80500df

Please sign in to comment.