WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/19/2019 03:21:28 AM (18 months 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-attachments-controller.php

    r44785 r44933  
    207207        sort( $keys );
    208208        $this->assertEquals( array( 'context', 'id' ), $keys );
     209    }
     210
     211    /**
     212     * @ticket 43701
     213     */
     214    public function test_allow_header_sent_on_options_request() {
     215        $id1      = $this->factory->attachment->create_object(
     216            $this->test_file,
     217            0,
     218            array(
     219                'post_mime_type' => 'image/jpeg',
     220                'post_excerpt'   => 'A sample caption',
     221            )
     222        );
     223        $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/media/%d', $id1 ) );
     224        $response = rest_get_server()->dispatch( $request );
     225        $response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request );
     226        $headers  = $response->get_headers();
     227
     228        $this->assertNotEmpty( $headers['Allow'] );
     229        $this->assertEquals( $headers['Allow'], 'GET' );
     230
     231        wp_set_current_user( self::$editor_id );
     232        $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/media/%d', $id1 ) );
     233        $response = rest_get_server()->dispatch( $request );
     234        $response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request );
     235        $headers  = $response->get_headers();
     236
     237        $this->assertNotEmpty( $headers['Allow'] );
     238        $this->assertEquals( $headers['Allow'], 'GET, POST, PUT, PATCH, DELETE' );
    209239    }
    210240
Note: See TracChangeset for help on using the changeset viewer.