Make WordPress Core

Ticket #33711: 33711.patch

File 33711.patch, 3.1 KB (added by johnbillion, 10 years ago)
  • src/wp-includes/http-functions.php

     
    290290}
    291291
    292292/**
     293 * Retrieve only the body from the raw response.
     294 *
     295 * @since 4.4.0
     296 *
     297 * @param array $response HTTP response.
     298 * @return array An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error.
     299 */
     300function wp_remote_retrieve_cookies( $response ) {
     301        if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
     302                return array();
     303        }
     304
     305        return $response['cookies'];
     306}
     307
     308/**
     309 * Retrieve a single cookie by name from the raw response.
     310 *
     311 * @since 4.4.0
     312 *
     313 * @param array  $response HTTP response.
     314 * @param string $name     The name of the cookie to retrieve.
     315 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
     316 */
     317function wp_remote_retrieve_cookie( $response, $name ) {
     318        $cookies = wp_remote_retrieve_cookies( $response );
     319
     320        if ( empty( $cookies ) ) {
     321                return '';
     322        }
     323
     324        foreach ( $cookies as $cookie ) {
     325                if ( $cookie->name === $name ) {
     326                        return $cookie;
     327                }
     328        }
     329
     330        return '';
     331}
     332
     333/**
     334 * Retrieve a single cookie's value by name from the raw response.
     335 *
     336 * @since 4.4.0
     337 *
     338 * @param array  $response HTTP response.
     339 * @param string $name     The name of the cookie to retrieve.
     340 * @return string The value of the cookie. Empty string if the cookie isn't present in the response.
     341 */
     342function wp_remote_retrieve_cookie_value( $response, $name ) {
     343        $cookie = wp_remote_retrieve_cookie( $response, $name );
     344
     345        if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
     346                return '';
     347        }
     348
     349        return $cookie->value;
     350}
     351
     352/**
    293353 * Determines if there is an HTTP Transport that can process this request.
    294354 *
    295355 * @since 3.2.0
  • tests/phpunit/tests/http/functions.php

     
    8383                $headers = wp_get_http( $url, $file, 6 );
    8484                $this->assertFalse( $headers );
    8585        }
     86
     87        /**
     88         * @ticket 33711
     89         */
     90        function test_get_response_cookies() {
     91                $url = 'https://wordpress.org/wp-login.php';
     92
     93                $response = wp_remote_head( $url );
     94                $cookies  = wp_remote_retrieve_cookies( $response );
     95
     96                $this->assertInternalType( 'array', $cookies );
     97                $this->assertNotEmpty( $cookies );
     98
     99                $cookie = wp_remote_retrieve_cookie( $response, 'wordpress_test_cookie' );
     100                $this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
     101                $this->assertSame( 'wordpress_test_cookie', $cookie->name );
     102                $this->assertSame( 'WP Cookie check', $cookie->value );
     103
     104                $value = wp_remote_retrieve_cookie_value( $response, 'wordpress_test_cookie' );
     105                $this->assertSame( 'WP Cookie check', $value );
     106
     107                $no_value = wp_remote_retrieve_cookie_value( $response, 'not_a_cookie' );
     108                $this->assertSame( '', $no_value );
     109
     110                $no_cookie = wp_remote_retrieve_cookie( $response, 'not_a_cookie' );
     111                $this->assertSame( '', $no_cookie );
     112        }
     113
    86114}