diff --git a/lib/Client.php b/lib/Client.php index ec732b9..ad94cd3 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -415,6 +415,10 @@ private function parseResponse($channel, $content) $headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE); $statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE); + if ($statusCode === 0) { + trigger_error(curl_error($channel), E_USER_ERROR); + } + $responseBody = substr($content, $headerSize); $responseHeaders = substr($content, 0, $headerSize); diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 538b1b0..148a309 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -12,6 +12,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase private $host; /** @var array */ private $headers; + /** @var array */ + private $errors; protected function setUp() { @@ -21,6 +23,24 @@ protected function setUp() 'Authorization: Bearer SG.XXXX' ]; $this->client = new MockClient($this->host, $this->headers, '/v3'); + $this->errors = []; + set_error_handler([$this, 'errorHandler']); + } + + public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) + { + $this->errors[] = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext'); + } + + public function assertError($expectedErrorStringRegex, $expectedErrorNumber) + { + foreach ($this->errors as $error) { + if ($error['errno'] === $expectedErrorNumber && preg_match($expectedErrorStringRegex, $error['errstr'])) { + return $this->assertTrue(true); + } + } + + $this->fail(sprintf('Error with level "%d" matching "%s" was not triggered', $expectedErrorNumber, $expectedErrorStringRegex)); } public function testConstructor() @@ -195,6 +215,14 @@ public function testCreateCurlOptionsWithBodyAndHeaders() ], $result); } + public function testMakeRequestWithUntrustedRootCert() + { + $client = new Client('https://untrusted-root.badssl.com/'); + $client->makeRequest('GET', 'https://untrusted-root.badssl.com/'); + + $this->assertError('/certificate/i', E_USER_ERROR); + } + /** * @param object $obj * @param string $name