Merge pull request #627 from flightphp/json-charset

Removed utf8 from json function, add default flags, and more unit tests
pull/628/merge
n0nag0n 3 days ago committed by GitHub
commit f4115927fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -830,7 +830,7 @@ class Engine
* @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 @@ class Engine
$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();

@ -12,6 +12,7 @@ use flight\net\Request;
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 @@ class EngineTest extends TestCase
{
$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 @@ class EngineTest extends TestCase
$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());
}

@ -81,6 +81,10 @@ PHP;
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);
}

Loading…
Cancel
Save