HTTP: Add some new Cookie helper functions:

* `wp_remote_retrieve_cookies( $response )`
* `wp_remote_retrieve_cookie( $response, $name )`
* `wp_remote_retrieve_cookie_value( $response, $name )`

Adds unit tests.

Props johnbillion.
Fixes #33711.


git-svn-id: https://develop.svn.wordpress.org/trunk@34369 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-09-22 03:13:13 +00:00
parent 782b9085c9
commit e75c7651b2
2 changed files with 88 additions and 0 deletions

View File

@ -285,6 +285,66 @@ function wp_remote_retrieve_body( $response ) {
return $response['body'];
}
/**
* Retrieve only the body from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @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.
*/
function wp_remote_retrieve_cookies( $response ) {
if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
return array();
}
return $response['cookies'];
}
/**
* Retrieve a single cookie by name from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @param string $name The name of the cookie to retrieve.
* @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
*/
function wp_remote_retrieve_cookie( $response, $name ) {
$cookies = wp_remote_retrieve_cookies( $response );
if ( empty( $cookies ) ) {
return '';
}
foreach ( $cookies as $cookie ) {
if ( $cookie->name === $name ) {
return $cookie;
}
}
return '';
}
/**
* Retrieve a single cookie's value by name from the raw response.
*
* @since 4.4.0
*
* @param array $response HTTP response.
* @param string $name The name of the cookie to retrieve.
* @return string The value of the cookie. Empty string if the cookie isn't present in the response.
*/
function wp_remote_retrieve_cookie_value( $response, $name ) {
$cookie = wp_remote_retrieve_cookie( $response, $name );
if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
return '';
}
return $cookie->value;
}
/**
* Determines if there is an HTTP Transport that can process this request.
*

View File

@ -75,4 +75,32 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
$response = wp_remote_get( $url, array( 'redirection' => -1 ) );
$this->assertWPError( $response );
}
/**
* @ticket 33711
*/
function test_get_response_cookies() {
$url = 'https://wordpress.org/wp-login.php';
$response = wp_remote_head( $url );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertInternalType( 'array', $cookies );
$this->assertNotEmpty( $cookies );
$cookie = wp_remote_retrieve_cookie( $response, 'wordpress_test_cookie' );
$this->assertInstanceOf( 'WP_Http_Cookie', $cookie );
$this->assertSame( 'wordpress_test_cookie', $cookie->name );
$this->assertSame( 'WP Cookie check', $cookie->value );
$value = wp_remote_retrieve_cookie_value( $response, 'wordpress_test_cookie' );
$this->assertSame( 'WP Cookie check', $value );
$no_value = wp_remote_retrieve_cookie_value( $response, 'not_a_cookie' );
$this->assertSame( '', $no_value );
$no_cookie = wp_remote_retrieve_cookie( $response, 'not_a_cookie' );
$this->assertSame( '', $no_cookie );
}
}