Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/router/src/GenericResponseSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function sendContent(Response $response): void
if ($response instanceof File || $response instanceof Download) {
readfile($body);
} elseif (is_array($body) || $body instanceof JsonSerializable) {
echo json_encode($body);
echo Json\encode($body);
} elseif ($body instanceof View) {
$this->renderView($response);
} else {
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Http/GenericResponseSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Tempest\Http\ServerSentMessage;
use Tempest\Http\Status;
use Tempest\Router\GenericResponseSender;
use Tempest\Support\Json\Exception\JsonCouldNotBeEncoded;
use Tempest\View\ViewRenderer;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

Expand Down Expand Up @@ -117,6 +118,21 @@ public function test_sending_of_array_to_json(): void
$this->assertSame('{"key":"value"}', $output);
}

public function test_sending_invalid_json_throws_exception(): void
{
ob_start();
$response = new GenericResponse(
status: Status::CREATED,
body: ['key' => "\xB1\x31"],
);

$responseSender = $this->container->get(GenericResponseSender::class);

$this->assertException(JsonCouldNotBeEncoded::class, fn () => $responseSender->send($response));

ob_get_clean();
}

public function test_sending_of_json_serializable_to_json(): void
{
ob_start();
Expand Down
Loading