| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Http_CookieJar
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- require_once 'Zend/Http/CookieJar.php';
- /**
- * Zend_Http_CookieJar unit tests
- *
- * @category Zend
- * @package Zend_Http_CookieJar
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Http
- * @group Zend_Http_CookieJar
- */
- class Zend_Http_CookieJarTest extends PHPUnit_Framework_TestCase
- {
- public function loadResponse($filename)
- {
- $message = file_get_contents($filename);
- // Line endings are sometimes an issue inside the canned responses; the
- // following is a negative lookbehind assertion, and replaces any \n
- // not preceded by \r with the sequence \r\n, ensuring that the message
- // is well-formed.
- return preg_replace("#(?<!\r)\n#", "\r\n", $message);
- }
- /**
- * Test we can add cookies to the jar
- *
- */
- public function testAddCookie()
- {
- $jar = new Zend_Http_CookieJar();
- $this->assertEquals(0, count($jar->getAllCookies()), 'Cookie jar is expected to contain 0 cookies');
- $jar->addCookie('foo=bar; domain=example.com');
- $cookie = $jar->getCookie('http://example.com/', 'foo');
- $this->assertTrue($cookie instanceof Zend_Http_Cookie, '$cookie is expected to be a Cookie object');
- $this->assertEquals('bar', $cookie->getValue(), 'Cookie value is expected to be "bar"');
- $jar->addCookie('cookie=brownie; domain=geekz.co.uk;');
- $this->assertEquals(2, count($jar->getAllCookies()), 'Cookie jar is expected to contain 2 cookies');
- }
- /**
- * Check we get an expection if a non-valid cookie is passed to addCookie
- *
- */
- public function testExceptAddInvalidCookie()
- {
- $jar = new Zend_Http_CookieJar();
- try {
- $jar->addCookie('garbage');
- $this->fail('Expected exception was not thrown');
- } catch (Zend_Http_Exception $e) {
- // We are ok
- }
- try {
- $jar->addCookie(new Zend_Http_Cookiejar());
- $this->fail('Expected exception was not thrown');
- } catch (Zend_Http_Exception $e) {
- // We are ok
- }
- }
- /**
- * Test we can read cookies from a Response object
- *
- */
- public function testAddCookiesFromResponse()
- {
- $jar = new Zend_Http_Cookiejar();
- $res_str = $this->loadResponse(
- dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
- );
- $response = Zend_Http_Response::fromString($res_str);
- $jar->addCookiesFromResponse($response, 'http://www.example.com');
- $this->assertEquals(3, count($jar->getAllCookies()));
- $cookie_str = 'foo=bar;BOFH=Feature+was+not+beta+tested;time=1164234700;';
- $this->assertEquals($cookie_str, $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT));
- }
- /**
- * 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');
- }
- static public function invalidResponseProvider()
- {
- return array(
- array(new stdClass),
- array(null),
- array(12),
- array('hi')
- );
- }
- /**
- * Test we can get all cookies as an array of Cookie objects
- *
- */
- public function testGetAllCookies()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- 'name=Arthur; domain=camelot.gov.uk',
- 'quest=holy+grail; domain=forest.euwing.com',
- 'swallow=african; domain=bridge-of-death.net'
- );
- foreach ($cookies as $cookie) {
- $jar->addCookie($cookie);
- }
- $cobjects = $jar->getAllCookies();
- foreach ($cobjects as $id => $cookie) {
- $this->assertContains((string) $cookie, $cookies[$id]);
- }
- }
- /**
- * Test we can get all cookies as a concatenated string
- *
- */
- public function testGetAllCookiesAsConcat()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- 'name=Arthur; domain=camelot.gov.uk',
- 'quest=holy+grail; domain=forest.euwing.com',
- 'swallow=african; domain=bridge-of-death.net'
- );
- foreach ($cookies as $cookie) {
- $jar->addCookie($cookie);
- }
- $expected = 'name=Arthur;quest=holy+grail;swallow=african;';
- $real = $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT );
- $this->assertEquals($expected, $real, 'Concatenated string is not as expected');
- }
- /**
- * Test we can get all cookies as a concatenated string
- * @group ZF-11726
- */
- public function testGetAllCookiesAsConcatStrictMode()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- 'name=Arthur; domain=camelot.gov.uk',
- 'quest=holy+grail; domain=forest.euwing.com',
- 'swallow=african; domain=bridge-of-death.net'
- );
- foreach ($cookies as $cookie) {
- $jar->addCookie($cookie);
- }
- $expected = 'name=Arthur; quest=holy+grail; swallow=african';
- $real = $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT_STRICT);
- $this->assertEquals($expected, $real, 'Concatenated string is not as expected');
- }
- /**
- * Test we can get a single cookie as an object
- *
- */
- public function testGetCookieAsObject()
- {
- $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
- $jar = new Zend_Http_CookieJar();
- $jar->addCookie($cookie->__toString(), 'http://www.example.com/tests/');
- $cobj = $jar->getCookie('http://www.example.com/tests/', 'foo');
- $this->assertTrue($cobj instanceof Zend_Http_Cookie, '$cobj is not a Cookie object');
- $this->assertEquals($cookie->getName(), $cobj->getName(), 'Cookie name is not as expected');
- $this->assertEquals($cookie->getValue(), $cobj->getValue(), 'Cookie value is not as expected');
- $this->assertEquals($cookie->getDomain(), $cobj->getDomain(), 'Cookie domain is not as expected');
- $this->assertEquals($cookie->getPath(), $cobj->getPath(), 'Cookie path is not as expected');
- }
- /**
- * Check we can get a cookie as a string
- */
- public function testGetCookieAsString()
- {
- $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
- $jar = new Zend_Http_CookieJar();
- $jar->addCookie($cookie);
- $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
- $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
- $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
- $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
- }
- /**
- * Check we can get false when trying to get a non-existant cookie
- */
- public function testGetCookieReturnFalse()
- {
- $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
- $jar = new Zend_Http_CookieJar();
- $jar->addCookie($cookie);
- $cstr = $jar->getCookie('http://www.example.com/tests/', 'otherfoo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
- $this->assertFalse($cstr, 'getCookie was expected to return false, no such cookie');
- $cstr = $jar->getCookie('http://www.otherexample.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
- $this->assertFalse($cstr, 'getCookie was expected to return false, no such domain');
- $cstr = $jar->getCookie('http://www.example.com/othertests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
- $this->assertFalse($cstr, 'getCookie was expected to return false, no such path');
- }
- /**
- * Test we get a proper exception when an invalid URI is passed
- */
- public function testExceptGetCookieInvalidUri()
- {
- $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
- $jar = new Zend_Http_CookieJar();
- $jar->addCookie($cookie);
- try {
- $jar->getCookie('foo.com', 'foo');
- $this->fail('Expected getCookie to throw exception, invalid URI string passed');
- } catch (Zend_Exception $e) {
- // We are ok!
- }
- try {
- $jar->getCookie(Zend_Uri::factory('mailto:nobody@dev.null.com'), 'foo');
- $this->fail('Expected getCookie to throw exception, invalid URI object passed');
- } catch (Zend_Exception $e) {
- // We are ok!
- }
- }
- /**
- * Test we get a proper exception when an invalid return constant is passed
- *
- */
- public function testExceptGetCookieInvalidReturnType()
- {
- $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=example.com;');
- $jar = new Zend_Http_CookieJar();
- $jar->addCookie($cookie);
- try {
- $jar->getCookie('http://example.com/', 'foo', 5);
- $this->fail('Expected getCookie to throw exception, invalid return type');
- } catch (Zend_Http_Exception $e) {
- // We are ok!
- }
- }
- /**
- * Test we can get all matching cookies for a request, with session cookies
- *
- * @dataProvider cookieMatchTestProvider
- */
- public function testGetMatchingCookies($url, $expected)
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
- Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
- Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo7=bar7; domain=newwww.foo.com; path=/;'),
- Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- $cookies = $jar->getMatchingCookies($url);
- $this->assertEquals($expected, count($cookies), $jar->getMatchingCookies($url, true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT));
- }
- static public function cookieMatchTestProvider()
- {
- return array(
- array('http://www.foo.com/path/file.txt', 4),
- array('http://foo.com/path/file.txt', 3),
- array('https://www.foo.com/path/file.txt', 5),
- array('http://subdomain.foo.com/path', 4),
- array('http://subdomain.foo.com/otherpath', 3),
- array('http://blog.foo.com/news', 2)
- );
- }
- /**
- * Test we can get all matching cookies for a request, without session cookies
- */
- public function testGetMatchingCookiesNoSession()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
- Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
- Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', false);
- $this->assertEquals(3, count($cookies), 'Cookie count is expected to be 3');
- $cookies = $jar->getMatchingCookies('https://www.foo.com/path/file.txt', false);
- $this->assertEquals(4, count($cookies), 'Cookie count is expected to be 4');
- }
- /**
- * Test we can get all matching cookies for a request, when we set a different time for now
- */
- public function testGetMatchingCookiesWithTime()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 7200)),
- Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
- Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
- Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() - 7200)),
- Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() + 3700);
- $this->assertEquals(2, count($cookies), 'Cookie count is expected to be 2');
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() - 3700);
- $this->assertEquals(5, count($cookies), 'Cookie count is expected to be 5');
- }
- /**
- * Test we can get all matching cookies for a request, and return as strings array / concat
- */
- public function testGetMatchingCookiesAsStrings()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
- Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
- Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
- $this->assertTrue(is_array($cookies), '$cookies is expected to be an array, but it is not');
- $this->assertTrue(is_string($cookies[0]), '$cookies[0] is expected to be a string');;
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
- $this->assertTrue(is_string($cookies), '$cookies is expected to be a string');
- $expected = 'foo1=bar1;foo2=bar2;foo4=bar4;foo7=bar7;';
- $this->assertEquals($expected, $cookies, 'Concatenated string is not as expected');
- $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT_STRICT);
- $this->assertTrue(is_string($cookies), '$cookies is expected to be a string');
- $expected = 'foo1=bar1; foo2=bar2; foo4=bar4; foo7=bar7';
- $this->assertEquals($expected, $cookies, 'Concatenated string is not as expected');
- }
- /**
- * Test we get a proper exception when an invalid URI is passed
- */
- public function testExceptGetMatchingCookiesInvalidUri()
- {
- $jar = new Zend_Http_CookieJar();
- try {
- $cookies = $jar->getMatchingCookies('invalid.com', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
- $this->fail('Expected getMatchingCookies to throw exception, invalid URI string passed');
- } catch (Zend_Exception $e) {
- // We are ok!
- }
- try {
- $cookies = $jar->getMatchingCookies(new stdClass(), true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
- $this->fail('Expected getCookie to throw exception, invalid URI object passed');
- } catch (Zend_Exception $e) {
- // We are ok!
- }
- }
- /**
- * Test we can build a new object from a response object (single cookie header)
- */
- public function testFromResponse()
- {
- $res_str = $this->loadResponse(
- dirname(realpath(__FILE__)) . '/_files/response_with_single_cookie'
- );
- $response = Zend_Http_Response::fromString($res_str);
- $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
- $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
- $this->assertEquals(1, count($jar->getAllCookies()), 'CookieJar expected to contain 1 cookie');
- }
- /**
- * Test we can build a new object from a response object (multiple cookie headers)
- */
- public function testFromResponseMultiHeader()
- {
- $res_str = $this->loadResponse(
- dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
- );
- $response = Zend_Http_Response::fromString($res_str);
- $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
- $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
- $this->assertEquals(3, count($jar->getAllCookies()), 'CookieJar expected to contain 3 cookies');
- }
- /**
- * Make sure that paths with trailing slashes are matched as well as paths with no trailing slashes
- */
- public function testMatchPathWithTrailingSlash()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- $cookies = $jar->getMatchingCookies('http://www.example.com/a/b/file.txt');
- $this->assertTrue(is_array($cookies));
- $this->assertEquals(2, count($cookies));
- }
- public function testIteratorAndCountable()
- {
- $jar = new Zend_Http_CookieJar();
- $cookies = array(
- Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
- Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
- );
- foreach ($cookies as $cookie) $jar->addCookie($cookie);
- foreach ($jar as $cookie) {
- $this->assertTrue($cookie instanceof Zend_Http_Cookie);
- }
- $this->assertEquals(2, count($jar));
- $this->assertFalse($jar->isEmpty());
- $jar->reset();
- $this->assertTrue($jar->isEmpty());
- }
- }
|