Make WordPress Core

Ticket #39933: 0001-for-39933.-Allows-for-passing-a-request-body-in-DELE.patch

File 0001-for-39933.-Allows-for-passing-a-request-body-in-DELE.patch, 3.6 KB (added by mnelson4, 8 years ago)

allows you to pass request body on DELETE requests over the REST API

  • src/wp-includes/rest-api/class-wp-rest-request.php

    From 6136d80565cd7be043041cb9a44d109c6677808c Mon Sep 17 00:00:00 2001
    From: Mike Nelson <michael@eventespresso.com>
    Date: Wed, 22 Feb 2017 14:30:31 -0800
    Subject: [PATCH] for 39933. Allows for passing a request body in DELETE
     requests over the REST API. Adds a unit test to verify request bodies are
     received over DELETE and PATCH requests
    
    ---
     src/wp-includes/rest-api/class-wp-rest-request.php |  2 +-
     tests/phpunit/tests/rest-api/rest-request.php      | 65 +++++++++++++++-------
     2 files changed, 46 insertions(+), 21 deletions(-)
    
    diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php
    index 4dd0dc2..74a81c9 100644
    a b class WP_REST_Request implements ArrayAccess { 
    364364                        $this->parse_body_params();
    365365                }
    366366
    367                 $accepts_body_data = array( 'POST', 'PUT', 'PATCH' );
     367                $accepts_body_data = array( 'POST', 'PUT', 'PATCH', 'DELETE' );
    368368                if ( in_array( $this->method, $accepts_body_data ) ) {
    369369                        $order[] = 'POST';
    370370                }
  • tests/phpunit/tests/rest-api/rest-request.php

    diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php
    index 7953f65..a6b9885 100644
    a b class Tests_REST_Request extends WP_UnitTestCase { 
    208208         * WP_REST_Request does it for us.
    209209         */
    210210        public function test_parameters_for_put() {
    211                 $data = array(
    212                         'foo' => 'bar',
    213                         'alot' => array(
    214                                 'of' => 'parameters',
    215                         ),
    216                         'list' => array(
    217                                 'of',
    218                                 'cool',
    219                                 'stuff',
    220                         ),
    221                 );
    222 
    223                 $this->request->set_method( 'PUT' );
    224                 $this->request->set_body_params( array() );
    225                 $this->request->set_body( http_build_query( $data ) );
    226 
    227                 foreach ( $data as $key => $expected_value ) {
    228                         $this->assertEquals( $expected_value, $this->request->get_param( $key ) );
    229                 }
    230         }
     211                $this->_test_parameters_request_method('PUT');
     212        }
     213
     214    /**
     215     * PATCH requests don't get $_POST automatically parsed, so ensure that
     216     * WP_REST_Request does it for us.
     217     */
     218    public function test_parameters_for_patch() {
     219        $this->_test_parameters_request_method('PATCH');
     220    }
     221
     222    /**
     223     * DELETE requests don't get $_POST automatically parsed, so ensure that
     224     * WP_REST_Request does it for us.
     225     */
     226    public function test_parameters_for_delete() {
     227        $this->_test_parameters_request_method('DELETE');
     228    }
     229
     230    /**
     231     * Test logic shared between various test methods
     232     * @param string $request_method
     233     */
     234        protected function _test_parameters_request_method( $request_method = 'POST' ){
     235        $data = array(
     236            'foo' => 'bar',
     237            'alot' => array(
     238                'of' => 'parameters',
     239            ),
     240            'list' => array(
     241                'of',
     242                'cool',
     243                'stuff',
     244            ),
     245        );
     246
     247        $this->request->set_method( $request_method );
     248        $this->request->set_body_params( array() );
     249        $this->request->set_body( http_build_query( $data ) );
     250
     251        foreach ( $data as $key => $expected_value ) {
     252            $this->assertEquals( $expected_value, $this->request->get_param( $key ), 'Using request method ' . $request_method );
     253        }
     254    }
    231255
    232256        public function test_parameters_for_json_put() {
    233257                $data = array(
    class Tests_REST_Request extends WP_UnitTestCase { 
    251275                }
    252276        }
    253277
     278
    254279        public function test_parameters_for_json_post() {
    255280                $data = array(
    256281                        'foo' => 'bar',