Open Journal Systems  3.3.0
guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php
1 <?php
2 
4 
6 
11 {
12  public function testInitializesDefaultValues()
13  {
14  $cookie = new Cookie();
15  $this->assertEquals('/', $cookie->getPath());
16  $this->assertEquals(array(), $cookie->getPorts());
17  }
18 
20  {
21  $cookie = new Cookie(array(
22  'expires' => 'November 20, 1984'
23  ));
24  $this->assertTrue(is_numeric($cookie->getExpires()));
25  }
26 
27  public function testAddsExpiresBasedOnMaxAge()
28  {
29  $t = time();
30  $cookie = new Cookie(array(
31  'max_age' => 100
32  ));
33  $this->assertEquals($t + 100, $cookie->getExpires());
34  }
35 
36  public function testHoldsValues()
37  {
38  $t = time();
39  $data = array(
40  'name' => 'foo',
41  'value' => 'baz',
42  'path' => '/bar',
43  'domain' => 'baz.com',
44  'expires' => $t,
45  'max_age' => 100,
46  'comment' => 'Hi',
47  'comment_url' => 'foo.com',
48  'port' => array(1, 2),
49  'version' => 2,
50  'secure' => true,
51  'discard' => true,
52  'http_only' => true,
53  'data' => array(
54  'foo' => 'baz',
55  'bar' => 'bam'
56  )
57  );
58 
59  $cookie = new Cookie($data);
60  $this->assertEquals($data, $cookie->toArray());
61 
62  $this->assertEquals('foo', $cookie->getName());
63  $this->assertEquals('baz', $cookie->getValue());
64  $this->assertEquals('baz.com', $cookie->getDomain());
65  $this->assertEquals('/bar', $cookie->getPath());
66  $this->assertEquals($t, $cookie->getExpires());
67  $this->assertEquals(100, $cookie->getMaxAge());
68  $this->assertEquals('Hi', $cookie->getComment());
69  $this->assertEquals('foo.com', $cookie->getCommentUrl());
70  $this->assertEquals(array(1, 2), $cookie->getPorts());
71  $this->assertEquals(2, $cookie->getVersion());
72  $this->assertTrue($cookie->getSecure());
73  $this->assertTrue($cookie->getDiscard());
74  $this->assertTrue($cookie->getHttpOnly());
75  $this->assertEquals('baz', $cookie->getAttribute('foo'));
76  $this->assertEquals('bam', $cookie->getAttribute('bar'));
77  $this->assertEquals(array(
78  'foo' => 'baz',
79  'bar' => 'bam'
80  ), $cookie->getAttributes());
81 
82  $cookie->setName('a')
83  ->setValue('b')
84  ->setPath('c')
85  ->setDomain('bar.com')
86  ->setExpires(10)
87  ->setMaxAge(200)
88  ->setComment('e')
89  ->setCommentUrl('f')
90  ->setPorts(array(80))
91  ->setVersion(3)
92  ->setSecure(false)
93  ->setHttpOnly(false)
94  ->setDiscard(false)
95  ->setAttribute('snoop', 'dog');
96 
97  $this->assertEquals('a', $cookie->getName());
98  $this->assertEquals('b', $cookie->getValue());
99  $this->assertEquals('c', $cookie->getPath());
100  $this->assertEquals('bar.com', $cookie->getDomain());
101  $this->assertEquals(10, $cookie->getExpires());
102  $this->assertEquals(200, $cookie->getMaxAge());
103  $this->assertEquals('e', $cookie->getComment());
104  $this->assertEquals('f', $cookie->getCommentUrl());
105  $this->assertEquals(array(80), $cookie->getPorts());
106  $this->assertEquals(3, $cookie->getVersion());
107  $this->assertFalse($cookie->getSecure());
108  $this->assertFalse($cookie->getDiscard());
109  $this->assertFalse($cookie->getHttpOnly());
110  $this->assertEquals('dog', $cookie->getAttribute('snoop'));
111  }
112 
113  public function testDeterminesIfExpired()
114  {
115  $c = new Cookie();
116  $c->setExpires(10);
117  $this->assertTrue($c->isExpired());
118  $c->setExpires(time() + 10000);
119  $this->assertFalse($c->isExpired());
120  }
121 
122  public function testMatchesPorts()
123  {
124  $cookie = new Cookie();
125  // Always matches when nothing is set
126  $this->assertTrue($cookie->matchesPort(2));
127 
128  $cookie->setPorts(array(1, 2));
129  $this->assertTrue($cookie->matchesPort(2));
130  $this->assertFalse($cookie->matchesPort(100));
131  }
132 
133  public function testMatchesDomain()
134  {
135  $cookie = new Cookie();
136  $this->assertTrue($cookie->matchesDomain('baz.com'));
137 
138  $cookie->setDomain('baz.com');
139  $this->assertTrue($cookie->matchesDomain('baz.com'));
140  $this->assertFalse($cookie->matchesDomain('bar.com'));
141 
142  $cookie->setDomain('.baz.com');
143  $this->assertTrue($cookie->matchesDomain('.baz.com'));
144  $this->assertTrue($cookie->matchesDomain('foo.baz.com'));
145  $this->assertFalse($cookie->matchesDomain('baz.bar.com'));
146  $this->assertTrue($cookie->matchesDomain('baz.com'));
147 
148  $cookie->setDomain('.127.0.0.1');
149  $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
150 
151  $cookie->setDomain('127.0.0.1');
152  $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
153 
154  $cookie->setDomain('.com.');
155  $this->assertFalse($cookie->matchesDomain('baz.com'));
156 
157  $cookie->setDomain('.local');
158  $this->assertTrue($cookie->matchesDomain('example.local'));
159  }
160 
161  public function testMatchesPath()
162  {
163  $cookie = new Cookie();
164  $this->assertTrue($cookie->matchesPath('/foo'));
165 
166  $cookie->setPath('/foo');
167 
168  // o The cookie-path and the request-path are identical.
169  $this->assertTrue($cookie->matchesPath('/foo'));
170  $this->assertFalse($cookie->matchesPath('/bar'));
171 
172  // o The cookie-path is a prefix of the request-path, and the first
173  // character of the request-path that is not included in the cookie-
174  // path is a %x2F ("/") character.
175  $this->assertTrue($cookie->matchesPath('/foo/bar'));
176  $this->assertFalse($cookie->matchesPath('/fooBar'));
177 
178  // o The cookie-path is a prefix of the request-path, and the last
179  // character of the cookie-path is %x2F ("/").
180  $cookie->setPath('/foo/');
181  $this->assertTrue($cookie->matchesPath('/foo/bar'));
182  $this->assertFalse($cookie->matchesPath('/fooBaz'));
183  $this->assertFalse($cookie->matchesPath('/foo'));
184 
185  }
186 
187  public function cookieValidateProvider()
188  {
189  return array(
190  array('foo', 'baz', 'bar', true),
191  array('0', '0', '0', true),
192  array('', 'baz', 'bar', 'The cookie name must not be empty'),
193  array('foo', '', 'bar', 'The cookie value must not be empty'),
194  array('foo', 'baz', '', 'The cookie domain must not be empty'),
195  array('foo\\', 'baz', '0', 'The cookie name must not contain invalid characters: foo\\'),
196  );
197  }
198 
202  public function testValidatesCookies($name, $value, $domain, $result)
203  {
204  $cookie = new Cookie(array(
205  'name' => $name,
206  'value' => $value,
207  'domain' => $domain
208  ));
209  $this->assertSame($result, $cookie->validate());
210  }
211 
213  {
214  $m = new \ReflectionMethod('Guzzle\Plugin\Cookie\Cookie', 'getInvalidCharacters');
215  $m->setAccessible(true);
216  $p = new \ReflectionProperty('Guzzle\Plugin\Cookie\Cookie', 'invalidCharString');
217  $p->setAccessible(true);
218  $p->setValue('');
219  // Expects a string containing 51 invalid characters
220  $this->assertEquals(51, strlen($m->invoke($m)));
221  $this->assertContains('@', $m->invoke($m));
222  }
223 }
Guzzle\Tests\Plugin\Cookie\CookieTest\testMatchesDomain
testMatchesDomain()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:133
Guzzle\Tests\Plugin\Cookie\CookieTest\testHoldsValues
testHoldsValues()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:36
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Plugin\Cookie\CookieTest\testMatchesPorts
testMatchesPorts()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:122
Guzzle\Tests\Plugin\Cookie\CookieTest\testConvertsDateTimeMaxAgeToUnixTimestamp
testConvertsDateTimeMaxAgeToUnixTimestamp()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:19
Guzzle\Tests\Plugin\Cookie\CookieTest
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:10
Guzzle\Plugin\Cookie\Cookie
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Plugin/Cookie/Cookie.php:10
Guzzle\Tests\Plugin\Cookie\CookieTest\testCreatesInvalidCharacterString
testCreatesInvalidCharacterString()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:212
Guzzle\Tests\Plugin\Cookie\CookieTest\testMatchesPath
testMatchesPath()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:161
Guzzle\Tests\Plugin\Cookie\CookieTest\testDeterminesIfExpired
testDeterminesIfExpired()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:113
Guzzle\Tests\Plugin\Cookie\CookieTest\testValidatesCookies
testValidatesCookies($name, $value, $domain, $result)
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:202
Guzzle\Tests\Plugin\Cookie\CookieTest\testAddsExpiresBasedOnMaxAge
testAddsExpiresBasedOnMaxAge()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:27
Guzzle\Tests\Plugin\Cookie\CookieTest\testInitializesDefaultValues
testInitializesDefaultValues()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:12
Guzzle\Tests\Plugin\Cookie\CookieTest\cookieValidateProvider
cookieValidateProvider()
Definition: guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieTest.php:187
Guzzle\Tests\Plugin\Cookie