Make WordPress Core


Ignore:
Timestamp:
03/02/2025 10:05:08 PM (3 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Improve performance for HEAD requests.

By default, the REST API responds to HEAD rqeuests by calling the GET handler and omitting the body from the response. While convenient, this ends up performing needless work that slows down the API response time.

This commit adjusts the Core controllers to specifically handle HEAD requests by not preparing the response body.

Fixes #56481.
Props antonvlasenko, janusdev, ironprogrammer, swissspidy, spacedmonkey, mukesh27, mamaduka, timothyblynjacobs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php

    r59630 r59899  
    246246
    247247    /**
    248      * @ticket 58524
    249      *
    250      * @covers WP_REST_Global_Styles_Controller::get_items
    251      */
    252     public function test_get_items_missing_parent() {
    253         wp_set_current_user( self::$admin_id );
    254         $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
     248     * @dataProvider data_readable_http_methods
     249     * @ticket 58524
     250     * @ticket 56481
     251     *
     252     * @covers WP_REST_Global_Styles_Controller::get_items
     253     *
     254     * @param string $method The HTTP method to use.
     255     */
     256    public function test_get_items_missing_parent( $method ) {
     257        wp_set_current_user( self::$admin_id );
     258        $request  = new WP_REST_Request( $method, '/wp/v2/global-styles/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
    255259        $response = rest_get_server()->dispatch( $request );
    256260        $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
     261    }
     262
     263    /**
     264     * Data provider intended to provide HTTP method names for testing GET and HEAD requests.
     265     *
     266     * @return array
     267     */
     268    public static function data_readable_http_methods() {
     269        return array(
     270            'GET request'  => array( 'GET' ),
     271            'HEAD request' => array( 'HEAD' ),
     272        );
    257273    }
    258274
     
    312328
    313329    /**
     330     * @ticket 56481
     331     *
     332     * @covers WP_REST_Global_Styles_Controller::prepare_item_for_response
     333     */
     334    public function test_get_items_should_return_no_response_body_for_head_requests() {
     335        wp_set_current_user( self::$admin_id );
     336        $request  = new WP_REST_Request( 'HEAD', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     337        $response = rest_get_server()->dispatch( $request );
     338        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
     339        $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     340    }
     341
     342    /**
    314343     * @ticket 59810
    315344     *
     
    328357
    329358    /**
     359     * @ticket 56481
     360     *
     361     * @covers WP_REST_Global_Styles_Controller::get_item
     362     * @covers WP_REST_Global_Styles_Controller::prepare_item_for_response
     363     */
     364    public function test_get_item_should_return_no_response_body_for_head_requests() {
     365        wp_set_current_user( self::$admin_id );
     366        $request  = new WP_REST_Request( 'HEAD', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/' . $this->revision_1_id );
     367        $response = rest_get_server()->dispatch( $request );
     368        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
     369        $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     370    }
     371
     372    /**
     373     * @dataProvider data_readable_http_methods
    330374     * @ticket 59810
     375     * @ticket 56481
    331376     *
    332377     * @covers WP_REST_Global_Styles_Controller::get_revision
    333      */
    334     public function test_get_item_invalid_revision_id_should_error() {
     378     *
     379     * @param string $method The HTTP method to use.
     380     */
     381    public function test_get_item_invalid_revision_id_should_error( $method ) {
    335382        wp_set_current_user( self::$admin_id );
    336383
    337384        $expected_error  = 'rest_post_invalid_id';
    338385        $expected_status = 404;
    339         $request         = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' );
     386        $request         = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' );
    340387        $response        = rest_get_server()->dispatch( $request );
    341388
     
    420467
    421468    /**
     469     * @dataProvider data_readable_http_methods
    422470     * @ticket 58524
    423471     * @ticket 60131
     472     * @ticket 56481
    424473     *
    425474     * @covers WP_REST_Global_Styles_Controller::get_item_permissions_check
    426      */
    427     public function test_get_item_permissions_check() {
     475     *
     476     * @param string $method The HTTP method to use.
     477     */
     478    public function test_get_item_permissions_check( $method ) {
    428479        wp_set_current_user( self::$author_id );
    429         $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     480        $request  = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    430481        $response = rest_get_server()->dispatch( $request );
    431482
     
    436487     * Tests the pagination header of the first page.
    437488     *
    438      * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_first_page
    439      *
    440      * @ticket 58524
    441      *
    442      * @covers WP_REST_Global_Styles_Controller::get_items
    443      */
    444     public function test_get_items_pagination_header_of_the_first_page() {
     489     * @dataProvider data_readable_http_methods
     490     * @ticket 58524
     491     * @ticket 56481
     492     *
     493     * @covers WP_REST_Global_Styles_Controller::get_items
     494     *
     495     * @param string $method The HTTP method to use.
     496     */
     497    public function test_get_items_pagination_header_of_the_first_page( $method ) {
    445498        wp_set_current_user( self::$admin_id );
    446499
     
    450503        $page        = 1;  // First page.
    451504
    452         $request = new WP_REST_Request( 'GET', $rest_route );
     505        $request = new WP_REST_Request( $method, $rest_route );
    453506        $request->set_query_params(
    454507            array(
     
    477530     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_last_page
    478531     *
    479      * @ticket 58524
    480      *
    481      * @covers WP_REST_Global_Styles_Controller::get_items
    482      */
    483     public function test_get_items_pagination_header_of_the_last_page() {
     532     * @dataProvider data_readable_http_methods
     533     * @ticket 58524
     534     * @ticket 56481
     535     *
     536     * @covers WP_REST_Global_Styles_Controller::get_items
     537     *
     538     * @param string $method The HTTP method to use.
     539     */
     540    public function test_get_items_pagination_header_of_the_last_page( $method ) {
    484541        wp_set_current_user( self::$admin_id );
    485542
     
    489546        $page        = 2;  // Last page.
    490547
    491         $request = new WP_REST_Request( 'GET', $rest_route );
     548        $request = new WP_REST_Request( $method, $rest_route );
    492549        $request->set_query_params(
    493550            array(
     
    515572     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_per_page_should_error
    516573     *
    517      * @ticket 58524
    518      *
    519      * @covers WP_REST_Global_Styles_Controller::get_items
    520      */
    521     public function test_get_items_invalid_per_page_should_error() {
     574     * @dataProvider data_readable_http_methods
     575     * @ticket 58524
     576     * @ticket 56481
     577     *
     578     * @covers WP_REST_Global_Styles_Controller::get_items
     579     *
     580     * @param string $method The HTTP method to use.
     581     */
     582    public function test_get_items_invalid_per_page_should_error( $method ) {
    522583        wp_set_current_user( self::$admin_id );
    523584
     
    526587        $expected_status = 400;
    527588
    528         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     589        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    529590        $request->set_param( 'per_page', $per_page );
    530591        $response = rest_get_server()->dispatch( $request );
     
    537598     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bounds_page_should_error
    538599     *
    539      * @ticket 58524
    540      *
    541      * @covers WP_REST_Global_Styles_Controller::get_items
    542      */
    543     public function test_get_items_out_of_bounds_page_should_error() {
     600     * @dataProvider data_readable_http_methods
     601     * @ticket 58524
     602     * @ticket 56481
     603     *
     604     * @covers WP_REST_Global_Styles_Controller::get_items
     605     *
     606     * @param string $method The HTTP method to use.
     607     */
     608    public function test_get_items_out_of_bounds_page_should_error( $method ) {
    544609        wp_set_current_user( self::$admin_id );
    545610
     
    550615        $expected_status = 400;
    551616
    552         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     617        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    553618        $request->set_query_params(
    554619            array(
     
    566631     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_max_pages_should_error
    567632     *
    568      * @ticket 58524
    569      *
    570      * @covers WP_REST_Global_Styles_Controller::get_items
    571      */
    572     public function test_get_items_invalid_max_pages_should_error() {
     633     * @dataProvider data_readable_http_methods
     634     * @ticket 58524
     635     * @ticket 56481
     636     *
     637     * @covers WP_REST_Global_Styles_Controller::get_items
     638     *
     639     * @param string $method The HTTP method to use.
     640     */
     641    public function test_get_items_invalid_max_pages_should_error( $method ) {
    573642        wp_set_current_user( self::$admin_id );
    574643
     
    578647        $expected_status = 400;
    579648
    580         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     649        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    581650        $request->set_query_params(
    582651            array(
     
    690759     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_total_revisions_offset_should_return_empty_data
    691760     *
    692      * @ticket 58524
    693      *
    694      * @covers WP_REST_Global_Styles_Controller::get_items
    695      */
    696     public function test_get_items_total_revisions_offset_should_return_empty_data() {
     761     * @dataProvider data_readable_http_methods
     762     * @ticket 58524
     763     * @ticket 56481
     764     *
     765     * @covers WP_REST_Global_Styles_Controller::get_items
     766     *
     767     * @param string $method The HTTP method to use.
     768     */
     769    public function test_get_items_total_revisions_offset_should_return_empty_data( $method ) {
    697770        wp_set_current_user( self::$admin_id );
    698771
     
    702775        $expected_status = 400;
    703776
    704         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     777        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    705778        $request->set_query_params(
    706779            array(
     
    718791     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bound_offset_should_error
    719792     *
    720      * @ticket 58524
    721      *
    722      * @covers WP_REST_Global_Styles_Controller::get_items
    723      */
    724     public function test_get_items_out_of_bound_offset_should_error() {
     793     * @dataProvider data_readable_http_methods
     794     * @ticket 58524
     795     * @ticket 56481
     796     *
     797     * @covers WP_REST_Global_Styles_Controller::get_items
     798     *
     799     * @param string $method The HTTP method to use.
     800     */
     801    public function test_get_items_out_of_bound_offset_should_error( $method ) {
    725802        wp_set_current_user( self::$admin_id );
    726803
     
    730807        $expected_status = 400;
    731808
    732         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     809        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    733810        $request->set_query_params(
    734811            array(
     
    746823     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_impossible_high_number_offset_should_error
    747824     *
    748      * @ticket 58524
    749      *
    750      * @covers WP_REST_Global_Styles_Controller::get_items
    751      */
    752     public function test_get_items_impossible_high_number_offset_should_error() {
     825     * @dataProvider data_readable_http_methods
     826     * @ticket 58524
     827     * @ticket 56481
     828     *
     829     * @covers WP_REST_Global_Styles_Controller::get_items
     830     *
     831     * @param string $method The HTTP method to use.
     832     */
     833    public function test_get_items_impossible_high_number_offset_should_error( $method ) {
    753834        wp_set_current_user( self::$admin_id );
    754835
     
    758839        $expected_status = 400;
    759840
    760         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     841        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    761842        $request->set_query_params(
    762843            array(
     
    774855     * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_offset_should_error
    775856     *
    776      * @ticket 58524
    777      *
    778      * @covers WP_REST_Global_Styles_Controller::get_items
    779      */
    780     public function test_get_items_invalid_offset_should_error() {
     857     * @dataProvider data_readable_http_methods
     858     * @ticket 58524
     859     * @ticket 56481
     860     *
     861     * @covers WP_REST_Global_Styles_Controller::get_items
     862     *
     863     * @param string $method The HTTP method to use.
     864     */
     865    public function test_get_items_invalid_offset_should_error( $method ) {
    781866        wp_set_current_user( self::$admin_id );
    782867
     
    786871        $expected_status = 400;
    787872
    788         $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
     873        $request = new WP_REST_Request( $method, '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
    789874        $request->set_query_params(
    790875            array(
Note: See TracChangeset for help on using the changeset viewer.