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

Removed utf8 from json function, add default flags, and more unit tests #627

Merged
merged 1 commit into from
Feb 20, 2025
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
8 changes: 5 additions & 3 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
25 changes: 22 additions & 3 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}
Expand Down
4 changes: 4 additions & 0 deletions tests/commands/RouteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down