/** * Add a cookie to the jar. Cookie should be passed either as a Zend_Http_Cookie object * or as a string - in which case an object is created from the string. * * @param \Zend\Http\Cookie|string $cookie * @param \Zend\Uri\Url|string $ref_uri Optional reference URI (for domain, path, secure) */ public function addCookie($cookie, $ref_uri = null) { if (is_string($cookie)) { $cookie = Cookie::fromString($cookie, $ref_uri); } if ($cookie instanceof Cookie) { $domain = $cookie->getDomain(); $path = $cookie->getPath(); if (! isset($this->cookies[$domain])) $this->cookies[$domain] = array(); if (! isset($this->cookies[$domain][$path])) $this->cookies[$domain][$path] = array(); $this->cookies[$domain][$path][$cookie->getName()] = $cookie; $this->_rawCookies[] = $cookie; } else { throw new Exception\InvalidArgumentException('Supplient argument is not a valid cookie string or object'); } }
/** * Make sure we can set cookie objects with a jar * */ public function testSetCookieObjectJar() { $this->client->setUri($this->baseuri . 'testCookies.php'); $this->client->setCookieJar(); $refuri = $this->client->getUri(); $cookies = array(HTTP\Cookie::fromString('chocolate=chips', $refuri), HTTP\Cookie::fromString('crumble=apple', $refuri)); $strcookies = array(); foreach ($cookies as $c) { $this->client->setCookie($c); $strcookies[$c->getName()] = $c->getValue(); } $res = $this->client->request(); $this->assertEquals($res->getBody(), serialize($strcookies), 'Response body does not contain the expected cookies'); }
public function testIteratorAndCountable() { $jar = new HTTP\CookieJar(); $cookies = array(HTTP\Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'), HTTP\Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')); foreach ($cookies as $cookie) { $jar->addCookie($cookie); } foreach ($jar as $cookie) { $this->assertType('Zend\\HTTP\\Cookie', $cookie); } $this->assertEquals(2, count($jar)); $this->assertFalse($jar->isEmpty()); $jar->reset(); $this->assertTrue($jar->isEmpty()); }
/** * Test that cookies with far future expiry date (beyond the 32 bit unsigned int range) are * not mistakenly marked as 'expired' * * @link http://framework.zend.com/issues/browse/ZF-5690 */ public function testZF5690OverflowingExpiryDate() { $expTime = "Sat, 29-Jan-2039 00:54:42 GMT"; $cookie = Http\Cookie::fromString("foo=bar; domain=.example.com; expires=$expTime"); $this->assertFalse($cookie->isExpired(), 'Expiry: ' . $cookie->getExpiryTime()); }
/** * Add a cookie to the request. If the client has no Cookie Jar, the cookies * will be added directly to the headers array as "Cookie" headers. * * @param \Zend\Http\Cookie|string $cookie * @param string|null $value If "cookie" is a string, this is the cookie value. * @return \Zend\Http\Client * @throws \Zend\Http\Client\Exception */ public function setCookie($cookie, $value = null) { if (is_array($cookie)) { foreach ($cookie as $c => $v) { if (is_string($c)) { $this->setCookie($c, $v); } else { $this->setCookie($v); } } return $this; } if ($value !== null && $this->config['encodecookies']) { $value = urlencode($value); } if (isset($this->cookiejar)) { if ($cookie instanceof Cookie) { $this->cookiejar->addCookie($cookie); } elseif (is_string($cookie) && $value !== null) { $cookie = Cookie::fromString("{$cookie}={$value}", $this->uri, $this->config['encodecookies']); $this->cookiejar->addCookie($cookie); } } else { if ($cookie instanceof Cookie) { $name = $cookie->getName(); $value = $cookie->getValue(); $cookie = $name; } if (preg_match("/[=,; \t\r\n\v\f]/", $cookie)) { throw new Client\Exception("Cookie name cannot contain these characters: =,; \t\r\n\v\f ({$cookie})"); } $value = addslashes($value); if (!isset($this->headers['cookie'])) { $this->headers['cookie'] = array('Cookie', ''); } $this->headers['cookie'][1] .= $cookie . '=' . $value . '; '; } return $this; }