JsonResponseTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Illuminate\Tests\Integration\Http;
  3. use Illuminate\Contracts\Support\Jsonable;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Support\Facades\Route;
  6. use Orchestra\Testbench\TestCase;
  7. class JsonResponseTest extends TestCase
  8. {
  9. public function testResponseWithInvalidJsonThrowsException()
  10. {
  11. $this->expectException('InvalidArgumentException');
  12. $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
  13. Route::get('/response', function () {
  14. return new JsonResponse(new class implements \JsonSerializable
  15. {
  16. public function jsonSerialize(): string
  17. {
  18. return "\xB1\x31";
  19. }
  20. });
  21. });
  22. $this->withoutExceptionHandling();
  23. $this->get('/response');
  24. }
  25. public function testResponseSetDataPassesWithPriorJsonErrors()
  26. {
  27. $response = new JsonResponse();
  28. // Trigger json_last_error() to have a non-zero value...
  29. json_encode(['a' => acos(2)]);
  30. $response->setData(new class implements Jsonable
  31. {
  32. public function toJson($options = 0): string
  33. {
  34. return '{}';
  35. }
  36. });
  37. $this->assertJson($response->getContent());
  38. }
  39. }