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 we can properly set an existing cookie jar * */ public function testSetReadyCookieJar() { $jar = new Http\CookieJar(); $jar->addCookie('cookie=value', 'http://www.example.com'); $jar->addCookie('chocolate=chips; path=/foo', 'http://www.example.com'); $this->_client->setCookieJar($jar); // Check we got the right cookiejar $this->assertEquals($jar, $this->_client->getCookieJar(), '$jar is not the client\'s cookie jar as expected'); }
/** * 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; }