mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-31 10:44:26 +00:00
REST API: Add WP_REST_Request::from_url()
Allows converting a REST URL into a Request object. Props danielbachhuber. Fixes #35803. git-svn-id: https://develop.svn.wordpress.org/trunk@36673 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -420,4 +420,46 @@ class Tests_REST_Request extends WP_UnitTestCase {
|
||||
public function _return_wp_error_on_validate_callback() {
|
||||
return new WP_Error( 'some-error', 'This is not valid!' );
|
||||
}
|
||||
|
||||
public function data_from_url() {
|
||||
return array(
|
||||
array(
|
||||
'permalink_structure' => '/%post_name%/',
|
||||
'original_url' => 'http://' . WP_TESTS_DOMAIN . '/wp-json/wp/v2/posts/1?foo=bar',
|
||||
),
|
||||
array(
|
||||
'permalink_structure' => '',
|
||||
'original_url' => 'http://' . WP_TESTS_DOMAIN . '/?rest_route=%2Fwp%2Fv2%2Fposts%2F1&foo=bar',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_from_url
|
||||
*/
|
||||
public function test_from_url( $permalink_structure, $original_url ) {
|
||||
update_option( 'permalink_structure', $permalink_structure );
|
||||
$url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) );
|
||||
$this->assertEquals( $original_url, $url );
|
||||
$request = WP_REST_Request::from_url( $url );
|
||||
$this->assertInstanceOf( 'WP_REST_Request', $request );
|
||||
$this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
|
||||
$this->assertEqualSets( array(
|
||||
'foo' => 'bar',
|
||||
), $request->get_query_params() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_from_url
|
||||
*/
|
||||
public function test_from_url_invalid( $permalink_structure ) {
|
||||
update_option( 'permalink_structure', $permalink_structure );
|
||||
$using_site = site_url( '/wp/v2/posts/1' );
|
||||
$request = WP_REST_Request::from_url( $using_site );
|
||||
$this->assertFalse( $request );
|
||||
|
||||
$using_home = home_url( '/wp/v2/posts/1' ) ;
|
||||
$request = WP_REST_Request::from_url( $using_home );
|
||||
$this->assertFalse( $request );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user