-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from cloudflare/apalaistras/fix-request-error…
…-handling Adapter/Guzzle: Fix error handling for v4 API
- Loading branch information
Showing
4 changed files
with
131 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
use Cloudflare\API\Adapter\ResponseException; | ||
use Cloudflare\API\Adapter\JSONException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
class ResponseExceptionTest extends TestCase | ||
{ | ||
public function testFromRequestExceptionNoResponse() | ||
{ | ||
$reqErr = new RequestException('foo', new Request('GET', '/test')); | ||
$respErr = ResponseException::fromRequestException($reqErr); | ||
|
||
$this->assertInstanceOf(ResponseException::class, $respErr); | ||
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); | ||
$this->assertEquals(0, $respErr->getCode()); | ||
$this->assertEquals($reqErr, $respErr->getPrevious()); | ||
} | ||
|
||
public function testFromRequestExceptionEmptyContentType() | ||
{ | ||
$resp = new Response(404); | ||
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); | ||
$respErr = ResponseException::fromRequestException($reqErr); | ||
|
||
$this->assertInstanceOf(ResponseException::class, $respErr); | ||
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); | ||
$this->assertEquals(0, $respErr->getCode()); | ||
$this->assertEquals($reqErr, $respErr->getPrevious()); | ||
} | ||
|
||
|
||
public function testFromRequestExceptionUnknownContentType() | ||
{ | ||
$resp = new Response(404, ['Content-Type' => ['application/octet-stream']]); | ||
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); | ||
$respErr = ResponseException::fromRequestException($reqErr); | ||
|
||
$this->assertInstanceOf(ResponseException::class, $respErr); | ||
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); | ||
$this->assertEquals(0, $respErr->getCode()); | ||
$this->assertEquals($reqErr, $respErr->getPrevious()); | ||
} | ||
|
||
public function testFromRequestExceptionJSONDecodeError() | ||
{ | ||
$resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], '[what]'); | ||
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); | ||
$respErr = ResponseException::fromRequestException($reqErr); | ||
|
||
$this->assertInstanceOf(ResponseException::class, $respErr); | ||
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage()); | ||
$this->assertEquals(0, $respErr->getCode()); | ||
$this->assertInstanceOf(JSONException::class, $respErr->getPrevious()); | ||
$this->assertEquals($reqErr, $respErr->getPrevious()->getPrevious()); | ||
} | ||
|
||
public function testFromRequestExceptionJSONWithErrors() | ||
{ | ||
$body = '{ | ||
"result": null, | ||
"success": false, | ||
"errors": [{"code":1003, "message":"This is an error"}], | ||
"messages": [] | ||
}'; | ||
|
||
$resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], $body); | ||
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp); | ||
$respErr = ResponseException::fromRequestException($reqErr); | ||
|
||
$this->assertInstanceOf(ResponseException::class, $respErr); | ||
$this->assertEquals('This is an error', $respErr->getMessage()); | ||
$this->assertEquals(1003, $respErr->getCode()); | ||
$this->assertEquals($reqErr, $respErr->getPrevious()); | ||
} | ||
} |