REST API: Remove trailing slashes when preloading requests and add unit tests for it.

Props antonvlasenko.
Fixes #51636.

git-svn-id: https://develop.svn.wordpress.org/trunk@51648 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2021-08-20 23:43:21 +00:00
parent 40506e7e64
commit fe3d953437
2 changed files with 30 additions and 0 deletions

View File

@ -2818,6 +2818,11 @@ function rest_preload_api_request( $memo, $path ) {
}
}
$path = untrailingslashit( $path );
if ( empty( $path ) ) {
$path = '/';
}
$path_parts = parse_url( $path );
if ( false === $path_parts ) {
return $memo;

View File

@ -954,6 +954,31 @@ class Tests_REST_API extends WP_UnitTestCase {
$GLOBALS['wp_rest_server'] = $rest_server;
}
/**
* @ticket 51636
*/
function test_rest_preload_api_request_removes_trailing_slashes() {
$rest_server = $GLOBALS['wp_rest_server'];
$GLOBALS['wp_rest_server'] = null;
$preload_paths = array(
'/wp/v2/types//',
array( '/wp/v2/media///', 'OPTIONS' ),
'////',
);
$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
array()
);
$this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS', '/' ) );
$this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
$GLOBALS['wp_rest_server'] = $rest_server;
}
/**
* @ticket 40614
*/