/** * @ticket 33711 */ function test_get_response_cookies() { $url = 'https://login.wordpress.org/wp-login.php'; $response = wp_remote_head($url); $cookies = wp_remote_retrieve_cookies($response); $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); }
/** * 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 ''; }
/** * @ticket 37437 */ function test_get_response_cookies_with_name_value_array() { $url = 'http://example.org'; $response = wp_remote_get($url, array('cookies' => array('test' => 'foo'))); $cookies = wp_remote_retrieve_cookies($response); $this->assertNotEmpty($cookies); $cookie = wp_remote_retrieve_cookie($response, 'test'); $this->assertInstanceOf('WP_Http_Cookie', $cookie); $this->assertSame('test', $cookie->name); $this->assertSame('foo', $cookie->value); }