From 3a84a9aaf2d085df231b6828fe0295d629f74905 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sun, 16 Feb 2025 08:21:52 -0700 Subject: [PATCH] removed utf8 from json function, add default flags, and more unit tests --- flight/Engine.php | 8 +++++--- tests/EngineTest.php | 25 ++++++++++++++++++++++--- tests/commands/RouteCommandTest.php | 4 ++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 0a77031..aa4e202 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -830,7 +830,7 @@ public function _render(string $file, ?array $data = null, ?string $key = null): * @param mixed $data JSON data * @param int $code HTTP status code * @param bool $encode Whether to perform JSON encoding - * @param string $charset Charset + * @param ?string $charset Charset * @param int $option Bitmask Json constant such as JSON_HEX_QUOT * * @throws Exception @@ -839,14 +839,16 @@ public function _json( $data, int $code = 200, bool $encode = true, - string $charset = 'utf-8', + ?string $charset = 'utf-8', int $option = 0 ): void { + // add some default flags + $option |= JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR; $json = $encode ? json_encode($data, $option) : $data; $this->response() ->status($code) - ->header('Content-Type', 'application/json; charset=' . $charset) + ->header('Content-Type', 'application/json') ->write($json); if ($this->response()->v2_output_buffering === true) { $this->response()->send(); diff --git a/tests/EngineTest.php b/tests/EngineTest.php index e2289c6..45445ed 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -12,6 +12,7 @@ use flight\net\Response; use flight\util\Collection; use InvalidArgumentException; +use JsonException; use PDOException; use PHPUnit\Framework\TestCase; use tests\classes\Container; @@ -355,18 +356,36 @@ public function testJson() { $engine = new Engine(); $engine->json(['key1' => 'value1', 'key2' => 'value2']); - $this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']); + $this->assertEquals('application/json', $engine->response()->headers()['Content-Type']); $this->assertEquals(200, $engine->response()->status()); $this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody()); } + public function testJsonWithDuplicateDefaultFlags() + { + $engine = new Engine(); + // utf8 emoji + $engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => '😀'], 201, true, '', JSON_HEX_TAG | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + $this->assertEquals('application/json', $engine->response()->headers()['Content-Type']); + $this->assertEquals(201, $engine->response()->status()); + $this->assertEquals('{"key1":"value1","key2":"value2","utf8_emoji":"😀"}', $engine->response()->getBody()); + } + + public function testJsonThrowOnErrorByDefault() + { + $engine = new Engine(); + $this->expectException(JsonException::class); + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); + $engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => "\xB1\x31"]); + } + public function testJsonV2OutputBuffering() { $engine = new Engine(); $engine->response()->v2_output_buffering = true; $engine->json(['key1' => 'value1', 'key2' => 'value2']); $this->expectOutputString('{"key1":"value1","key2":"value2"}'); - $this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']); + $this->assertEquals('application/json', $engine->response()->headers()['Content-Type']); $this->assertEquals(200, $engine->response()->status()); } @@ -375,7 +394,7 @@ public function testJsonHalt() $engine = new Engine(); $this->expectOutputString('{"key1":"value1","key2":"value2"}'); $engine->jsonHalt(['key1' => 'value1', 'key2' => 'value2']); - $this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']); + $this->assertEquals('application/json', $engine->response()->headers()['Content-Type']); $this->assertEquals(200, $engine->response()->status()); $this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody()); } diff --git a/tests/commands/RouteCommandTest.php b/tests/commands/RouteCommandTest.php index fd4cc2b..aa3fff3 100644 --- a/tests/commands/RouteCommandTest.php +++ b/tests/commands/RouteCommandTest.php @@ -81,6 +81,10 @@ protected function createIndexFile() protected function removeColors(string $str): string { + // replace \n with \r\n if windows + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + $str = str_replace("\r\n", "\n", $str); + } return preg_replace('/\e\[[\d;]*m/', '', $str); }