Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Changed fromResponse() method to SetCookie header
Browse files Browse the repository at this point in the history
  • Loading branch information
iwalz committed Dec 24, 2012
1 parent 4eae49a commit 7f470d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 7 additions & 3 deletions library/Zend/Http/Client/Cookies.php
Expand Up @@ -11,7 +11,8 @@
namespace Zend\Http\Client;

use ArrayIterator;
use Zend\Http\Header\Cookie;
use Zend\Http\Header\SetCookie;
use Zend\Stdlib\ArrayUtils;
use Zend\Http\Response;
use Zend\Uri;

Expand Down Expand Up @@ -93,7 +94,7 @@ public function __construct()
* Add a cookie to the class. Cookie should be passed either as a Zend\Http\Header\Cookie object
* or as a string - in which case an object is created from the string.
*
* @param Cookie|string $cookie
* @param SetCookie|string $cookie
* @param Uri\Uri|string $ref_uri Optional reference URI (for domain, path, secure)
* @throws Exception\InvalidArgumentException if invalid $cookie value
*/
Expand All @@ -103,7 +104,7 @@ public function addCookie($cookie, $ref_uri = null)
$cookie = Cookie::fromString($cookie, $ref_uri);
}

if ($cookie instanceof Cookie) {
if ($cookie instanceof SetCookie) {
$domain = $cookie->getDomain();
$path = $cookie->getPath();
if (!isset($this->cookies[$domain])) {
Expand All @@ -128,6 +129,9 @@ public function addCookie($cookie, $ref_uri = null)
public function addCookiesFromResponse(Response $response, $ref_uri)
{
$cookie_hdrs = $response->getHeaders()->get('Set-Cookie');
if ($cookie_hdrs instanceof \ArrayIterator) {
$cookie_hdrs = ArrayUtils::iteratorToArray($cookie_hdrs);
}

if (is_array($cookie_hdrs)) {
foreach ($cookie_hdrs as $cookie) {
Expand Down
15 changes: 14 additions & 1 deletion tests/ZendTest/Http/CookiesTest.php
Expand Up @@ -27,8 +27,21 @@ public function testFromResponseInSetCookie()
$headers->addHeader($header);
$response->setHeaders($headers);


$response = \Zend\Http\Cookies::fromResponse($response, "http://www.zend.com");
$this->assertSame($header, $response->getCookie('http://www.zend.com', 'foo'));
}

public function testFromResponseInCookie()
{
$response = new Response();
$headers = new Headers();
$header = new \Zend\Http\Header\SetCookie("foo", "bar");
$header->setDomain("www.zend.com");
$header->setPath("/");
$headers->addHeader($header);
$response->setHeaders($headers);

$response = \Zend\Http\Client\Cookies::fromResponse($response, "http://www.zend.com");
$this->assertSame($header, $response->getCookie('http://www.zend.com', 'foo'));
}
}

0 comments on commit 7f470d7

Please sign in to comment.