Make WordPress Core


Ignore:
Timestamp:
12/13/2016 03:33:14 AM (10 years ago)
Author:
jnylen0
Message:

REST API: Do not error on empty JSON body

It's fairly common for clients to send Content-Type: application/json with an
empty body. While technically not valid JSON, we've historically supported
this behaviour, so it shouldn't cause an error.

Props JPry.
Fixes #39150.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r39563 r39594  
    1111 */
    1212class Tests_REST_Request extends WP_UnitTestCase {
     13        public $request;
     14
    1315        public function setUp() {
    1416                parent::setUp();
    … …  
    449451                $data = $valid->get_error_data();
    450452                $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
     453        }
     454
     455
     456        public function test_has_valid_params_empty_json_no_error() {
     457                if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
     458                        return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' );
     459                }
     460
     461                $this->request->set_header( 'Content-Type', 'application/json' );
     462                $this->request->set_body( '' );
     463
     464                $valid = $this->request->has_valid_params();
     465                $this->assertNotWPError( $valid );
    451466        }
    452467
Note: See TracChangeset for help on using the changeset viewer.