1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224:
<?php
namespace Guzzle\Tests\Plugin\Cookie;
use 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();
$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');
$this->assertTrue($cookie->matchesPath('/foo'));
$this->assertFalse($cookie->matchesPath('/bar'));
$this->assertTrue($cookie->matchesPath('/foo/bar'));
$this->assertFalse($cookie->matchesPath('/fooBar'));
$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\\'),
);
}
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('');
$this->assertEquals(51, strlen($m->invoke($m)));
$this->assertContains('@', $m->invoke($m));
}
}