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 225
|
<?php
// $Id: cookies_test.php,v 1.3 2005/12/05 04:47:03 lastcraft Exp $
class TestOfCookie extends UnitTestCase {
function testCookieDefaults() {
$cookie = new SimpleCookie("name");
$this->assertFalse($cookie->getValue());
$this->assertEqual($cookie->getPath(), "/");
$this->assertIdentical($cookie->getHost(), false);
$this->assertFalse($cookie->getExpiry());
$this->assertFalse($cookie->isSecure());
}
function testCookieAccessors() {
$cookie = new SimpleCookie(
"name",
"value",
"/path",
"Mon, 18 Nov 2002 15:50:29 GMT",
true);
$this->assertEqual($cookie->getName(), "name");
$this->assertEqual($cookie->getValue(), "value");
$this->assertEqual($cookie->getPath(), "/path/");
$this->assertEqual($cookie->getExpiry(), "Mon, 18 Nov 2002 15:50:29 GMT");
$this->assertTrue($cookie->isSecure());
}
function testFullHostname() {
$cookie = new SimpleCookie("name");
$this->assertTrue($cookie->setHost("host.name.here"));
$this->assertEqual($cookie->getHost(), "host.name.here");
$this->assertTrue($cookie->setHost("host.com"));
$this->assertEqual($cookie->getHost(), "host.com");
}
function testHostTruncation() {
$cookie = new SimpleCookie("name");
$cookie->setHost("this.host.name.here");
$this->assertEqual($cookie->getHost(), "host.name.here");
$cookie->setHost("this.host.com");
$this->assertEqual($cookie->getHost(), "host.com");
$this->assertTrue($cookie->setHost("dashes.in-host.com"));
$this->assertEqual($cookie->getHost(), "in-host.com");
}
function testBadHosts() {
$cookie = new SimpleCookie("name");
$this->assertFalse($cookie->setHost("gibberish"));
$this->assertFalse($cookie->setHost("host.here"));
$this->assertFalse($cookie->setHost("host..com"));
$this->assertFalse($cookie->setHost("..."));
$this->assertFalse($cookie->setHost("host.com."));
}
function testHostValidity() {
$cookie = new SimpleCookie("name");
$cookie->setHost("this.host.name.here");
$this->assertTrue($cookie->isValidHost("host.name.here"));
$this->assertTrue($cookie->isValidHost("that.host.name.here"));
$this->assertFalse($cookie->isValidHost("bad.host"));
$this->assertFalse($cookie->isValidHost("nearly.name.here"));
}
function testPathValidity() {
$cookie = new SimpleCookie("name", "value", "/path");
$this->assertFalse($cookie->isValidPath("/"));
$this->assertTrue($cookie->isValidPath("/path/"));
$this->assertTrue($cookie->isValidPath("/path/more"));
}
function testSessionExpiring() {
$cookie = new SimpleCookie("name", "value", "/path");
$this->assertTrue($cookie->isExpired(0));
}
function testTimestampExpiry() {
$cookie = new SimpleCookie("name", "value", "/path", 456);
$this->assertFalse($cookie->isExpired(0));
$this->assertTrue($cookie->isExpired(457));
$this->assertFalse($cookie->isExpired(455));
}
function testDateExpiry() {
$cookie = new SimpleCookie(
"name",
"value",
"/path",
"Mon, 18 Nov 2002 15:50:29 GMT");
$this->assertTrue($cookie->isExpired("Mon, 18 Nov 2002 15:50:30 GMT"));
$this->assertFalse($cookie->isExpired("Mon, 18 Nov 2002 15:50:28 GMT"));
}
function testAging() {
$cookie = new SimpleCookie("name", "value", "/path", 200);
$cookie->agePrematurely(199);
$this->assertFalse($cookie->isExpired(0));
$cookie->agePrematurely(2);
$this->assertTrue($cookie->isExpired(0));
}
}
class TestOfCookieJar extends UnitTestCase {
function testAddCookie() {
$jar = new SimpleCookieJar();
$jar->setCookie("a", "A");
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
}
function testHostFilter() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', 'my-host.com');
$jar->setCookie('b', 'B', 'another-host.com');
$jar->setCookie('c', 'C');
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('my-host.com')),
array('a=A', 'c=C'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('another-host.com')),
array('b=B', 'c=C'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('www.another-host.com')),
array('b=B', 'c=C'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('new-host.org')),
array('c=C'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('/')),
array('a=A', 'b=B', 'c=C'));
}
function testPathFilter() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', false, '/path/');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/elsewhere')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/')), array('a=A'));
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path')), array('a=A'));
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/pa')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/here')), array('a=A'));
}
function testPathFilterDeeply() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', false, '/path/more_path/');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/pa')), array());
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/more_path/')), array('a=A'));
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/more_path/and_more')), array('a=A'));
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/not_here/')), array());
}
function testMultipleCookieWithDifferentPathsButSameName() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'abc', false, '/');
$jar->setCookie('a', '123', false, '/path/here/');
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('/')),
array('a=abc'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('my-host.com/')),
array('a=abc'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('my-host.com/path/')),
array('a=abc'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('my-host.com/path/here')),
array('a=abc', 'a=123'));
$this->assertEqual(
$jar->selectAsPairs(new SimpleUrl('my-host.com/path/here/there')),
array('a=abc', 'a=123'));
}
function testOverwrite() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'abc', false, '/');
$jar->setCookie('a', 'cde', false, '/');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=cde'));
}
function testClearSessionCookies() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', false, '/');
$jar->restartSession();
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
}
function testExpiryFilterByDate() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', false, '/', 'Wed, 25-Dec-02 04:24:20 GMT');
$jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
$jar->restartSession("Wed, 25-Dec-02 04:24:21 GMT");
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
}
function testExpiryFilterByAgeing() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A', false, '/', 'Wed, 25-Dec-02 04:24:20 GMT');
$jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
$jar->agePrematurely(2);
$jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
}
function testCookieClearing() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'abc', false, '/');
$jar->setCookie('a', '', false, '/');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a='));
}
function testCookieClearByLoweringDate() {
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'abc', false, '/', 'Wed, 25-Dec-02 04:24:21 GMT');
$jar->setCookie('a', 'def', false, '/', 'Wed, 25-Dec-02 04:24:19 GMT');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=def'));
$jar->restartSession('Wed, 25-Dec-02 04:24:20 GMT');
$this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
}
}
?>
|