Ticket #45753: 45753.3.diff
File 45753.3.diff, 4.8 KB (added by , 6 years ago) |
---|
-
src/wp-includes/rest-api.php
diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php index a2e3363951..a541e1dadd 100644
function rest_send_cors_headers( $value ) { 604 604 * @param mixed $response Current response, either response or `null` to indicate pass-through. 605 605 * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server). 606 606 * @param WP_REST_Request $request The request that was used to make current response. 607 * 607 608 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. 608 609 */ 609 610 function rest_handle_options_request( $response, $handler, $request ) { … … function rest_handle_options_request( $response, $handler, $request ) { 615 616 $data = array(); 616 617 617 618 foreach ( $handler->get_routes() as $route => $endpoints ) { 618 $match = preg_match( '@^' . $route . '$@i', $request->get_route() );619 $match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches ); 619 620 620 621 if ( ! $match ) { 621 622 continue; 622 623 } 623 624 625 $args = array(); 626 foreach ( $matches as $param => $value ) { 627 if ( ! is_int( $param ) ) { 628 $args[ $param ] = $value; 629 } 630 } 631 632 foreach ( $endpoints as $endpoint ) { 633 // Remove the redundant preg_match argument. 634 unset( $args[0] ); 635 636 $request->set_url_params( $args ); 637 $request->set_attributes( $endpoint ); 638 } 639 624 640 $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); 625 641 $response->set_matched_route( $route ); 626 642 break; 627 643 } 628 644 629 645 $response->set_data( $data ); 646 630 647 return $response; 631 648 } 632 649 -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php index 3ad2b09a72..556cc6c8be 100644
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 208 208 $this->assertEquals( array( 'context', 'id' ), $keys ); 209 209 } 210 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' ); 239 } 240 211 241 public function test_get_items() { 212 242 wp_set_current_user( 0 ); 213 243 $id1 = $this->factory->attachment->create_object( -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php index ee01eb717e..40d609c75d 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 189 189 $this->assertEquals( array( 'context', 'id', 'password' ), $keys ); 190 190 } 191 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' ); 212 } 213 192 214 public function test_get_items() { 193 215 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 194 216 $response = rest_get_server()->dispatch( $request );