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

Commit e32cd63

Browse files
committed
Merge pull request #121 from samsonasik/curl-timeout-int
cast to (int) for Curl adapter timeout config
2 parents 697579b + 4dc2c30 commit e32cd63

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

src/Client/Adapter/Curl.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ public function connect($host, $port = 80, $secure = false)
208208
} else {
209209
$connectTimeout = null;
210210
}
211+
212+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
213+
throw new AdapterException\InvalidArgumentException(sprintf(
214+
'integer or numeric string expected, got %s',
215+
gettype($connectTimeout)
216+
));
217+
}
218+
219+
if ($connectTimeout !== null) {
220+
$connectTimeout = (int) $connectTimeout;
221+
}
222+
211223
if ($connectTimeout !== null) {
212224
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
213225
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000);

src/Client/Adapter/Socket.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ public function connect($host, $port = 80, $secure = false)
262262
} else {
263263
$connectTimeout = $this->config['timeout'];
264264
}
265+
266+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
267+
throw new AdapterException\InvalidArgumentException(sprintf(
268+
'integer or numeric string expected, got %s',
269+
gettype($connectTimeout)
270+
));
271+
}
272+
265273
ErrorHandler::start();
266274
$this->socket = stream_socket_client(
267275
$host . ':' . $port,

test/Client/CurlTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ public function testConfigSetAsZendConfig()
9696
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
9797
}
9898

99+
public function provideValidTimeoutConfig()
100+
{
101+
return [
102+
'integer' => [10],
103+
'numeric' => ['10'],
104+
];
105+
}
106+
107+
/**
108+
* @dataProvider provideValidTimeoutConfig
109+
*/
110+
public function testPassValidTimeout($timeout)
111+
{
112+
$adapter = new Adapter\Curl();
113+
$adapter->setOptions(['timeout' => $timeout]);
114+
115+
$adapter->connect('http://framework.zend.com');
116+
}
117+
118+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
119+
{
120+
$adapter = new Adapter\Curl();
121+
$adapter->setOptions(['timeout' => 'timeout']);
122+
123+
$this->expectException(InvalidArgumentException::class);
124+
$this->expectExceptionMessage('integer or numeric string expected, got string');
125+
126+
$adapter->connect('http://framework.zend.com');
127+
}
128+
99129
/**
100130
* Check that an exception is thrown when trying to set invalid config
101131
*

test/Client/SocketTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ public function testSetConfigInvalidConfig($config)
171171
$this->_adapter->setOptions($config);
172172
}
173173

174+
public function provideValidTimeoutConfig()
175+
{
176+
return [
177+
'integer' => [10],
178+
'numeric' => ['10'],
179+
];
180+
}
181+
182+
/**
183+
* @dataProvider provideValidTimeoutConfig
184+
*/
185+
public function testPassValidTimeout($timeout)
186+
{
187+
$adapter = new Adapter\Socket();
188+
$adapter->setOptions(['timeout' => $timeout]);
189+
190+
$adapter->connect('http://framework.zend.com');
191+
}
192+
193+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
194+
{
195+
$adapter = new Adapter\Socket();
196+
$adapter->setOptions(['timeout' => 'timeout']);
197+
198+
$this->expectException(InvalidArgumentException::class);
199+
$this->expectExceptionMessage('integer or numeric string expected, got string');
200+
201+
$adapter->connect('http://framework.zend.com');
202+
}
203+
174204
/**
175205
* Stream context related tests
176206
*/

0 commit comments

Comments
 (0)