/** * Return a subset of a domain-matching cookies that also match a specified path * * @param array $dom_array * @param string $path * @return array */ protected function _matchPath($domains, $path) { $ret = array(); foreach ($domains as $dom => $paths_array) { foreach (array_keys($paths_array) as $cpath) { if (Cookie::matchCookiePath($cpath, $path)) { if (! isset($ret[$dom])) { $ret[$dom] = array(); } $ret[$dom][$cpath] = $paths_array[$cpath]; } } } return $ret; }
/** * 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; }