add max-age to http\Cookie
[m6w6/ext-http] / phpunit / CookieTest.php
1 <?php
2
3 class CookieTest extends PHPUnit_Framework_TestCase {
4 function testEmpty() {
5 $c = new http\Cookie;
6 $o = clone $c;
7 $a = array(
8 "cookies" => array(),
9 "extras" => array(),
10 "flags" => 0,
11 "expires" => -1,
12 "path" => "",
13 "domain" => "",
14 "max-age" => -1,
15 );
16 $this->assertEquals($a, $c->toArray());
17 $this->assertEquals($a, $o->toArray());
18 }
19
20 function testExpiresAsDate() {
21 $d = new DateTime;
22 $c = new http\Cookie(array("expires" => $d->format(DateTime::RFC1123)));
23 $this->assertEquals($d->format("U"), $c->getExpires());
24 }
25
26 function testNumeric() {
27 $c = new http\Cookie("1=%20; 2=%22; 3=%5D", 0, array(2));
28 $this->assertEquals("1=%20; 3=%5D; 2=%22; ", (string) $c);
29 }
30
31 function testRaw() {
32 $c = new http\Cookie("1=%20; 2=%22; e3=%5D", http\Cookie::PARSE_RAW, array(2));
33 $this->assertEquals("1=%2520; e3=%255D; 2=%2522; ", (string) $c);
34 }
35
36 function testSimple() {
37 $orig = new http\Cookie("key=value");
38 $copy = clone $orig;
39 $same = new http\Cookie($copy);
40 $even = new http\Cookie($same->toArray());
41 foreach (array($orig, $copy) as $c) {
42 $this->assertEquals("value", $c->getCookie("key"));
43 $this->assertEquals(-1, $c->getExpires());
44 $this->assertEquals(-1, $c->getMaxAge());
45 $this->assertEquals(0, $c->getFlags());
46 $this->assertEquals(null, $c->getPath());
47 $this->assertEquals(null, $c->getDomain());
48 $this->assertEquals(array(), $c->getExtras());
49 $this->assertEquals(array("key" => "value"), $c->getCookies());
50 $this->assertEquals("key=value; ", $c->toString());
51 $this->assertEquals(
52 array (
53 "cookies" =>
54 array (
55 "key" => "value",
56 ),
57 "extras" =>
58 array (
59 ),
60 "flags" => 0,
61 "expires" => -1,
62 "path" => "",
63 "domain" => "",
64 "max-age" => -1,
65 ),
66 $c->toArray()
67 );
68 }
69 }
70
71 function testExpires() {
72 $c = new http\Cookie("this=expires; expires=Tue, 24 Jan 2012 10:35:32 +0100");
73 $this->assertEquals("expires", $c->getCookie("this"));
74 $this->assertEquals(1327397732, $c->getExpires());
75 $o = clone $c;
76 $t = time();
77 $o->setExpires();
78 $this->assertEquals(-1, $o->getExpires());
79 $this->assertNotEquals(-1, $c->getExpires());
80 $o->setExpires($t);
81 $this->assertEquals($t, $o->getExpires());
82 $this->assertNotEquals($t, $c->getExpires());
83 $this->assertEquals(
84 sprintf(
85 "this=expires; expires=%s; ",
86 date_create("@$t")->format("D, d M Y H:i:s \\G\\M\\T")
87 ),
88 $o->toString()
89 );
90 }
91
92 function testMaxAge() {
93 $c = new http\Cookie("this=max-age; max-age=12345");
94 $this->assertEquals("max-age", $c->getCookie("this"));
95 $this->assertEquals(12345, $c->getMaxAge());
96 $o = clone $c;
97 $t = 54321;
98 $o->setMaxAge();
99 $this->assertEquals(-1, $o->getMaxAge());
100 $this->assertNotEquals(-1, $c->getMaxAge());
101 $o->setMaxAge($t);
102 $this->assertEquals($t, $o->getMaxAge());
103 $this->assertNotEquals($t, $c->getMaxAge());
104 $this->assertEquals(
105 "this=max-age; max-age=$t; ",
106 $o->toString()
107 );
108 }
109
110 function testPath() {
111 $c = new http\Cookie("this=has a path; path=/down; ");
112 $this->assertEquals("has a path", $c->getCookie("this"));
113 $this->assertEquals("this=has%20a%20path; path=/down; ", (string)$c);
114 $this->assertEquals("/down", $c->getPath());
115 $o = clone $c;
116 $p = "/up";
117 $o->setPath();
118 $this->assertEquals(null, $o->getPath());
119 $this->assertNotEquals(null, $c->getPath());
120 $o->setPath($p);
121 $this->assertEquals($p, $o->getPath());
122 $this->assertNotEquals($p, $c->getPath());
123 $this->assertEquals("this=has%20a%20path; path=$p; ", $o->toString());
124 }
125
126 function testDomain() {
127 $c = new http\Cookie("this=has a domain; domain=.example.com; ");
128 $this->assertEquals("has a domain", $c->getCookie("this"));
129 $this->assertEquals("this=has%20a%20domain; domain=.example.com; ", (string)$c);
130 $this->assertEquals(".example.com", $c->getDomain());
131 $o = clone $c;
132 $d = "sub.example.com";
133 $o->setDomain();
134 $this->assertEquals(null, $o->getDomain());
135 $this->assertNotEquals(null, $c->getDomain());
136 $o->setDomain($d);
137 $this->assertEquals($d, $o->getDomain());
138 $this->assertNotEquals($d, $c->getDomain());
139 $this->assertEquals("this=has%20a%20domain; domain=$d; ", $o->toString());
140 }
141
142 function testFlags() {
143 $c = new http\Cookie("icanhas=flags; secure; httpOnly");
144 $this->assertEquals(http\Cookie::SECURE, $c->getFlags() & http\Cookie::SECURE, "secure");
145 $this->assertEquals(http\Cookie::HTTPONLY, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
146 $c->setFlags($c->getFlags() ^ http\Cookie::SECURE);
147 $this->assertEquals(0, $c->getFlags() & http\Cookie::SECURE, "secure");
148 $this->assertEquals(http\Cookie::HTTPONLY, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
149 $c->setFlags($c->getFlags() ^ http\Cookie::HTTPONLY);
150 $this->assertEquals(0, $c->getFlags() & http\Cookie::SECURE, "secure");
151 $this->assertEquals(0, $c->getFlags() & http\Cookie::HTTPONLY, "httpOnly");
152 $this->assertEquals("icanhas=flags; ", $c->toString());
153 $c->setFlags(http\Cookie::SECURE|http\Cookie::HTTPONLY);
154 $this->assertEquals("icanhas=flags; secure; httpOnly; ", $c->toString());
155 }
156
157 function testExtras() {
158 $c = new http\Cookie("c1=v1; e0=1; e2=2; c2=v2", 0, array("e0", "e1", "e2"));
159 $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getCookies());
160 $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getExtras());
161 $c->addExtra("e1", 1);
162 $c->setExtra("e0");
163 $c->setExtra("e3", 123);
164 $this->assertEquals(123, $c->getExtra("e3"));
165 $c->setExtra("e3");
166 $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getExtras());
167 $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; ", $c->toString());
168 $c->addExtras(array("e3"=>3, "e4"=>4));
169 $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getExtras());
170 $this->assertEquals("c1=v1; c2=v2; e2=2; e1=1; e3=3; e4=4; ", $c->toString());
171 $c->setExtras(array("e"=>"x"));
172 $this->assertEquals(array("e"=>"x"), $c->getExtras());
173 $this->assertEquals("c1=v1; c2=v2; e=x; ", $c->toString());
174 $c->setExtras();
175 $this->assertEquals(array(), $c->getExtras());
176 $this->assertEquals("c1=v1; c2=v2; ", $c->toString());
177 }
178
179 function testCookies() {
180 $c = new http\Cookie("e0=1; c1=v1; e2=2; c2=v2", 0, array("c0", "c1", "c2"));
181 $this->assertEquals(array("c1"=>"v1", "c2"=>"v2"), $c->getExtras());
182 $this->assertEquals(array("e0"=>"1", "e2"=>"2"), $c->getCookies());
183 $c->addCookie("e1", 1);
184 $c->setCookie("e0");
185 $c->setCookie("e3", 123);
186 $this->assertEquals(123, $c->getCookie("e3"));
187 $c->setCookie("e3");
188 $this->assertEquals(array("e2"=>"2", "e1"=>1), $c->getCookies());
189 $this->assertEquals("e2=2; e1=1; c1=v1; c2=v2; ", $c->toString());
190 $c->addCookies(array("e3"=>3, "e4"=>4));
191 $this->assertEquals(array("e2"=>"2", "e1"=>1, "e3"=>3, "e4"=>4), $c->getCookies());
192 $this->assertEquals("e2=2; e1=1; e3=3; e4=4; c1=v1; c2=v2; ", $c->toString());
193 $c->setCookies(array("e"=>"x"));
194 $this->assertEquals(array("e"=>"x"), $c->getCookies());
195 $this->assertEquals("e=x; c1=v1; c2=v2; ", $c->toString());
196 $c->setCookies();
197 $this->assertEquals(array(), $c->getCookies());
198 $this->assertEquals("c1=v1; c2=v2; ", $c->toString());
199 }
200 }