/** * @dataProvider cookieDomains * @covers Cookie::validateCookieDomain */ public function testValidateCookieDomain($expected, $domain, $origin = null) { if ($origin) { $ok = Cookie::validateCookieDomain($domain, $origin); $msg = "{$domain} against origin {$origin}"; } else { $ok = Cookie::validateCookieDomain($domain); $msg = "{$domain}"; } $this->assertEquals($expected, $ok, $msg); }
/** * Parse the content of an Set-Cookie HTTP Response header. * * @param string $cookie * @param string $domain Cookie's domain * @return null */ public function parseCookieResponseHeader($cookie, $domain) { $len = strlen('Set-Cookie:'); if (substr_compare('Set-Cookie:', $cookie, 0, $len, true) === 0) { $cookie = substr($cookie, $len); } $bit = array_map('trim', explode(';', $cookie)); if (count($bit) >= 1) { list($name, $value) = explode('=', array_shift($bit), 2); $attr = array(); foreach ($bit as $piece) { $parts = explode('=', $piece); if (count($parts) > 1) { $attr[strtolower($parts[0])] = $parts[1]; } else { $attr[strtolower($parts[0])] = true; } } if (!isset($attr['domain'])) { $attr['domain'] = $domain; } elseif (!Cookie::validateCookieDomain($attr['domain'], $domain)) { return null; } $this->setCookie($name, $value, $attr); } }
function testValidateCookieDomain() { $this->assertFalse(Cookie::validateCookieDomain("co.uk")); $this->assertFalse(Cookie::validateCookieDomain(".co.uk")); $this->assertFalse(Cookie::validateCookieDomain("gov.uk")); $this->assertFalse(Cookie::validateCookieDomain(".gov.uk")); $this->assertTrue(Cookie::validateCookieDomain("supermarket.uk")); $this->assertFalse(Cookie::validateCookieDomain("uk")); $this->assertFalse(Cookie::validateCookieDomain(".uk")); $this->assertFalse(Cookie::validateCookieDomain("127.0.0.")); $this->assertFalse(Cookie::validateCookieDomain("127.")); $this->assertFalse(Cookie::validateCookieDomain("127.0.0.1.")); $this->assertTrue(Cookie::validateCookieDomain("127.0.0.1")); $this->assertFalse(Cookie::validateCookieDomain("333.0.0.1")); $this->assertTrue(Cookie::validateCookieDomain("example.com")); $this->assertFalse(Cookie::validateCookieDomain("example.com.")); $this->assertTrue(Cookie::validateCookieDomain(".example.com")); $this->assertTrue(Cookie::validateCookieDomain(".example.com", "www.example.com")); $this->assertFalse(Cookie::validateCookieDomain("example.com", "www.example.com")); $this->assertTrue(Cookie::validateCookieDomain("127.0.0.1", "127.0.0.1")); $this->assertFalse(Cookie::validateCookieDomain("127.0.0.1", "localhost")); }