Skip to content

Trigger an E_USER_ERROR whenever a curl request fails (#90) #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
4 changes: 4 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions test/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
private $host;
/** @var array */
private $headers;
/** @var array */
private $errors;

protected function setUp()
{
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down