From 207304b3737fae20a915c704f68a4719d2d157e6 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 22 May 2024 21:16:47 -0400 Subject: [PATCH] Test JSON responses. --- app/tests/Controller/ControllerTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php index d6ee8b0..349b887 100644 --- a/app/tests/Controller/ControllerTest.php +++ b/app/tests/Controller/ControllerTest.php @@ -53,4 +53,26 @@ class ControllerTest extends TestCase $this->assertEquals('

Oh no!

', $response->getBody()); $this->assertEquals('text/html', $response->getHeaderLine('Content-Type')); } + + public function testJsonResponse(): void + { + $testController = new class extends Controller { + public function respond(): Response + { + return $this->json([ 'data' => 'hello' ]); + } + }; + + $app = $this->getAppInstance(); + $app->get('/test', $testController); + + $request = $this->createRequest('GET', '/test'); + $response = $app->handle($request); + + $this->assertEquals('{"data":"hello"}', $response->getBody()); + $this->assertEquals( + 'application/json', + $response->getHeaderLine('Content-Type') + ); + } }