Skip to content
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

Improve unserializable response handling #111

Merged
merged 6 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions src/Exceptions/CouldntUnserialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\ResponseCache\Exceptions;

use Exception;

class CouldntUnserialize extends Exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this to CouldNotUnserialize

{
public static function serializedResponse(string $serializedResponse): self
{
return new static("Couldn't unserialize `{$serializedResponse}`");
}
}
18 changes: 18 additions & 0 deletions src/ResponseSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\ResponseCache;

use Symfony\Component\HttpFoundation\Response;
use Spatie\ResponseCache\Exceptions\CouldntUnserialize;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ResponseSerializer
Expand All @@ -19,6 +20,10 @@ public function unserialize(string $serializedResponse): Response
{
$responseProperties = unserialize($serializedResponse);

if (! $this->containsValidResponseProperties($responseProperties)) {
throw CouldntUnserialize::serializedResponse($serializedResponse);
}

$response = $this->buildResponse($responseProperties);

$response->headers = $responseProperties['headers'];
Expand All @@ -44,6 +49,19 @@ protected function getResponseData(Response $response): array
return compact('statusCode', 'headers', 'content', 'type');
}

protected function containsValidResponseProperties($properties): bool
{
if (! is_array($properties)) {
return false;
}

if (! isset($properties['content'], $properties['statusCode'])) {
return false;
}

return true;
}

protected function buildResponse(array $responseProperties): Response
{
$type = $responseProperties['type'] ?? self::RESPONSE_TYPE_NORMAL;
Expand Down
9 changes: 9 additions & 0 deletions tests/ResponseSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\ResponseCache\ResponseSerializer;
use Symfony\Component\HttpFoundation\Response;
use Spatie\ResponseCache\Exceptions\CouldntUnserialize;

class ResponseSerializerTest extends TestCase
{
Expand Down Expand Up @@ -46,4 +47,12 @@ public function it_can_serialize_and_unserialize_a_response()

$this->assertEquals('testValue', $unserializedResponse->headers->get('testHeader'));
}

/** @test */
public function it_throws_when_something_something_else_than_a_response_is_unserialized()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to it_throws_an_exception_when_something_else_than_a_response_is_unserialized

{
$this->expectException(CouldntUnserialize::class);

$this->responseSerializer->unserialize('b:0;');
}
}