Skip to content

Commit

Permalink
Merge pull request #3 from DoclerLabs/remove-specific-payload-handlin…
Browse files Browse the repository at this point in the history
…g-in-response-handler

Removed data key handling from ResponseHandler when handling payload
  • Loading branch information
Mikhail Bakulin authored Aug 31, 2020
2 parents b51a5bc + 9f58a02 commit 859d4ca
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.0.0] - 2020-08-31
### Changed
- The response handling in `ResponseHandler`: Removed "data" key handling when handling payload.

## [2.0.1] - 2020-08-24
### Fixed
- The superkey "data" handling in ResponseHandler.
Expand Down
45 changes: 24 additions & 21 deletions src/Response/Handler/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DoclerLabs\ApiClientBase\Json\JsonException;
use DoclerLabs\ApiClientBase\Response\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class ResponseHandler implements ResponseHandlerInterface
{
Expand All @@ -28,45 +29,47 @@ class ResponseHandler implements ResponseHandlerInterface
*/
public function handle(ResponseInterface $response): Response
{
$statusCode = $response->getStatusCode();
$body = $response->getBody();
$headers = $response->getHeaders();
$statusCode = $response->getStatusCode();
$body = $response->getBody();
$headers = $response->getHeaders();
$isResponseBodyEmpty = $this->isResponseBodyEmpty($body);
$responseBody = '';

if (!$isResponseBodyEmpty) {
$responseBody = (string)$body;
}

if ($statusCode >= 200 && $statusCode < 300) {
if ($body === null || (int)$body->getSize() === 0) {
return new Response($statusCode, [], $headers);
if ($isResponseBodyEmpty) {
return new Response($statusCode, $headers);
}

$payload = Json::decode($body->__toString(), true, 512, self::JSON_OPTIONS);
$decodedPayload = Json::decode($responseBody, true, 512, self::JSON_OPTIONS);

if (isset($payload['data']) && count(array_keys($payload)) === 1) {
$payload = $payload['data'];
}

return new Response($statusCode, $payload, $headers);
}

$errorPayload = '';
if ($body !== null) {
$errorPayload = (string)$body;
return new Response($statusCode, $headers, $decodedPayload);
}

if ($statusCode === 400) {
throw new BadRequestResponseException($errorPayload);
throw new BadRequestResponseException($responseBody);
}

if ($statusCode === 401) {
throw new UnauthorizedResponseException($errorPayload);
throw new UnauthorizedResponseException($responseBody);
}

if ($statusCode === 403) {
throw new ForbiddenResponseException($errorPayload);
throw new ForbiddenResponseException($responseBody);
}

if ($statusCode === 404) {
throw new NotFoundResponseException($errorPayload);
throw new NotFoundResponseException($responseBody);
}

throw new UnexpectedResponseException($statusCode, $errorPayload);
throw new UnexpectedResponseException($statusCode, $responseBody);
}

private function isResponseBodyEmpty(StreamInterface $responseBody = null): bool
{
return $responseBody === null || (int)$responseBody->getSize() === 0;
}
}
18 changes: 9 additions & 9 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ class Response
private $statusCode;

/** @var array */
private $payload;
private $headers;

/** @var array */
private $headers;
private $payload;

public function __construct(int $statusCode, array $payload = [], array $headers = [])
public function __construct(int $statusCode, array $headers = [], $payload = null)
{
$this->statusCode = $statusCode;
$this->payload = $payload;
$this->headers = $headers;
$this->payload = $payload;
}

public function getStatusCode(): int
{
return $this->statusCode;
}

public function getPayload(): array
public function getHeaders(): array
{
return $this->payload;
return $this->headers;
}

public function getHeaders(): array
public function getPayload()
{
return $this->headers;
return $this->payload;
}
}
}
20 changes: 15 additions & 5 deletions test/suite/unit/Response/Handler/ResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ResponseHandlerTest extends TestCase
{
/**
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testResponse()
{
Expand Down Expand Up @@ -59,6 +60,7 @@ public function testResponse()

/**
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testResponseWithDataAndExtraFields()
{
Expand Down Expand Up @@ -98,6 +100,7 @@ public function testResponseWithDataAndExtraFields()

/**
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testResponseWithData()
{
Expand All @@ -107,7 +110,7 @@ public function testResponseWithData()
$testRawBody = '{"data":{"test-key":"test-value"}}';
$testHeaders = ['X-Foo' => 'bar'];
$testBodySize = 10;
$expectedBody = ['test-key' => 'test-value'];
$expectedBody = ['data' => ['test-key' => 'test-value']];
$expectedHeaders = $testHeaders;

$stream = $this->createMock(StreamInterface::class);
Expand All @@ -131,12 +134,13 @@ public function testResponseWithData()

$result = $handler->handle($response);

self::assertEquals($expectedBody, $result->getPayload());
self::assertEquals($expectedHeaders, $result->getHeaders());
self::assertEquals($expectedBody, $result->getPayload());
}

/**
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testResponseEmpty()
{
Expand All @@ -157,12 +161,13 @@ public function testResponseEmpty()

$result = $handler->handle($response);

self::assertEquals([], $result->getPayload());
self::assertEquals([], $result->getHeaders());
self::assertNull($result->getPayload());
}

/**
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testResponseEmptyWhenBodySizeIsEmpty()
{
Expand All @@ -189,13 +194,14 @@ public function testResponseEmptyWhenBodySizeIsEmpty()

$result = $handler->handle($response);

self::assertEquals([], $result->getPayload());
self::assertEquals([], $result->getHeaders());
self::assertNull($result->getPayload());
}

/**
* @dataProvider exceptionsDataProvider
* @covers ::handle
* @covers ::isResponseBodyEmpty
*/
public function testHttpError(int $testStatusCode, string $exceptionClassName)
{
Expand All @@ -207,9 +213,13 @@ public function testHttpError(int $testStatusCode, string $exceptionClassName)
->willReturn($testStatusCode);

$body = $this->createMock(StreamInterface::class);
$body->expects(self::once())
->method('getSize')
->willReturn(20);

$body->expects(self::once())
->method('__toString')
->willReturn('');
->willReturn(json_encode(['error' => 'something went wrong']));

$response->expects(self::once())
->method('getBody')
Expand Down
6 changes: 3 additions & 3 deletions test/suite/unit/Response/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function testFilled()
{
$body = ['foo'];
$headers = ['bar'];
$responseData = new Response(2000, $body, $headers);
$responseData = new Response(2000, $headers, $body);

self::assertSame(2000, $responseData->getStatusCode());
self::assertSame($body, $responseData->getPayload());
self::assertSame($headers, $responseData->getHeaders());
self::assertSame($body, $responseData->getPayload());
}
}
}

0 comments on commit 859d4ca

Please sign in to comment.