public function testSetRemoveCookie() { $response = new Response(); $foo = new Cookie('foo', 'bar'); $foo->setSecure(true); $this->assertSame($response, $response->setCookie($foo)); $this->assertSame([$foo], $response->getCookies()); $this->assertSame(['foo=bar; Secure'], $response->getHeaderAsArray('Set-Cookie')); $bar = new Cookie('bar', 'baz'); $bar->setHttpOnly(true); $this->assertSame($response, $response->setCookie($bar)); $this->assertSame([$foo, $bar], $response->getCookies()); $this->assertSame(['foo=bar; Secure', 'bar=baz; HttpOnly'], $response->getHeaderAsArray('Set-Cookie')); $this->assertSame($response, $response->removeCookies()); $this->assertSame([], $response->getCookies()); $this->assertSame([], $response->getHeaderAsArray('Set-Cookie')); }
public function testToString() { $cookie = new Cookie('foo', 'bar'); $this->assertSame('foo=bar', (string) $cookie); $cookie->setExpires(2000000000); $this->assertSame('foo=bar; Expires=Wed, 18 May 2033 03:33:20 +0000', (string) $cookie); $cookie->setExpires(0)->setDomain('example.com'); $this->assertSame('foo=bar; Domain=example.com', (string) $cookie); $cookie->setDomain(null)->setPath('/'); $this->assertSame('foo=bar; Path=/', (string) $cookie); $cookie->setPath(null)->setSecure(true); $this->assertSame('foo=bar; Secure', (string) $cookie); $cookie->setHttpOnly(true); $this->assertSame('foo=bar; Secure; HttpOnly', (string) $cookie); }
/** * Returns a key=value representation of this Cookie, that can be used in a Cookie header. * * @return string */ public function toString() { return $this->cookie->getName() . '=' . rawurlencode($this->cookie->getValue()); }
/** * Writes the session cookie to the Response. * * @param \Brick\Http\Response $response * * @return void */ public function handleResponse(Response $response) { $lifetime = $this->cookieParams['lifetime']; $expires = $lifetime == 0 ? 0 : time() + $lifetime; $cookie = new Cookie($this->cookieParams['name'], $this->id); $cookie->setExpires($expires)->setPath($this->cookieParams['path'])->setDomain($this->cookieParams['domain'])->setSecure($this->cookieParams['secure'])->setHttpOnly($this->cookieParams['http-only']); $response->setCookie($cookie); }