WordPress.org

Make WordPress Core

Changeset 51648


Ignore:
Timestamp:
08/20/2021 11:43:21 PM (2 months ago)
Author:
azaozz
Message:

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

Props antonvlasenko.
Fixes #51636.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r51278 r51648  
    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 ) {
  • trunk/tests/phpunit/tests/rest-api.php

    r51568 r51648  
    950950
    951951        $this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS' ) );
     952        $this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
     953
     954        $GLOBALS['wp_rest_server'] = $rest_server;
     955    }
     956
     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', '/' ) );
    952977        $this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
    953978
Note: See TracChangeset for help on using the changeset viewer.