From 194c8d0e33066e0fd7b1ea71a85044609f1f548f Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Wed, 6 Jul 2016 17:50:44 +0000 Subject: [PATCH] HTTP API: Switch back to returning an array. The array-compatibility object we started returning in r37428 unfortunately isn't enough like an array. In particular, `is_array()` checks fail, despite the object implementing ArrayAccess. Mea culpa. This moves the WP_HTTP_Response object to a new http_response key in the array, and changes the value back to an actual array. Fixes #37097. See #33055. git-svn-id: https://develop.svn.wordpress.org/trunk@37989 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-http.php | 9 +- .../class-wp-http-requests-response.php | 98 +++---------------- tests/phpunit/tests/http/functions.php | 9 ++ 3 files changed, 31 insertions(+), 85 deletions(-) diff --git a/src/wp-includes/class-http.php b/src/wp-includes/class-http.php index 510ae27208..c5de3e01d4 100644 --- a/src/wp-includes/class-http.php +++ b/src/wp-includes/class-http.php @@ -348,10 +348,14 @@ class WP_Http { $options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] ); try { - $response = Requests::request( $url, $headers, $data, $type, $options ); + $requests_response = Requests::request( $url, $headers, $data, $type, $options ); // Convert the response into an array - $response = new WP_HTTP_Requests_Response( $response, $r['filename'] ); + $http_response = new WP_HTTP_Requests_Response( $requests_response, $r['filename'] ); + $response = $http_response->to_array(); + + // Add the original object to the array. + $response['http_response'] = $http_response; } catch ( Requests_Exception $e ) { $response = new WP_Error( 'http_request_failed', $e->getMessage() ); @@ -382,6 +386,7 @@ class WP_Http { 'message' => false, ), 'cookies' => array(), + 'http_response' => null, ); } diff --git a/src/wp-includes/class-wp-http-requests-response.php b/src/wp-includes/class-wp-http-requests-response.php index d85dffc7e8..741d1d79bd 100644 --- a/src/wp-includes/class-wp-http-requests-response.php +++ b/src/wp-includes/class-wp-http-requests-response.php @@ -1,13 +1,13 @@ get_headers(); - - case 'body': - return $this->get_data(); - - case 'response': - return array( - 'code' => $this->get_status(), - 'message' => get_status_header_desc( $this->get_status() ), - ); - - case 'cookies': - return $this->get_cookies(); - - case 'filename': - return $this->filename; - } - - return null; - } - - /** - * Set an ArrayAccess value. - * - * This is for array access back-compat. - * - * @param string|int $key Array offset to set. - * @param mixed $value Value to set. - */ - public function offsetSet( $key, $value ) { - switch ( $key ) { - case 'headers': - $this->set_headers( $value ); - break; - - case 'body': - $this->set_data( $value ); - break; - - case 'response': - if ( isset( $value['code'] ) ) { - $this->set_status( $value['code'] ); - } - break; - - case 'filename': - $this->filename = $value; - break; - } - } - - /** - * Unset an ArrayAccess value. - * - * This is for array access back-compat. - * - * @param string|int $key Array offset to remove. - */ - public function offsetUnset( $key ) { - $this->offsetSet( $key, null ); + public function to_array() { + return array( + 'headers' => $this->get_headers(), + 'body' => $this->get_data(), + 'response' => array( + 'code' => $this->get_status(), + 'message' => get_status_header_desc( $this->get_status() ), + ), + 'cookies' => $this->get_cookies(), + 'filename' => $this->filename, + ); } } diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index 05bfc26266..69bd9ab238 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -24,6 +24,15 @@ class Tests_HTTP_Functions extends WP_UnitTestCase { $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); } + /** + * @depends test_head_request + */ + function test_returns_array() { + $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; + $response = wp_remote_head( $url ); + $this->assertInternalType( 'array', $response ); + } + function test_head_redirect() { // this url will 301 redirect $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';