12
12
use flight \net \Response ;
13
13
use flight \util \Collection ;
14
14
use InvalidArgumentException ;
15
+ use JsonException ;
15
16
use PDOException ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
use tests \classes \Container ;
@@ -355,18 +356,36 @@ public function testJson()
355
356
{
356
357
$ engine = new Engine ();
357
358
$ 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 ' ]);
359
360
$ this ->assertEquals (200 , $ engine ->response ()->status ());
360
361
$ this ->assertEquals ('{"key1":"value1","key2":"value2"} ' , $ engine ->response ()->getBody ());
361
362
}
362
363
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
+
363
382
public function testJsonV2OutputBuffering ()
364
383
{
365
384
$ engine = new Engine ();
366
385
$ engine ->response ()->v2_output_buffering = true ;
367
386
$ engine ->json (['key1 ' => 'value1 ' , 'key2 ' => 'value2 ' ]);
368
387
$ 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 ' ]);
370
389
$ this ->assertEquals (200 , $ engine ->response ()->status ());
371
390
}
372
391
@@ -375,7 +394,7 @@ public function testJsonHalt()
375
394
$ engine = new Engine ();
376
395
$ this ->expectOutputString ('{"key1":"value1","key2":"value2"} ' );
377
396
$ 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 ' ]);
379
398
$ this ->assertEquals (200 , $ engine ->response ()->status ());
380
399
$ this ->assertEquals ('{"key1":"value1","key2":"value2"} ' , $ engine ->response ()->getBody ());
381
400
}
0 commit comments