Make WordPress Core

Changeset 45809


Ignore:
Timestamp:
08/15/2019 07:55:13 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Do not send response body if status is 204 or body is null.

Status code 204 should indicate no response body is sent. Previously, a "null" string was sent, which MacOS Safari would try to parse as JSON and thereby fail to complete the request.

Props TimothyBlynJacobs, andizer, matthias.thiel.
Fixes #43691.

Location:
trunk
Files:
2 edited

Legend:

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

    r45687 r45809  
    405405                         */
    406406                        $result = apply_filters( 'rest_pre_echo_response', $result, $this, $request );
     407
     408                        // The 204 response shouldn't have a body.
     409                        if ( 204 === $code || null === $result ) {
     410                                return null;
     411                        }
    407412
    408413                        $result = wp_json_encode( $result );
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r43652 r45809  
    11451145        }
    11461146
     1147        /**
     1148         * @ticket 43691
     1149         */
     1150        public function test_does_not_echo_body_for_null_responses() {
     1151                register_rest_route(
     1152                        'test-ns',
     1153                        '/test',
     1154                        array(
     1155                                'methods'  => array( 'GET' ),
     1156                                'callback' => function () {
     1157                                        return new WP_REST_Response();
     1158                                },
     1159                        )
     1160                );
     1161
     1162                $result = rest_get_server()->serve_request( '/test-ns/test' );
     1163
     1164                $this->assertNull( $result );
     1165                $this->assertEquals( '', rest_get_server()->sent_body );
     1166        }
     1167
     1168        /**
     1169         * @ticket 43691
     1170         */
     1171        public function test_does_not_echo_body_for_responses_with_204_status() {
     1172                register_rest_route(
     1173                        'test-ns',
     1174                        '/test',
     1175                        array(
     1176                                'methods'  => array( 'GET' ),
     1177                                'callback' => function () {
     1178                                        return new WP_REST_Response( 'data', 204 );
     1179                                },
     1180                        )
     1181                );
     1182
     1183                $result = rest_get_server()->serve_request( '/test-ns/test' );
     1184
     1185                $this->assertNull( $result );
     1186                $this->assertEquals( '', rest_get_server()->sent_body );
     1187        }
     1188
    11471189        public function _validate_as_integer_123( $value, $request, $key ) {
    11481190                if ( ! is_int( $value ) ) {
Note: See TracChangeset for help on using the changeset viewer.