mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-29 15:44:27 +00:00
REST API: Support a broader range of JSON media types.
Previously, we only supported `application/json` which prevented using subtypes like `application/activity+json`. This allows for the REST API to `json_decode` the body of requests using a JSON subtype `Content-Type`. Additionally, `wp_die()` now properly sends the error as JSON when a JSON subtype is specified in the `Accept` header. Props pfefferle. Fixes #49404. git-svn-id: https://develop.svn.wordpress.org/trunk@49329 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1728,4 +1728,27 @@ class Tests_Functions extends WP_UnitTestCase {
|
||||
array( '03:61:59', false ), // Out of bound.
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49404
|
||||
* @dataProvider data_test_wp_is_json_media_type
|
||||
*/
|
||||
public function test_wp_is_json_media_type( $input, $expected ) {
|
||||
$this->assertEquals( $expected, wp_is_json_media_type( $input ) );
|
||||
}
|
||||
|
||||
|
||||
public function data_test_wp_is_json_media_type() {
|
||||
return array(
|
||||
array( 'application/ld+json', true ),
|
||||
array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', true ),
|
||||
array( 'application/activity+json', true ),
|
||||
array( 'application/json+oembed', true ),
|
||||
array( 'application/json', true ),
|
||||
array( 'application/nojson', false ),
|
||||
array( 'application/no.json', false ),
|
||||
array( 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8', false ),
|
||||
array( 'application/activity+json, application/nojson', true ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,6 +178,87 @@ class Tests_REST_Request extends WP_UnitTestCase {
|
||||
$this->assertEmpty( $this->request->get_param( 'has_json_params' ) );
|
||||
}
|
||||
|
||||
public static function alternate_json_content_type_provider() {
|
||||
return array(
|
||||
array( 'application/ld+json', 'json', true ),
|
||||
array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', 'json', true ),
|
||||
array( 'application/activity+json', 'json', true ),
|
||||
array( 'application/json+oembed', 'json', true ),
|
||||
array( 'application/nojson', 'body', false ),
|
||||
array( 'application/no.json', 'body', false ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49404
|
||||
* @dataProvider alternate_json_content_type_provider
|
||||
*
|
||||
* @param string $content_type The content-type
|
||||
* @param string $source The source value.
|
||||
* @param boolean $accept_json The accept_json value.
|
||||
*/
|
||||
public function test_alternate_json_content_type( $content_type, $source, $accept_json ) {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_method( 'POST' );
|
||||
$this->request->set_header( 'Content-Type', $content_type );
|
||||
$this->request->set_attributes( array( 'accept_json' => true ) );
|
||||
|
||||
// Check that JSON takes precedence.
|
||||
$this->assertEquals( $source, $this->request->get_param( 'source' ) );
|
||||
$this->assertEquals( $accept_json, $this->request->get_param( 'has_json_params' ) );
|
||||
}
|
||||
|
||||
public static function is_json_content_type_provider() {
|
||||
return array(
|
||||
array( 'application/ld+json', true ),
|
||||
array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', true ),
|
||||
array( 'application/activity+json', true ),
|
||||
array( 'application/json+oembed', true ),
|
||||
array( 'application/nojson', false ),
|
||||
array( 'application/no.json', false ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49404
|
||||
* @dataProvider is_json_content_type_provider
|
||||
*
|
||||
* @param string $content_type The content-type
|
||||
* @param boolean $is_json The is_json value.
|
||||
*/
|
||||
public function test_is_json_content_type( $content_type, $is_json ) {
|
||||
$this->request_with_parameters();
|
||||
|
||||
$this->request->set_header( 'Content-Type', $content_type );
|
||||
|
||||
// Check for JSON content-type.
|
||||
$this->assertEquals( $is_json, $this->request->is_json_content_type() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49404
|
||||
*/
|
||||
public function test_content_type_cache() {
|
||||
$this->request_with_parameters();
|
||||
$this->assertFalse( $this->request->is_json_content_type() );
|
||||
|
||||
$this->request->set_header( 'Content-Type', 'application/json' );
|
||||
$this->assertTrue( $this->request->is_json_content_type() );
|
||||
|
||||
$this->request->set_header( 'Content-Type', 'application/activity+json' );
|
||||
$this->assertTrue( $this->request->is_json_content_type() );
|
||||
|
||||
$this->request->set_header( 'Content-Type', 'application/nojson' );
|
||||
$this->assertFalse( $this->request->is_json_content_type() );
|
||||
|
||||
$this->request->set_header( 'Content-Type', 'application/json' );
|
||||
$this->assertTrue( $this->request->is_json_content_type() );
|
||||
|
||||
$this->request->remove_header( 'Content-Type' );
|
||||
$this->assertFalse( $this->request->is_json_content_type() );
|
||||
}
|
||||
|
||||
public function test_parameter_order_json() {
|
||||
$this->request_with_parameters();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user