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'); }
/** * Prepare the request headers * * @return array */ protected function _prepareHeaders() { $headers = array(); // Set the host header if (!isset($this->headers['host'])) { $host = $this->uri->getHost(); // If the port is not default, add it if (!($this->uri->getScheme() == 'http' && $this->uri->getPort() == 80 || $this->uri->getScheme() == 'https' && $this->uri->getPort() == 443)) { $host .= ':' . $this->uri->getPort(); } $headers[] = "Host: {$host}"; } // Set the connection header if (!isset($this->headers['connection'])) { if (!$this->config['keepalive']) { $headers[] = "Connection: close"; } } // Set the Accept-encoding header if not set - depending on whether // zlib is available or not. if (!isset($this->headers['accept-encoding'])) { if (function_exists('gzinflate')) { $headers[] = 'Accept-encoding: gzip, deflate'; } else { $headers[] = 'Accept-encoding: identity'; } } // Set the Content-Type header if ($this->method == self::POST && (!isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) { $headers[] = self::CONTENT_TYPE . ': ' . $this->enctype; } // Set the user agent header if (!isset($this->headers['user-agent']) && isset($this->config['useragent'])) { $headers[] = "User-Agent: {$this->config['useragent']}"; } // Set HTTP authentication if needed if (is_array($this->auth)) { $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'], $this->auth['type']); $headers[] = "Authorization: {$auth}"; } // Load cookies from cookie jar if (isset($this->cookiejar)) { $cookstr = $this->cookiejar->getMatchingCookies($this->uri, true, CookieJar::COOKIE_STRING_CONCAT); if ($cookstr) { $headers[] = "Cookie: {$cookstr}"; } } // Add all other user defined headers foreach ($this->headers as $header) { list($name, $value) = $header; if (is_array($value)) { $value = implode(', ', $value); } $headers[] = "{$name}: {$value}"; } return $headers; }