Skip to content

Commit f411592

Browse files
authored
Merge pull request #627 from flightphp/json-charset
Removed utf8 from json function, add default flags, and more unit tests
2 parents 2bb058e + 3a84a9a commit f411592

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

flight/Engine.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public function _render(string $file, ?array $data = null, ?string $key = null):
830830
* @param mixed $data JSON data
831831
* @param int $code HTTP status code
832832
* @param bool $encode Whether to perform JSON encoding
833-
* @param string $charset Charset
833+
* @param ?string $charset Charset
834834
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
835835
*
836836
* @throws Exception
@@ -839,14 +839,16 @@ public function _json(
839839
$data,
840840
int $code = 200,
841841
bool $encode = true,
842-
string $charset = 'utf-8',
842+
?string $charset = 'utf-8',
843843
int $option = 0
844844
): void {
845+
// add some default flags
846+
$option |= JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR;
845847
$json = $encode ? json_encode($data, $option) : $data;
846848

847849
$this->response()
848850
->status($code)
849-
->header('Content-Type', 'application/json; charset=' . $charset)
851+
->header('Content-Type', 'application/json')
850852
->write($json);
851853
if ($this->response()->v2_output_buffering === true) {
852854
$this->response()->send();

tests/EngineTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use flight\net\Response;
1313
use flight\util\Collection;
1414
use InvalidArgumentException;
15+
use JsonException;
1516
use PDOException;
1617
use PHPUnit\Framework\TestCase;
1718
use tests\classes\Container;
@@ -355,18 +356,36 @@ public function testJson()
355356
{
356357
$engine = new Engine();
357358
$engine->json(['key1' => 'value1', 'key2' => 'value2']);
358-
$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
359+
$this->assertEquals('application/json', $engine->response()->headers()['Content-Type']);
359360
$this->assertEquals(200, $engine->response()->status());
360361
$this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody());
361362
}
362363

364+
public function testJsonWithDuplicateDefaultFlags()
365+
{
366+
$engine = new Engine();
367+
// utf8 emoji
368+
$engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => '😀'], 201, true, '', JSON_HEX_TAG | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
369+
$this->assertEquals('application/json', $engine->response()->headers()['Content-Type']);
370+
$this->assertEquals(201, $engine->response()->status());
371+
$this->assertEquals('{"key1":"value1","key2":"value2","utf8_emoji":"😀"}', $engine->response()->getBody());
372+
}
373+
374+
public function testJsonThrowOnErrorByDefault()
375+
{
376+
$engine = new Engine();
377+
$this->expectException(JsonException::class);
378+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
379+
$engine->json(['key1' => 'value1', 'key2' => 'value2', 'utf8_emoji' => "\xB1\x31"]);
380+
}
381+
363382
public function testJsonV2OutputBuffering()
364383
{
365384
$engine = new Engine();
366385
$engine->response()->v2_output_buffering = true;
367386
$engine->json(['key1' => 'value1', 'key2' => 'value2']);
368387
$this->expectOutputString('{"key1":"value1","key2":"value2"}');
369-
$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
388+
$this->assertEquals('application/json', $engine->response()->headers()['Content-Type']);
370389
$this->assertEquals(200, $engine->response()->status());
371390
}
372391

@@ -375,7 +394,7 @@ public function testJsonHalt()
375394
$engine = new Engine();
376395
$this->expectOutputString('{"key1":"value1","key2":"value2"}');
377396
$engine->jsonHalt(['key1' => 'value1', 'key2' => 'value2']);
378-
$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
397+
$this->assertEquals('application/json', $engine->response()->headers()['Content-Type']);
379398
$this->assertEquals(200, $engine->response()->status());
380399
$this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody());
381400
}

tests/commands/RouteCommandTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ protected function createIndexFile()
8181

8282
protected function removeColors(string $str): string
8383
{
84+
// replace \n with \r\n if windows
85+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
86+
$str = str_replace("\r\n", "\n", $str);
87+
}
8488
return preg_replace('/\e\[[\d;]*m/', '', $str);
8589
}
8690

0 commit comments

Comments
 (0)