Skip to content

Commit

Permalink
test: add test for Validation::withRequest()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 5, 2023
1 parent 3cb7c69 commit 0b926ea
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Validation;

use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 0b926ea

Please sign in to comment.