Skip to content

Commit

Permalink
Merge pull request #4 from php-api-clients/content-type-check
Browse files Browse the repository at this point in the history
Content-Type checks
  • Loading branch information
WyriHaximus committed Jul 30, 2017
2 parents 2715b99 + cc062ee commit 95b2cc4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/JsonDecodeMiddleware.php
Expand Up @@ -48,6 +48,16 @@ public function post(
return resolve($response);
}

if (!isset($options[self::class]) &&
$response->getHeaderLine('Content-Type') !== 'application/json') {
return resolve($response);
}

if (isset($options[self::class][Options::CONTENT_TYPE]) &&
$response->getHeaderLine('Content-Type') !== $options[self::class][Options::CONTENT_TYPE]) {
return resolve($response);
}

$body = (string)$response->getBody();
if ($body === '') {
$stream = new BufferStream(0);
Expand Down
9 changes: 9 additions & 0 deletions src/Options.php
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace ApiClients\Middleware\Json;

final class Options
{
const CONTENT_TYPE = 'CONTENT_TYPE';
const NO_CONTENT_TYPE_CHECK = 'NO_CONTENT_TYPE_CHECK';
}
75 changes: 74 additions & 1 deletion tests/JsonDecodeMiddlewareTest.php
Expand Up @@ -4,6 +4,7 @@

use ApiClients\Middleware\Json\JsonStream;
use ApiClients\Middleware\Json\JsonDecodeMiddleware;
use ApiClients\Middleware\Json\Options;
use ApiClients\Tools\Json\JsonDecodeService;
use ApiClients\Tools\TestUtilities\TestCase;
use Clue\React\Buzz\Message\ReadableBodyStream;
Expand All @@ -19,7 +20,7 @@ public function testPost()
$loop = Factory::create();
$service = new JsonDecodeService($loop);
$middleware = new JsonDecodeMiddleware($service);
$response = new Response(200, [], '[]');
$response = new Response(200, ['Content-Type' => 'application/json'], '[]');

$body = await(
$middleware->post($response, 'abc'),
Expand All @@ -34,6 +35,78 @@ public function testPost()
);
}

public function testPostNoContentType()
{
$loop = Factory::create();
$service = new JsonDecodeService($loop);
$middleware = new JsonDecodeMiddleware($service);
$response = new Response(200, [], '[]');

self::assertSame(
$response,
await(
$middleware->post($response, 'abc'),
$loop
)
);
}

public function testPostNoContentTypeCheck()
{
$loop = Factory::create();
$service = new JsonDecodeService($loop);
$middleware = new JsonDecodeMiddleware($service);
$response = new Response(200, [], '[]');

$body = await(
$middleware->post(
$response,
'abc',
[
JsonDecodeMiddleware::class => [
Options::NO_CONTENT_TYPE_CHECK => true,
],
]
),
$loop
)->getBody();

self::assertInstanceOf(JsonStream::class, $body);

self::assertSame(
[],
$body->getJson()
);
}

public function testPostCustomTYpe()
{
$loop = Factory::create();
$service = new JsonDecodeService($loop);
$middleware = new JsonDecodeMiddleware($service);
$response = new Response(200, ['Content-Type' => 'custom/type'], '[]');

$body = await(
$middleware->post(
$response,
'abc',
[
JsonDecodeMiddleware::class => [
Options::CONTENT_TYPE => 'custom/type',
],
]
),
$loop
)->getBody();

self::assertInstanceOf(JsonStream::class, $body);

self::assertSame(
[],
$body->getJson()
);
}

public function testPostNoJson()
{
$loop = Factory::create();
Expand Down

0 comments on commit 95b2cc4

Please sign in to comment.