Make WordPress Core


Ignore:
Timestamp:
07/27/2016 03:31:48 PM (9 years ago)
Author:
ocean90
Message:

HTTP API: Normalize cookies before passing them to Requests.

Requests has its own cookie object in form of Requests_Cookie. Therefore we have to convert WP_Http_Cookie objects to Requests_Cookie.
This introduces WP_Http_Cookie::get_attributes() to retrieve cookie attributes of a WP_Http_Cookie object and WP_Http::normalize_cookies() to convert the cookie objects.

Fixes #37437.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/http/functions.php

    r37989 r38164  
    108108    }
    109109
     110    /**
     111     * @ticket 37437
     112     */
     113    function test_get_response_cookies_with_wp_http_cookie_object() {
     114        $url = 'http://example.org';
     115
     116        $response = wp_remote_get( $url, array(
     117            'cookies' => array(
     118                new WP_Http_Cookie( array( 'name' => 'test', 'value' => 'foo' ) ),
     119            ),
     120        ) );
     121        $cookies  = wp_remote_retrieve_cookies( $response );
     122
     123        $this->assertNotEmpty( $cookies );
     124
     125        $cookie = wp_remote_retrieve_cookie( $response, 'test' );
     126        $this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
     127        $this->assertSame( 'test', $cookie->name );
     128        $this->assertSame( 'foo', $cookie->value );
     129    }
     130
     131    /**
     132     * @ticket 37437
     133     */
     134    function test_get_response_cookies_with_name_value_array() {
     135        $url = 'http://example.org';
     136
     137        $response = wp_remote_get( $url, array(
     138            'cookies' => array(
     139                'test' => 'foo',
     140            ),
     141        ) );
     142        $cookies  = wp_remote_retrieve_cookies( $response );
     143
     144        $this->assertNotEmpty( $cookies );
     145
     146        $cookie = wp_remote_retrieve_cookie( $response, 'test' );
     147        $this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
     148        $this->assertSame( 'test', $cookie->name );
     149        $this->assertSame( 'foo', $cookie->value );
     150    }
    110151}
Note: See TracChangeset for help on using the changeset viewer.