Make WordPress Core


Ignore:
Timestamp:
05/03/2020 10:56:01 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Accept POST requests in the block renderer endpoint.

Rendering a block is idempotent, so a GET is the most natural request method. However, the maximum length of URLs prevented blocks with large attributes from being rendered.

Props ryankienstra.
Fixes #49680.

File:
1 edited

Legend:

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

    r47360 r47756  
    457457
    458458    /**
     459     * Test a POST request, with the attributes in the body.
     460     *
     461     * @ticket 49680
     462     */
     463    public function test_get_item_post_request() {
     464        wp_set_current_user( self::$user_id );
     465        $string_attribute = 'Lorem ipsum dolor';
     466        $attributes       = array( 'some_string' => $string_attribute );
     467        $request          = new WP_REST_Request( 'POST', self::$rest_api_route . self::$block_name );
     468        $request->set_param( 'context', 'edit' );
     469        $request->set_header( 'content-type', 'application/json' );
     470        $request->set_body( wp_json_encode( compact( 'attributes' ) ) );
     471        $response = rest_get_server()->dispatch( $request );
     472
     473        $this->assertEquals( 200, $response->get_status() );
     474        $this->assertContains( $string_attribute, $response->get_data()['rendered'] );
     475    }
     476
     477    /**
    459478     * Test getting item with invalid post ID.
    460479     *
     
    504523        $data     = $response->get_data();
    505524
    506         $this->assertEqualSets( array( 'GET' ), $data['endpoints'][0]['methods'] );
     525        $this->assertEqualSets( array( 'GET', 'POST' ), $data['endpoints'][0]['methods'] );
    507526        $this->assertEqualSets(
    508527            array( 'name', 'context', 'attributes', 'post_id' ),
Note: See TracChangeset for help on using the changeset viewer.