WordPress.org

Make WordPress Core

Changeset 51960


Ignore:
Timestamp:
10/31/2021 06:06:02 AM (3 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Send a 500 status code when JSON encoding fails.

Previously, a 200 status code would be sent despite the 500 status code present in the response body.

Props hermpheus, lalitjalandhar.
Fixes #53056.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r51915 r51960  
    498498
    499499            if ( $json_error_message ) {
     500                $this->set_status( 500 );
    500501                $json_error_obj = new WP_Error(
    501502                    'rest_encode_error',
  • trunk/tests/phpunit/includes/spy-rest-server.php

    r47253 r51960  
    77    public $last_request        = null;
    88    public $override_by_default = false;
     9    public $status              = null;
    910
    1011    /**
     
    3637    public function send_header( $header, $value ) {
    3738        $this->sent_headers[ $header ] = $value;
     39    }
     40
     41    /**
     42     * Stores last set status.
     43     * @param int $code HTTP status.
     44     */
     45    public function set_status( $status ) {
     46        $this->status = $status;
    3847    }
    3948
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r51657 r51960  
    20232023    }
    20242024
     2025    /**
     2026     * @ticket 53056
     2027     */
     2028    public function test_json_encode_error_results_in_500_status_code() {
     2029        register_rest_route(
     2030            'test-ns/v1',
     2031            '/test',
     2032            array(
     2033                array(
     2034                    'methods'             => \WP_REST_Server::READABLE,
     2035                    'callback'            => function() {
     2036                        return new \WP_REST_Response( INF );
     2037                    },
     2038                    'permission_callback' => '__return_true',
     2039                    'args'                => array(),
     2040                ),
     2041            )
     2042        );
     2043        rest_get_server()->serve_request( '/test-ns/v1/test' );
     2044        $this->assertSame( 500, rest_get_server()->status );
     2045    }
     2046
    20252047    public function _validate_as_integer_123( $value, $request, $key ) {
    20262048        if ( ! is_int( $value ) ) {
Note: See TracChangeset for help on using the changeset viewer.