Skip to content

Commit 4d2a4ba

Browse files
committed
test: add test for Validation::withRequest()
1 parent 8ba7444 commit 4d2a4ba

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎tests/system/Validation/ValidationTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Validation;
1313

14+
use CodeIgniter\HTTP\Exceptions\HTTPException;
1415
use CodeIgniter\HTTP\IncomingRequest;
1516
use CodeIgniter\HTTP\URI;
1617
use CodeIgniter\HTTP\UserAgent;
@@ -789,6 +790,25 @@ public function testJsonInput(): void
789790
unset($_SERVER['CONTENT_TYPE']);
790791
}
791792

793+
public function testJsonInputInvalid(): void
794+
{
795+
$this->expectException(HTTPException::class);
796+
$this->expectExceptionMessage('Failed to parse JSON string. Error: Syntax error');
797+
798+
$config = new App();
799+
$json = 'invalid';
800+
$request = new IncomingRequest($config, new URI(), $json, new UserAgent());
801+
$request->setHeader('Content-Type', 'application/json');
802+
803+
$rules = [
804+
'role' => 'if_exist|max_length[5]',
805+
];
806+
$this->validation
807+
->withRequest($request->withMethod('POST'))
808+
->setRules($rules)
809+
->run();
810+
}
811+
792812
/**
793813
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6466
794814
*/

0 commit comments

Comments
 (0)