/**
  * Processes Set-Cookie headers from a request/response pair.
  *
  * @param RequestInterface $request  A request object
  * @param MessageInterface $response A response object
  */
 public function processSetCookieHeaders(RequestInterface $request, MessageInterface $response)
 {
     foreach ($response->getHeader('Set-Cookie', false) as $header) {
         $cookie = new Cookie();
         $cookie->fromSetCookieHeader($header, parse_url($request->getHost(), PHP_URL_HOST));
         $this->addCookie($cookie);
     }
 }
Example #2
0
 /**
  * Adds a cookie to the current cookie jar.
  *
  * @param Cookie $cookie A cookie object
  */
 public function addCookie(Cookie $cookie)
 {
     foreach ($this->cookies as $key => $_cookie) {
         if ($_cookie->getName() == $cookie->getName()) {
             $this->cookies[$key] = $cookie;
             return;
         }
     }
     $this->cookies[] = $cookie;
 }
 public function testClearExpiredCookiesRemovesExpiredCookies()
 {
     $cookie = new Cookie();
     $cookie->setName('SESSION');
     $cookie->setValue('asdf');
     $cookie->setAttribute(Cookie::ATTR_EXPIRES, 'Fri, 01-Dec-1999 00:00:00 GMT');
     $jar = new CookieJar();
     $jar->addCookie($cookie);
     $jar->clearExpiredCookies();
     $this->assertEquals(0, count($jar->getCookies()));
     $cookie = new Cookie();
     $cookie->setName('SESSION');
     $cookie->setValue('asdf');
     $cookie->setAttribute(Cookie::ATTR_MAX_AGE, '-60');
     $jar = new CookieJar();
     $jar->addCookie($cookie);
     $jar->clearExpiredCookies();
     $this->assertEquals(0, count($jar->getCookies()));
 }
 public function testMatchesRequestChecksSecureAttribute()
 {
     $request = new Message\Request();
     $request->setHost('https://example.com');
     $cookie = new Cookie();
     $cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com');
     $cookie->setAttribute(Cookie::ATTR_SECURE, null);
     $this->assertTrue($cookie->matchesRequest($request));
     $request = new Message\Request();
     $request->setHost('http://example.com');
     $this->assertFalse($cookie->matchesRequest($request));
 }