summaryrefslogtreecommitdiff
path: root/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie')
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/ArrayCookieJarTest.php385
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/FileCookieJarTest.php63
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookiePluginTest.php134
-rw-r--r--vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php223
4 files changed, 805 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/ArrayCookieJarTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/ArrayCookieJarTest.php
new file mode 100644
index 0000000..5d0f668
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/ArrayCookieJarTest.php
@@ -0,0 +1,385 @@
+<?php
+
+namespace Guzzle\Tests\Plugin\Cookie\CookieJar;
+
+use Guzzle\Plugin\Cookie\Cookie;
+use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
+use Guzzle\Http\Message\Response;
+use Guzzle\Http\Message\Request;
+
+/**
+ * @covers Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar
+ */
+class ArrayCookieJarTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ /**
+ * @var ArrayCookieJar
+ */
+ private $jar;
+
+ public function setUp()
+ {
+ $this->jar = new ArrayCookieJar();
+ }
+
+ protected function getTestCookies()
+ {
+ return array(
+ new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'foo.com', 'path' => '/', 'discard' => true)),
+ new Cookie(array('name' => 'test', 'value' => '123', 'domain' => 'baz.com', 'path' => '/foo', 'expires' => 2)),
+ new Cookie(array('name' => 'you', 'value' => '123', 'domain' => 'bar.com', 'path' => '/boo', 'expires' => time() + 1000))
+ );
+ }
+
+ /**
+ * Provides test data for cookie cookieJar retrieval
+ */
+ public function getCookiesDataProvider()
+ {
+ return array(
+ array(array('foo', 'baz', 'test', 'muppet', 'googoo'), '', '', '', false),
+ array(array('foo', 'baz', 'muppet', 'googoo'), '', '', '', true),
+ array(array('googoo'), 'www.example.com', '', '', false),
+ array(array('muppet', 'googoo'), 'test.y.example.com', '', '', false),
+ array(array('foo', 'baz'), 'example.com', '', '', false),
+ array(array('muppet'), 'x.y.example.com', '/acme/', '', false),
+ array(array('muppet'), 'x.y.example.com', '/acme/test/', '', false),
+ array(array('googoo'), 'x.y.example.com', '/test/acme/test/', '', false),
+ array(array('foo', 'baz'), 'example.com', '', '', false),
+ array(array('baz'), 'example.com', '', 'baz', false),
+ );
+ }
+
+ public function testStoresAndRetrievesCookies()
+ {
+ $cookies = $this->getTestCookies();
+ foreach ($cookies as $cookie) {
+ $this->assertTrue($this->jar->add($cookie));
+ }
+
+ $this->assertEquals(3, count($this->jar));
+ $this->assertEquals(3, count($this->jar->getIterator()));
+ $this->assertEquals($cookies, $this->jar->all(null, null, null, false, false));
+ }
+
+ public function testRemovesExpiredCookies()
+ {
+ $cookies = $this->getTestCookies();
+ foreach ($this->getTestCookies() as $cookie) {
+ $this->jar->add($cookie);
+ }
+ $this->jar->removeExpired();
+ $this->assertEquals(array($cookies[0], $cookies[2]), $this->jar->all());
+ }
+
+ public function testRemovesTemporaryCookies()
+ {
+ $cookies = $this->getTestCookies();
+ foreach ($this->getTestCookies() as $cookie) {
+ $this->jar->add($cookie);
+ }
+ $this->jar->removeTemporary();
+ $this->assertEquals(array($cookies[2]), $this->jar->all());
+ }
+
+ public function testIsSerializable()
+ {
+ $this->assertEquals('[]', $this->jar->serialize());
+ $this->jar->unserialize('[]');
+ $this->assertEquals(array(), $this->jar->all());
+
+ $cookies = $this->getTestCookies();
+ foreach ($this->getTestCookies() as $cookie) {
+ $this->jar->add($cookie);
+ }
+
+ // Remove discard and expired cookies
+ $serialized = $this->jar->serialize();
+ $data = json_decode($serialized, true);
+ $this->assertEquals(1, count($data));
+
+ $a = new ArrayCookieJar();
+ $a->unserialize($serialized);
+ $this->assertEquals(1, count($a));
+ }
+
+ public function testRemovesSelectively()
+ {
+ $cookies = $this->getTestCookies();
+ foreach ($this->getTestCookies() as $cookie) {
+ $this->jar->add($cookie);
+ }
+
+ // Remove foo.com cookies
+ $this->jar->remove('foo.com');
+ $this->assertEquals(2, count($this->jar));
+ // Try again, removing no further cookies
+ $this->jar->remove('foo.com');
+ $this->assertEquals(2, count($this->jar));
+
+ // Remove bar.com cookies with path of /boo
+ $this->jar->remove('bar.com', '/boo');
+ $this->assertEquals(1, count($this->jar));
+
+ // Remove cookie by name
+ $this->jar->remove(null, null, 'test');
+ $this->assertEquals(0, count($this->jar));
+ }
+
+ public function testDoesNotAddIncompleteCookies()
+ {
+ $this->assertEquals(false, $this->jar->add(new Cookie()));
+ $this->assertFalse($this->jar->add(new Cookie(array(
+ 'name' => 'foo'
+ ))));
+ $this->assertFalse($this->jar->add(new Cookie(array(
+ 'name' => false
+ ))));
+ $this->assertFalse($this->jar->add(new Cookie(array(
+ 'name' => true
+ ))));
+ $this->assertFalse($this->jar->add(new Cookie(array(
+ 'name' => 'foo',
+ 'domain' => 'foo.com'
+ ))));
+ }
+
+ public function testDoesAddValidCookies()
+ {
+ $this->assertTrue($this->jar->add(new Cookie(array(
+ 'name' => 'foo',
+ 'domain' => 'foo.com',
+ 'value' => 0
+ ))));
+ $this->assertTrue($this->jar->add(new Cookie(array(
+ 'name' => 'foo',
+ 'domain' => 'foo.com',
+ 'value' => 0.0
+ ))));
+ $this->assertTrue($this->jar->add(new Cookie(array(
+ 'name' => 'foo',
+ 'domain' => 'foo.com',
+ 'value' => '0'
+ ))));
+ }
+
+ public function testOverwritesCookiesThatAreOlderOrDiscardable()
+ {
+ $t = time() + 1000;
+ $data = array(
+ 'name' => 'foo',
+ 'value' => 'bar',
+ 'domain' => '.example.com',
+ 'path' => '/',
+ 'max_age' => '86400',
+ 'port' => array(80, 8080),
+ 'version' => '1',
+ 'secure' => true,
+ 'discard' => true,
+ 'expires' => $t
+ );
+
+ // Make sure that the discard cookie is overridden with the non-discard
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+
+ unset($data['discard']);
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+ $this->assertEquals(1, count($this->jar));
+
+ $c = $this->jar->all();
+ $this->assertEquals(false, $c[0]->getDiscard());
+
+ // Make sure it doesn't duplicate the cookie
+ $this->jar->add(new Cookie($data));
+ $this->assertEquals(1, count($this->jar));
+
+ // Make sure the more future-ful expiration date supersede the other
+ $data['expires'] = time() + 2000;
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+ $this->assertEquals(1, count($this->jar));
+ $c = $this->jar->all();
+ $this->assertNotEquals($t, $c[0]->getExpires());
+ }
+
+ public function testOverwritesCookiesThatHaveChanged()
+ {
+ $t = time() + 1000;
+ $data = array(
+ 'name' => 'foo',
+ 'value' => 'bar',
+ 'domain' => '.example.com',
+ 'path' => '/',
+ 'max_age' => '86400',
+ 'port' => array(80, 8080),
+ 'version' => '1',
+ 'secure' => true,
+ 'discard' => true,
+ 'expires' => $t
+ );
+
+ // Make sure that the discard cookie is overridden with the non-discard
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+
+ $data['value'] = 'boo';
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+ $this->assertEquals(1, count($this->jar));
+
+ // Changing the value plus a parameter also must overwrite the existing one
+ $data['value'] = 'zoo';
+ $data['secure'] = false;
+ $this->assertTrue($this->jar->add(new Cookie($data)));
+ $this->assertEquals(1, count($this->jar));
+
+ $c = $this->jar->all();
+ $this->assertEquals('zoo', $c[0]->getValue());
+ }
+
+ public function testAddsCookiesFromResponseWithNoRequest()
+ {
+ $response = new Response(200, array(
+ 'Set-Cookie' => array(
+ "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
+ "FPCK3=AgBNbvoQAGpGEABZLRAAbFsQAF1tEABkDhAAeO0=; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
+ "CH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1",
+ "CH=AgBNbvoQAAEcEAApuhAAMJcQADQvEAAvGxAALe0QAD6uEAATwhAAC1AQAC8t; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1"
+ )
+ ));
+
+ $this->jar->addCookiesFromResponse($response);
+ $this->assertEquals(3, count($this->jar));
+ $this->assertEquals(1, count($this->jar->all(null, null, 'fpc')));
+ $this->assertEquals(1, count($this->jar->all(null, null, 'FPCK3')));
+ $this->assertEquals(1, count($this->jar->all(null, null, 'CH')));
+ }
+
+ public function testAddsCookiesFromResponseWithRequest()
+ {
+ $response = new Response(200, array(
+ 'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
+ ));
+ $request = new Request('GET', 'http://www.example.com');
+ $this->jar->addCookiesFromResponse($response, $request);
+ $this->assertEquals(1, count($this->jar));
+ }
+
+ public function getMatchingCookiesDataProvider()
+ {
+ return array(
+ array('https://example.com', array(0)),
+ array('http://example.com', array()),
+ array('https://example.com:8912', array()),
+ array('https://foo.example.com', array(0)),
+ array('http://foo.example.com/test/acme/', array(4))
+ );
+ }
+
+ /**
+ * @dataProvider getMatchingCookiesDataProvider
+ */
+ public function testReturnsCookiesMatchingRequests($url, $cookies)
+ {
+ $bag = array(
+ new Cookie(array(
+ 'name' => 'foo',
+ 'value' => 'bar',
+ 'domain' => 'example.com',
+ 'path' => '/',
+ 'max_age' => '86400',
+ 'port' => array(443, 8080),
+ 'version' => '1',
+ 'secure' => true
+ )),
+ new Cookie(array(
+ 'name' => 'baz',
+ 'value' => 'foobar',
+ 'domain' => 'example.com',
+ 'path' => '/',
+ 'max_age' => '86400',
+ 'port' => array(80, 8080),
+ 'version' => '1',
+ 'secure' => true
+ )),
+ new Cookie(array(
+ 'name' => 'test',
+ 'value' => '123',
+ 'domain' => 'www.foobar.com',
+ 'path' => '/path/',
+ 'discard' => true
+ )),
+ new Cookie(array(
+ 'name' => 'muppet',
+ 'value' => 'cookie_monster',
+ 'domain' => '.y.example.com',
+ 'path' => '/acme/',
+ 'comment' => 'Comment goes here...',
+ 'expires' => time() + 86400
+ )),
+ new Cookie(array(
+ 'name' => 'googoo',
+ 'value' => 'gaga',
+ 'domain' => '.example.com',
+ 'path' => '/test/acme/',
+ 'max_age' => 1500,
+ 'version' => 2
+ ))
+ );
+
+ foreach ($bag as $cookie) {
+ $this->jar->add($cookie);
+ }
+
+ $request = new Request('GET', $url);
+ $results = $this->jar->getMatchingCookies($request);
+ $this->assertEquals(count($cookies), count($results));
+ foreach ($cookies as $i) {
+ $this->assertContains($bag[$i], $results);
+ }
+ }
+
+ /**
+ * @expectedException \Guzzle\Plugin\Cookie\Exception\InvalidCookieException
+ * @expectedExceptionMessage The cookie name must not contain invalid characters: abc:@123
+ */
+ public function testThrowsExceptionWithStrictMode()
+ {
+ $a = new ArrayCookieJar();
+ $a->setStrictMode(true);
+ $a->add(new Cookie(array(
+ 'name' => 'abc:@123',
+ 'value' => 'foo',
+ 'domain' => 'bar'
+ )));
+ }
+
+ public function testRemoveExistingCookieIfEmpty()
+ {
+ // Add a cookie that should not be affected
+ $a = new Cookie(array(
+ 'name' => 'foo',
+ 'value' => 'nope',
+ 'domain' => 'foo.com',
+ 'path' => '/abc'
+ ));
+ $this->jar->add($a);
+
+ $data = array(
+ 'name' => 'foo',
+ 'value' => 'bar',
+ 'domain' => 'foo.com',
+ 'path' => '/'
+ );
+
+ $b = new Cookie($data);
+ $this->assertTrue($this->jar->add($b));
+ $this->assertEquals(2, count($this->jar));
+
+ // Try to re-set the same cookie with no value: assert that cookie is not added
+ $data['value'] = null;
+ $this->assertFalse($this->jar->add(new Cookie($data)));
+ // assert that original cookie has been deleted
+ $cookies = $this->jar->all('foo.com');
+ $this->assertTrue(in_array($a, $cookies, true));
+ $this->assertFalse(in_array($b, $cookies, true));
+ $this->assertEquals(1, count($this->jar));
+ }
+}
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/FileCookieJarTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/FileCookieJarTest.php
new file mode 100644
index 0000000..ac9471f
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/FileCookieJarTest.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Guzzle\Tests\Plugin\Cookie\CookieJar;
+
+use Guzzle\Plugin\Cookie\Cookie;
+use Guzzle\Plugin\Cookie\CookieJar\FileCookieJar;
+
+/**
+ * @covers Guzzle\Plugin\Cookie\CookieJar\FileCookieJar
+ */
+class FileCookieJarTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ private $file;
+
+ public function setUp()
+ {
+ $this->file = tempnam('/tmp', 'file-cookies');
+ }
+
+ public function testLoadsFromFileFile()
+ {
+ $jar = new FileCookieJar($this->file);
+ $this->assertEquals(array(), $jar->all());
+ unlink($this->file);
+ }
+
+ public function testPersistsToFileFile()
+ {
+ $jar = new FileCookieJar($this->file);
+ $jar->add(new Cookie(array(
+ 'name' => 'foo',
+ 'value' => 'bar',
+ 'domain' => 'foo.com',
+ 'expires' => time() + 1000
+ )));
+ $jar->add(new Cookie(array(
+ 'name' => 'baz',
+ 'value' => 'bar',
+ 'domain' => 'foo.com',
+ 'expires' => time() + 1000
+ )));
+ $jar->add(new Cookie(array(
+ 'name' => 'boo',
+ 'value' => 'bar',
+ 'domain' => 'foo.com',
+ )));
+
+ $this->assertEquals(3, count($jar));
+ unset($jar);
+
+ // Make sure it wrote to the file
+ $contents = file_get_contents($this->file);
+ $this->assertNotEmpty($contents);
+
+ // Load the cookieJar from the file
+ $jar = new FileCookieJar($this->file);
+
+ // Weeds out temporary and session cookies
+ $this->assertEquals(2, count($jar));
+ unset($jar);
+ unlink($this->file);
+ }
+}
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookiePluginTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookiePluginTest.php
new file mode 100644
index 0000000..f8c175c
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookiePluginTest.php
@@ -0,0 +1,134 @@
+<?php
+
+namespace Guzzle\Tests\Plugin\Cookie;
+
+use Guzzle\Common\Event;
+use Guzzle\Plugin\Cookie\Cookie;
+use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
+use Guzzle\Http\Client;
+use Guzzle\Http\Message\Response;
+use Guzzle\Plugin\Cookie\CookiePlugin;
+
+/**
+ * @group server
+ * @covers Guzzle\Plugin\Cookie\CookiePlugin
+ */
+class CookiePluginTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ public function testExtractsAndStoresCookies()
+ {
+ $response = new Response(200);
+ $mock = $this->getMockBuilder('Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar')
+ ->setMethods(array('addCookiesFromResponse'))
+ ->getMock();
+
+ $mock->expects($this->exactly(1))
+ ->method('addCookiesFromResponse')
+ ->with($response);
+
+ $plugin = new CookiePlugin($mock);
+ $plugin->onRequestSent(new Event(array(
+ 'response' => $response
+ )));
+ }
+
+ public function testAddsCookiesToRequests()
+ {
+ $cookie = new Cookie(array(
+ 'name' => 'foo',
+ 'value' => 'bar'
+ ));
+
+ $mock = $this->getMockBuilder('Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar')
+ ->setMethods(array('getMatchingCookies'))
+ ->getMock();
+
+ $mock->expects($this->once())
+ ->method('getMatchingCookies')
+ ->will($this->returnValue(array($cookie)));
+
+ $plugin = new CookiePlugin($mock);
+
+ $client = new Client();
+ $client->getEventDispatcher()->addSubscriber($plugin);
+
+ $request = $client->get('http://www.example.com');
+ $plugin->onRequestBeforeSend(new Event(array(
+ 'request' => $request
+ )));
+
+ $this->assertEquals('bar', $request->getCookie('foo'));
+ }
+
+ public function testCookiesAreExtractedFromRedirectResponses()
+ {
+ $plugin = new CookiePlugin(new ArrayCookieJar());
+ $this->getServer()->flush();
+ $this->getServer()->enqueue(array(
+ "HTTP/1.1 302 Moved Temporarily\r\n" .
+ "Set-Cookie: test=583551; expires=Wednesday, 23-Mar-2050 19:49:45 GMT; path=/\r\n" .
+ "Location: /redirect\r\n\r\n",
+ "HTTP/1.1 200 OK\r\n" .
+ "Content-Length: 0\r\n\r\n",
+ "HTTP/1.1 200 OK\r\n" .
+ "Content-Length: 0\r\n\r\n"
+ ));
+
+ $client = new Client($this->getServer()->getUrl());
+ $client->getEventDispatcher()->addSubscriber($plugin);
+
+ $client->get()->send();
+ $request = $client->get();
+ $request->send();
+ $this->assertEquals('test=583551', $request->getHeader('Cookie'));
+
+ $requests = $this->getServer()->getReceivedRequests(true);
+ // Confirm subsequent requests have the cookie.
+ $this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
+ // Confirm the redirected request has the cookie.
+ $this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
+ }
+
+ public function testCookiesAreNotAddedWhenParamIsSet()
+ {
+ $jar = new ArrayCookieJar();
+ $plugin = new CookiePlugin($jar);
+
+ $jar->add(new Cookie(array(
+ 'domain' => 'example.com',
+ 'path' => '/',
+ 'name' => 'test',
+ 'value' => 'hi',
+ 'expires' => time() + 3600
+ )));
+
+ $client = new Client('http://example.com');
+ $client->getEventDispatcher()->addSubscriber($plugin);
+
+ // Ensure that it is normally added
+ $request = $client->get();
+ $request->setResponse(new Response(200), true);
+ $request->send();
+ $this->assertEquals('hi', $request->getCookie('test'));
+
+ // Now ensure that it is not added
+ $request = $client->get();
+ $request->getParams()->set('cookies.disable', true);
+ $request->setResponse(new Response(200), true);
+ $request->send();
+ $this->assertNull($request->getCookie('test'));
+ }
+
+ public function testProvidesCookieJar()
+ {
+ $jar = new ArrayCookieJar();
+ $plugin = new CookiePlugin($jar);
+ $this->assertSame($jar, $plugin->getCookieJar());
+ }
+
+ public function testEscapesCookieDomains()
+ {
+ $cookie = new Cookie(array('domain' => '/foo/^$[A-Z]+/'));
+ $this->assertFalse($cookie->matchesDomain('foo'));
+ }
+}
diff --git a/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php
new file mode 100644
index 0000000..9fb0b43
--- /dev/null
+++ b/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php
@@ -0,0 +1,223 @@
+<?php
+
+namespace Guzzle\Tests\Plugin\Cookie;
+
+use Guzzle\Plugin\Cookie\Cookie;
+
+/**
+ * @covers Guzzle\Plugin\Cookie\Cookie
+ */
+class CookieTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ public function testInitializesDefaultValues()
+ {
+ $cookie = new Cookie();
+ $this->assertEquals('/', $cookie->getPath());
+ $this->assertEquals(array(), $cookie->getPorts());
+ }
+
+ public function testConvertsDateTimeMaxAgeToUnixTimestamp()
+ {
+ $cookie = new Cookie(array(
+ 'expires' => 'November 20, 1984'
+ ));
+ $this->assertTrue(is_numeric($cookie->getExpires()));
+ }
+
+ public function testAddsExpiresBasedOnMaxAge()
+ {
+ $t = time();
+ $cookie = new Cookie(array(
+ 'max_age' => 100
+ ));
+ $this->assertEquals($t + 100, $cookie->getExpires());
+ }
+
+ public function testHoldsValues()
+ {
+ $t = time();
+ $data = array(
+ 'name' => 'foo',
+ 'value' => 'baz',
+ 'path' => '/bar',
+ 'domain' => 'baz.com',
+ 'expires' => $t,
+ 'max_age' => 100,
+ 'comment' => 'Hi',
+ 'comment_url' => 'foo.com',
+ 'port' => array(1, 2),
+ 'version' => 2,
+ 'secure' => true,
+ 'discard' => true,
+ 'http_only' => true,
+ 'data' => array(
+ 'foo' => 'baz',
+ 'bar' => 'bam'
+ )
+ );
+
+ $cookie = new Cookie($data);
+ $this->assertEquals($data, $cookie->toArray());
+
+ $this->assertEquals('foo', $cookie->getName());
+ $this->assertEquals('baz', $cookie->getValue());
+ $this->assertEquals('baz.com', $cookie->getDomain());
+ $this->assertEquals('/bar', $cookie->getPath());
+ $this->assertEquals($t, $cookie->getExpires());
+ $this->assertEquals(100, $cookie->getMaxAge());
+ $this->assertEquals('Hi', $cookie->getComment());
+ $this->assertEquals('foo.com', $cookie->getCommentUrl());
+ $this->assertEquals(array(1, 2), $cookie->getPorts());
+ $this->assertEquals(2, $cookie->getVersion());
+ $this->assertTrue($cookie->getSecure());
+ $this->assertTrue($cookie->getDiscard());
+ $this->assertTrue($cookie->getHttpOnly());
+ $this->assertEquals('baz', $cookie->getAttribute('foo'));
+ $this->assertEquals('bam', $cookie->getAttribute('bar'));
+ $this->assertEquals(array(
+ 'foo' => 'baz',
+ 'bar' => 'bam'
+ ), $cookie->getAttributes());
+
+ $cookie->setName('a')
+ ->setValue('b')
+ ->setPath('c')
+ ->setDomain('bar.com')
+ ->setExpires(10)
+ ->setMaxAge(200)
+ ->setComment('e')
+ ->setCommentUrl('f')
+ ->setPorts(array(80))
+ ->setVersion(3)
+ ->setSecure(false)
+ ->setHttpOnly(false)
+ ->setDiscard(false)
+ ->setAttribute('snoop', 'dog');
+
+ $this->assertEquals('a', $cookie->getName());
+ $this->assertEquals('b', $cookie->getValue());
+ $this->assertEquals('c', $cookie->getPath());
+ $this->assertEquals('bar.com', $cookie->getDomain());
+ $this->assertEquals(10, $cookie->getExpires());
+ $this->assertEquals(200, $cookie->getMaxAge());
+ $this->assertEquals('e', $cookie->getComment());
+ $this->assertEquals('f', $cookie->getCommentUrl());
+ $this->assertEquals(array(80), $cookie->getPorts());
+ $this->assertEquals(3, $cookie->getVersion());
+ $this->assertFalse($cookie->getSecure());
+ $this->assertFalse($cookie->getDiscard());
+ $this->assertFalse($cookie->getHttpOnly());
+ $this->assertEquals('dog', $cookie->getAttribute('snoop'));
+ }
+
+ public function testDeterminesIfExpired()
+ {
+ $c = new Cookie();
+ $c->setExpires(10);
+ $this->assertTrue($c->isExpired());
+ $c->setExpires(time() + 10000);
+ $this->assertFalse($c->isExpired());
+ }
+
+ public function testMatchesPorts()
+ {
+ $cookie = new Cookie();
+ // Always matches when nothing is set
+ $this->assertTrue($cookie->matchesPort(2));
+
+ $cookie->setPorts(array(1, 2));
+ $this->assertTrue($cookie->matchesPort(2));
+ $this->assertFalse($cookie->matchesPort(100));
+ }
+
+ public function testMatchesDomain()
+ {
+ $cookie = new Cookie();
+ $this->assertTrue($cookie->matchesDomain('baz.com'));
+
+ $cookie->setDomain('baz.com');
+ $this->assertTrue($cookie->matchesDomain('baz.com'));
+ $this->assertFalse($cookie->matchesDomain('bar.com'));
+
+ $cookie->setDomain('.baz.com');
+ $this->assertTrue($cookie->matchesDomain('.baz.com'));
+ $this->assertTrue($cookie->matchesDomain('foo.baz.com'));
+ $this->assertFalse($cookie->matchesDomain('baz.bar.com'));
+ $this->assertTrue($cookie->matchesDomain('baz.com'));
+
+ $cookie->setDomain('.127.0.0.1');
+ $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
+
+ $cookie->setDomain('127.0.0.1');
+ $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
+
+ $cookie->setDomain('.com.');
+ $this->assertFalse($cookie->matchesDomain('baz.com'));
+
+ $cookie->setDomain('.local');
+ $this->assertTrue($cookie->matchesDomain('example.local'));
+ }
+
+ public function testMatchesPath()
+ {
+ $cookie = new Cookie();
+ $this->assertTrue($cookie->matchesPath('/foo'));
+
+ $cookie->setPath('/foo');
+
+ // o The cookie-path and the request-path are identical.
+ $this->assertTrue($cookie->matchesPath('/foo'));
+ $this->assertFalse($cookie->matchesPath('/bar'));
+
+ // o The cookie-path is a prefix of the request-path, and the first
+ // character of the request-path that is not included in the cookie-
+ // path is a %x2F ("/") character.
+ $this->assertTrue($cookie->matchesPath('/foo/bar'));
+ $this->assertFalse($cookie->matchesPath('/fooBar'));
+
+ // o The cookie-path is a prefix of the request-path, and the last
+ // character of the cookie-path is %x2F ("/").
+ $cookie->setPath('/foo/');
+ $this->assertTrue($cookie->matchesPath('/foo/bar'));
+ $this->assertFalse($cookie->matchesPath('/fooBaz'));
+ $this->assertFalse($cookie->matchesPath('/foo'));
+
+ }
+
+ public function cookieValidateProvider()
+ {
+ return array(
+ array('foo', 'baz', 'bar', true),
+ array('0', '0', '0', true),
+ array('', 'baz', 'bar', 'The cookie name must not be empty'),
+ array('foo', '', 'bar', 'The cookie value must not be empty'),
+ array('foo', 'baz', '', 'The cookie domain must not be empty'),
+ array('foo\\', 'baz', '0', 'The cookie name must not contain invalid characters: foo\\'),
+ );
+ }
+
+ /**
+ * @dataProvider cookieValidateProvider
+ */
+ public function testValidatesCookies($name, $value, $domain, $result)
+ {
+ $cookie = new Cookie(array(
+ 'name' => $name,
+ 'value' => $value,
+ 'domain' => $domain
+ ));
+ $this->assertSame($result, $cookie->validate());
+ }
+
+ public function testCreatesInvalidCharacterString()
+ {
+ $m = new \ReflectionMethod('Guzzle\Plugin\Cookie\Cookie', 'getInvalidCharacters');
+ $m->setAccessible(true);
+ $p = new \ReflectionProperty('Guzzle\Plugin\Cookie\Cookie', 'invalidCharString');
+ $p->setAccessible(true);
+ $p->setValue('');
+ // Expects a string containing 51 invalid characters
+ $this->assertEquals(51, strlen($m->invoke($m)));
+ $this->assertContains('@', $m->invoke($m));
+ }
+}