public function testExceptAddCookiesInvalidResponse() { $jar = new Zend_Http_Cookiejar(); try { $jar->addCookiesFromResponse('somestring', 'http://www.example.com'); $this->fail('Excepted exception was not thrown'); } catch (Zend_Http_Exception $e) { // We are ok } try { $jar->addCookiesFromResponse(new stdClass(), 'http://www.example.com'); $this->fail('Excepted exception was not thrown'); } catch (Zend_Http_Exception $e) { // We are ok } }
/** * Test we get an exception in case of invalid response objects * * @dataProvider invalidResponseProvider * @expectedException Zend_Http_Exception */ public function testExceptAddCookiesInvalidResponse($resp) { $jar = new Zend_Http_Cookiejar(); $jar->addCookiesFromResponse($resp, 'http://www.example.com'); }
/** * Create a new CookieJar object and automatically load into it all the * cookies set in an Http_Response object. If $uri is set, it will be * considered as the requested URI for setting default domain and path * of the cookie. * * @param Zend_Http_Response $response HTTP Response object * @param Zend_Uri_Http|string $uri The requested URI * @return Zend_Http_Cookiejar * @todo Add the $uri functionality. */ public static function factory(Zend_Http_Response $response, $ref_uri) { $jar = new Zend_Http_Cookiejar(); $jar->addCookiesFromResponse($response, $ref_uri); return $jar; }
/** * Send the HTTP request and return an HTTP response object * * @param string $method * @return Zend_Http_Response */ public function request($method = null) { $this->redirectCounter = 0; $response = null; // Send the first request. If redirected, continue. do { $response = parent::request($method); // Load cookies into cookie jar if (isset($this->Cookiejar)) { $this->Cookiejar->addCookiesFromResponse($response, $this->uri); } // If we got redirected, look for the Location header if ($response->isRedirect() && ($location = $response->getHeader('location'))) { // Check whether we send the exact same request again, or drop the parameters // and send a GET request if ($response->getStatus() == 303 || !$this->doStrictRedirects && ($response->getStatus() == 302 || $response->getStatus() == 301)) { $this->resetParameters(); $this->setMethod(self::METHOD_GET); } // If we got a well formed absolute URI if (Zend_Uri_Http::check($location)) { $this->setUri($location); } else { // Split into path and query and set the query list($location, $query) = explode('?', $location, 2); $this->uri->setQueryString($query); // Else, if we got just an absolute path, set it if (strpos($location, '/') === 0) { $this->uri->setPath($location); // Else, assume we have a relative path } else { // Get the current path directory, removing any trailing slashes $path = rtrim(dirname($this->uri->getPath()), "/"); $this->uri->setPath($path . '/' . $location); } } $this->redirectCounter++; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->maxRedirects); return $response; }