Make WordPress Core


Ignore:
Timestamp:
03/19/2019 03:21:28 AM (6 years ago)
Author:
kadamwhite
Message:

REST API: Ensure "Allow" header is returned for OPTIONS requests.

This changeset ensures $request->set_url_params() is called while fulfilling OPTIONS requests, where previously it was skipped because OPTIONS requests short-circuit the logic in dispatch which handles this setup for other request methods. Omitting the URL parameters prevented the Allow header from being set.

Props killua99, noisysocks.
Fixes #45753.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r44452 r44933  
    188188        sort( $keys );
    189189        $this->assertEquals( array( 'context', 'id', 'password' ), $keys );
     190    }
     191
     192    /**
     193     * @ticket 43701
     194     */
     195    public function test_allow_header_sent_on_options_request() {
     196        $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     197        $response = rest_get_server()->dispatch( $request );
     198        $response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request );
     199        $headers  = $response->get_headers();
     200
     201        $this->assertNotEmpty( $headers['Allow'] );
     202        $this->assertEquals( $headers['Allow'], 'GET' );
     203
     204        wp_set_current_user( self::$editor_id );
     205        $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     206        $response = rest_get_server()->dispatch( $request );
     207        $response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request );
     208        $headers  = $response->get_headers();
     209
     210        $this->assertNotEmpty( $headers['Allow'] );
     211        $this->assertEquals( $headers['Allow'], 'GET, POST, PUT, PATCH, DELETE' );
    190212    }
    191213
Note: See TracChangeset for help on using the changeset viewer.