Make WordPress Core

Ticket #37097: 37097.2.diff

File 37097.2.diff, 4.7 KB (added by rmccue, 9 years ago)

Remove @see and add unit test

  • src/wp-includes/class-http.php

     
    348348                $options['verify'] = apply_filters( 'https_ssl_verify', $options['verify'] );
    349349
    350350                try {
    351                         $response = Requests::request( $url, $headers, $data, $type, $options );
     351                        $requests_response = Requests::request( $url, $headers, $data, $type, $options );
    352352
    353353                        // Convert the response into an array
    354                         $response = new WP_HTTP_Requests_Response( $response, $r['filename'] );
     354                        $http_response = new WP_HTTP_Requests_Response( $requests_response, $r['filename'] );
     355                        $response = $http_response->to_array();
     356
     357                        // Add the original object to the array.
     358                        $response['http_response'] = $http_response;
    355359                }
    356360                catch ( Requests_Exception $e ) {
    357361                        $response = new WP_Error( 'http_request_failed', $e->getMessage() );
     
    382386                                        'message' => false,
    383387                                ),
    384388                                'cookies' => array(),
     389                                'http_response' => null,
    385390                        );
    386391                }
    387392
  • src/wp-includes/class-wp-http-requests-response.php

     
    11<?php
    22
    33/**
    4  * Wrapper object for a Requests_Response for compatibility.
     4 * Wrapper object for a Requests_Response for standardisation.
    55 *
    66 * @package WordPress
    77 * @subpackage HTTP
    88 * @since 4.6.0
    99 */
    10 class WP_HTTP_Requests_Response extends WP_HTTP_Response implements ArrayAccess {
     10class WP_HTTP_Requests_Response extends WP_HTTP_Response {
    1111        /**
    1212         * Requests Response object.
    1313         *
     
    142142        }
    143143
    144144        /**
    145          * Check if an ArrayAccess offset exists.
     145         * Convert the object to a WP_Http response array.
    146146         *
    147          * This is for array access back-compat.
    148          *
    149          * @param string|int $key Array offset.
    150          * @return bool True if the offset exists, false otherwise.
     147         * @return array WP_Http response array, per WP_Http::request().
    151148         */
    152         public function offsetExists( $key ) {
    153                 $allowed = array( 'headers', 'body', 'response', 'cookies', 'filename' );
    154                 return in_array( $key, $allowed );
     149        public function to_array() {
     150                return array(
     151                        'headers' => $this->get_headers(),
     152                        'body' => $this->get_data(),
     153                        'response' => array(
     154                                'code'    => $this->get_status(),
     155                                'message' => get_status_header_desc( $this->get_status() ),
     156                        ),
     157                        'cookies' => $this->get_cookies(),
     158                        'filename' => $this->filename,
     159                );
    155160        }
    156 
    157         /**
    158          * Get an ArrayAccess value.
    159          *
    160          * This is for array access back-compat.
    161          *
    162          * @param string|int $key Array offset to get.
    163          * @return mixed Value if the key is a valid offset, null if invalid.
    164          */
    165         public function offsetGet( $key ) {
    166                 switch ( $key ) {
    167                         case 'headers':
    168                                 return $this->get_headers();
    169 
    170                         case 'body':
    171                                 return $this->get_data();
    172 
    173                         case 'response':
    174                                 return array(
    175                                         'code'    => $this->get_status(),
    176                                         'message' => get_status_header_desc( $this->get_status() ),
    177                                 );
    178 
    179                         case 'cookies':
    180                                 return $this->get_cookies();
    181 
    182                         case 'filename':
    183                                 return $this->filename;
    184                 }
    185 
    186                 return null;
    187         }
    188 
    189         /**
    190          * Set an ArrayAccess value.
    191          *
    192          * This is for array access back-compat.
    193          *
    194          * @param string|int $key Array offset to set.
    195          * @param mixed $value Value to set.
    196          */
    197         public function offsetSet( $key, $value ) {
    198                 switch ( $key ) {
    199                         case 'headers':
    200                                 $this->set_headers( $value );
    201                                 break;
    202 
    203                         case 'body':
    204                                 $this->set_data( $value );
    205                                 break;
    206 
    207                         case 'response':
    208                                 if ( isset( $value['code'] ) ) {
    209                                         $this->set_status( $value['code'] );
    210                                 }
    211                                 break;
    212 
    213                         case 'filename':
    214                                 $this->filename = $value;
    215                                 break;
    216                 }
    217         }
    218 
    219         /**
    220          * Unset an ArrayAccess value.
    221          *
    222          * This is for array access back-compat.
    223          *
    224          * @param string|int $key Array offset to remove.
    225          */
    226         public function offsetUnset( $key ) {
    227                 $this->offsetSet( $key, null );
    228         }
    229161}
  • tests/phpunit/tests/http/functions.php

     
    2424                $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
    2525        }
    2626
     27        /**
     28         * @depends test_head_request
     29         */
     30        function test_returns_array() {
     31                $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
     32                $response = wp_remote_head( $url );
     33                $this->assertInternalType( 'array', $response );
     34        }
     35
    2736        function test_head_redirect() {
    2837                // this url will 301 redirect
    2938                $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';