Skip to content

Commit 97a7811

Browse files
fix(router): use Json\encode for encoding when sending json responses (#2199)
1 parent 542701d commit 97a7811

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

packages/router/src/GenericResponseSender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function sendContent(Response $response): void
104104
if ($response instanceof File || $response instanceof Download) {
105105
readfile($body);
106106
} elseif (is_array($body) || $body instanceof JsonSerializable) {
107-
echo json_encode($body);
107+
echo Json\encode($body);
108108
} elseif ($body instanceof View) {
109109
$this->renderView($response);
110110
} else {

tests/Integration/Http/GenericResponseSenderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Tempest\Http\ServerSentMessage;
2020
use Tempest\Http\Status;
2121
use Tempest\Router\GenericResponseSender;
22+
use Tempest\Support\Json\Exception\JsonCouldNotBeEncoded;
2223
use Tempest\View\ViewRenderer;
2324
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
2425

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

121+
public function test_sending_invalid_json_throws_exception(): void
122+
{
123+
ob_start();
124+
$response = new GenericResponse(
125+
status: Status::CREATED,
126+
body: ['key' => "\xB1\x31"],
127+
);
128+
129+
$responseSender = $this->container->get(GenericResponseSender::class);
130+
131+
$this->assertException(JsonCouldNotBeEncoded::class, fn () => $responseSender->send($response));
132+
133+
ob_get_clean();
134+
}
135+
120136
public function test_sending_of_json_serializable_to_json(): void
121137
{
122138
ob_start();

0 commit comments

Comments
 (0)