REST API: Return error when JSON decoding fails.

If you send a request to the REST API with invalid JSON in body than it will now return a error. This assists developers if they accidentally send invalid JSON and wonder why their data appears to be ignored.

Props rmccue.
Fixes #38547.


git-svn-id: https://develop.svn.wordpress.org/trunk@39109 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Hoyle
2016-11-03 04:04:41 +00:00
parent 973ade2a28
commit f784a0481b
2 changed files with 35 additions and 3 deletions

View File

@@ -399,6 +399,21 @@ class Tests_REST_Request extends WP_UnitTestCase {
$this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
}
public function test_has_valid_params_json_error() {
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' );
}
$this->request->set_header( 'Content-Type', 'application/json' );
$this->request->set_body( '{"invalid": JSON}' );
$valid = $this->request->has_valid_params();
$this->assertWPError( $valid );
$this->assertEquals( 'rest_invalid_json', $valid->get_error_code() );
$data = $valid->get_error_data();
$this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
}
public function test_has_multiple_invalid_params_validate_callback() {
$this->request->set_url_params( array(
'someinteger' => '123',