Skip to content

Commit fe3d953

Browse files
committed
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
1 parent 40506e7 commit fe3d953

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/wp-includes/rest-api.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,11 @@ function rest_preload_api_request( $memo, $path ) {
28182818
}
28192819
}
28202820

2821+
$path = untrailingslashit( $path );
2822+
if ( empty( $path ) ) {
2823+
$path = '/';
2824+
}
2825+
28212826
$path_parts = parse_url( $path );
28222827
if ( false === $path_parts ) {
28232828
return $memo;

tests/phpunit/tests/rest-api.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,31 @@ function test_rest_preload_api_request_with_method() {
954954
$GLOBALS['wp_rest_server'] = $rest_server;
955955
}
956956

957+
/**
958+
* @ticket 51636
959+
*/
960+
function test_rest_preload_api_request_removes_trailing_slashes() {
961+
$rest_server = $GLOBALS['wp_rest_server'];
962+
$GLOBALS['wp_rest_server'] = null;
963+
964+
$preload_paths = array(
965+
'/wp/v2/types//',
966+
array( '/wp/v2/media///', 'OPTIONS' ),
967+
'////',
968+
);
969+
970+
$preload_data = array_reduce(
971+
$preload_paths,
972+
'rest_preload_api_request',
973+
array()
974+
);
975+
976+
$this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS', '/' ) );
977+
$this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
978+
979+
$GLOBALS['wp_rest_server'] = $rest_server;
980+
}
981+
957982
/**
958983
* @ticket 40614
959984
*/

0 commit comments

Comments
 (0)