Skip to content

Commit fde3a52

Browse files
committed
Trigger an E_USER_ERROR whenever a curl request fails (#90)
1 parent e9a04d9 commit fde3a52

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ private function parseResponse($channel, $content)
415415
$headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
416416
$statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);
417417

418+
if ($statusCode === 0) {
419+
trigger_error(curl_error($channel), E_USER_ERROR);
420+
}
421+
418422
$responseBody = substr($content, $headerSize);
419423

420424
$responseHeaders = substr($content, 0, $headerSize);

test/unit/ClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
1212
private $host;
1313
/** @var array */
1414
private $headers;
15+
/** @var array */
16+
private $errors;
1517

1618
protected function setUp()
1719
{
@@ -21,6 +23,24 @@ protected function setUp()
2123
'Authorization: Bearer SG.XXXX'
2224
];
2325
$this->client = new MockClient($this->host, $this->headers, '/v3');
26+
$this->errors = [];
27+
set_error_handler([$this, 'errorHandler']);
28+
}
29+
30+
public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
31+
{
32+
$this->errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
33+
}
34+
35+
public function assertError($expectedErrorStringRegex, $expectedErrorNumber)
36+
{
37+
foreach ($this->errors as $error) {
38+
if ($error['errno'] === $expectedErrorNumber && preg_match($expectedErrorStringRegex, $error['errstr'])) {
39+
return $this->assertTrue(true);
40+
}
41+
}
42+
43+
$this->fail(sprintf('Error with level "%d" matching "%s" was not triggered', $expectedErrorNumber, $expectedErrorStringRegex));
2444
}
2545

2646
public function testConstructor()
@@ -195,6 +215,14 @@ public function testCreateCurlOptionsWithBodyAndHeaders()
195215
], $result);
196216
}
197217

218+
public function testMakeRequestWithUntrustedRootCert()
219+
{
220+
$client = new Client('https://untrusted-root.badssl.com/');
221+
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
222+
223+
$this->assertError('/certificate/i', E_USER_ERROR);
224+
}
225+
198226
/**
199227
* @param object $obj
200228
* @param string $name

0 commit comments

Comments
 (0)