From 76c53c6260e43cb38d2f76653c6ef38b530255fe Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 6 Nov 2023 07:07:25 +0900 Subject: [PATCH] test: add test for Validation::withRequest() --- tests/system/Validation/ValidationTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/system/Validation/ValidationTest.php b/tests/system/Validation/ValidationTest.php index 7cc6f99c40d5..b2db31ad00b0 100644 --- a/tests/system/Validation/ValidationTest.php +++ b/tests/system/Validation/ValidationTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Validation; +use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\URI; use CodeIgniter\HTTP\UserAgent; @@ -789,6 +790,25 @@ public function testJsonInput(): void unset($_SERVER['CONTENT_TYPE']); } + public function testJsonInputInvalid(): void + { + $this->expectException(HTTPException::class); + $this->expectExceptionMessage('Failed to parse JSON string. Error: Syntax error'); + + $config = new App(); + $json = 'invalid'; + $request = new IncomingRequest($config, new URI(), $json, new UserAgent()); + $request->setHeader('Content-Type', 'application/json'); + + $rules = [ + 'role' => 'if_exist|max_length[5]', + ]; + $this->validation + ->withRequest($request->withMethod('POST')) + ->setRules($rules) + ->run(); + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/6466 */