Open Journal Systems  3.3.0
CookieParserProvider.php
1 <?php
2 
4 
6 
11 {
17  public function cookieParserDataProvider()
18  {
19  return array(
20  array(
21  'ASIHTTPRequestTestCookie=This+is+the+value; expires=Sat, 26-Jul-2008 17:00:42 GMT; path=/tests; domain=allseeing-i.com; PHPSESSID=6c951590e7a9359bcedde25cda73e43c; path=/";',
22  array(
23  'domain' => 'allseeing-i.com',
24  'path' => '/',
25  'data' => array(
26  'PHPSESSID' => '6c951590e7a9359bcedde25cda73e43c'
27  ),
28  'max_age' => NULL,
29  'expires' => 'Sat, 26-Jul-2008 17:00:42 GMT',
30  'version' => NULL,
31  'secure' => NULL,
32  'discard' => NULL,
33  'port' => NULL,
34  'cookies' => array(
35  'ASIHTTPRequestTestCookie' => 'This+is+the+value'
36  ),
37  'comment' => null,
38  'comment_url' => null,
39  'http_only' => false
40  )
41  ),
42  array('', false),
43  array('foo', false),
44  // Test setting a blank value for a cookie
45  array(array(
46  'foo=', 'foo =', 'foo =;', 'foo= ;', 'foo =', 'foo= '),
47  array(
48  'cookies' => array(
49  'foo' => ''
50  ),
51  'data' => array(),
52  'discard' => null,
53  'domain' => null,
54  'expires' => null,
55  'max_age' => null,
56  'path' => '/',
57  'port' => null,
58  'secure' => null,
59  'version' => null,
60  'comment' => null,
61  'comment_url' => null,
62  'http_only' => false
63  )
64  ),
65  // Test setting a value and removing quotes
66  array(array(
67  'foo=1', 'foo =1', 'foo =1;', 'foo=1 ;', 'foo =1', 'foo= 1', 'foo = 1 ;', 'foo="1"', 'foo="1";', 'foo= "1";'),
68  array(
69  'cookies' => array(
70  'foo' => '1'
71  ),
72  'data' => array(),
73  'discard' => null,
74  'domain' => null,
75  'expires' => null,
76  'max_age' => null,
77  'path' => '/',
78  'port' => null,
79  'secure' => null,
80  'version' => null,
81  'comment' => null,
82  'comment_url' => null,
83  'http_only' => false
84  )
85  ),
86  // Test setting multiple values
87  array(array(
88  'foo=1; bar=2;', 'foo =1; bar = "2"', 'foo=1; bar=2'),
89  array(
90  'cookies' => array(
91  'foo' => '1',
92  'bar' => '2',
93  ),
94  'data' => array(),
95  'discard' => null,
96  'domain' => null,
97  'expires' => null,
98  'max_age' => null,
99  'path' => '/',
100  'port' => null,
101  'secure' => null,
102  'version' => null,
103  'comment' => null,
104  'comment_url' => null,
105  'http_only' => false
106  )
107  ),
108  // Tests getting the domain and path from a reference request
109  array(array(
110  'foo=1; port="80,8081"; httponly', 'foo=1; port="80,8081"; domain=www.test.com; HttpOnly;', 'foo=1; ; domain=www.test.com; path=/path; port="80,8081"; HttpOnly;'),
111  array(
112  'cookies' => array(
113  'foo' => 1
114  ),
115  'data' => array(),
116  'discard' => null,
117  'domain' => 'www.test.com',
118  'expires' => null,
119  'max_age' => null,
120  'path' => '/path',
121  'port' => array('80', '8081'),
122  'secure' => null,
123  'version' => null,
124  'comment' => null,
125  'comment_url' => null,
126  'http_only' => true
127  ),
128  'http://www.test.com/path/'
129  ),
130  // Some of the following tests are based on http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Http/CookieTest.php
131  array(
132  'justacookie=foo; domain=example.com',
133  array(
134  'cookies' => array(
135  'justacookie' => 'foo'
136  ),
137  'domain' => 'example.com',
138  'data' => array(),
139  'discard' => null,
140  'expires' => null,
141  'max_age' => null,
142  'path' => '/',
143  'port' => null,
144  'secure' => null,
145  'version' => null,
146  'comment' => null,
147  'comment_url' => null,
148  'http_only' => false
149  )
150  ),
151  array(
152  'expires=tomorrow; secure; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com',
153  array(
154  'cookies' => array(
155  'expires' => 'tomorrow'
156  ),
157  'domain' => '.example.com',
158  'path' => '/Space Out/',
159  'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
160  'data' => array(),
161  'discard' => null,
162  'port' => null,
163  'secure' => true,
164  'version' => null,
165  'max_age' => null,
166  'comment' => null,
167  'comment_url' => null,
168  'http_only' => false
169  )
170  ),
171  array(
172  'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com; path=/some value/',
173  array(
174  'cookies' => array(
175  'domain' => 'unittests'
176  ),
177  'domain' => 'example.com',
178  'path' => '/some value/',
179  'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
180  'secure' => false,
181  'data' => array(),
182  'discard' => null,
183  'max_age' => null,
184  'port' => null,
185  'version' => null,
186  'comment' => null,
187  'comment_url' => null,
188  'http_only' => false
189  )
190  ),
191  array(
192  'path=indexAction; path=/; domain=.foo.com; expires=Tue, 21-Nov-2006 08:33:44 GMT',
193  array(
194  'cookies' => array(
195  'path' => 'indexAction'
196  ),
197  'domain' => '.foo.com',
198  'path' => '/',
199  'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
200  'secure' => false,
201  'data' => array(),
202  'discard' => null,
203  'max_age' => null,
204  'port' => null,
205  'version' => null,
206  'comment' => null,
207  'comment_url' => null,
208  'http_only' => false
209  )
210  ),
211  array(
212  'secure=sha1; secure; SECURE; domain=some.really.deep.domain.com; version=1; Max-Age=86400',
213  array(
214  'cookies' => array(
215  'secure' => 'sha1'
216  ),
217  'domain' => 'some.really.deep.domain.com',
218  'path' => '/',
219  'secure' => true,
220  'data' => array(),
221  'discard' => null,
222  'expires' => time() + 86400,
223  'max_age' => 86400,
224  'port' => null,
225  'version' => 1,
226  'comment' => null,
227  'comment_url' => null,
228  'http_only' => false
229  )
230  ),
231  array(
232  'PHPSESSID=123456789+abcd%2Cef; secure; discard; domain=.localdomain; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT;',
233  array(
234  'cookies' => array(
235  'PHPSESSID' => '123456789+abcd%2Cef'
236  ),
237  'domain' => '.localdomain',
238  'path' => '/foo/baz',
239  'expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
240  'secure' => true,
241  'data' => array(),
242  'discard' => true,
243  'max_age' => null,
244  'port' => null,
245  'version' => null,
246  'comment' => null,
247  'comment_url' => null,
248  'http_only' => false
249  )
250  ),
251  // rfc6265#section-5.1.4
252  array(
253  'cookie=value',
254  array(
255  'cookies' => array(
256  'cookie' => 'value'
257  ),
258  'domain' => 'example.com',
259  'data' => array(),
260  'discard' => null,
261  'expires' => null,
262  'max_age' => null,
263  'path' => '/some/path',
264  'port' => null,
265  'secure' => null,
266  'version' => null,
267  'comment' => null,
268  'comment_url' => null,
269  'http_only' => false
270  ),
271  'http://example.com/some/path/test.html'
272  ),
273  array(
274  'empty=path',
275  array(
276  'cookies' => array(
277  'empty' => 'path'
278  ),
279  'domain' => 'example.com',
280  'data' => array(),
281  'discard' => null,
282  'expires' => null,
283  'max_age' => null,
284  'path' => '/',
285  'port' => null,
286  'secure' => null,
287  'version' => null,
288  'comment' => null,
289  'comment_url' => null,
290  'http_only' => false
291  ),
292  'http://example.com/test.html'
293  ),
294  array(
295  'baz=qux',
296  array(
297  'cookies' => array(
298  'baz' => 'qux'
299  ),
300  'domain' => 'example.com',
301  'data' => array(),
302  'discard' => null,
303  'expires' => null,
304  'max_age' => null,
305  'path' => '/',
306  'port' => null,
307  'secure' => null,
308  'version' => null,
309  'comment' => null,
310  'comment_url' => null,
311  'http_only' => false
312  ),
313  'http://example.com?query=here'
314  ),
315  array(
316  'test=noSlashPath; path=someString',
317  array(
318  'cookies' => array(
319  'test' => 'noSlashPath'
320  ),
321  'domain' => 'example.com',
322  'data' => array(),
323  'discard' => null,
324  'expires' => null,
325  'max_age' => null,
326  'path' => '/real/path',
327  'port' => null,
328  'secure' => null,
329  'version' => null,
330  'comment' => null,
331  'comment_url' => null,
332  'http_only' => false
333  ),
334  'http://example.com/real/path/'
335  ),
336  );
337  }
338 
342  public function testParseCookie($cookie, $parsed, $url = null)
343  {
344  $c = $this->cookieParserClass;
345  $parser = new $c();
346 
347  $request = null;
348  if ($url) {
349  $url = Url::factory($url);
350  $host = $url->getHost();
351  $path = $url->getPath();
352  } else {
353  $host = '';
354  $path = '';
355  }
356 
357  foreach ((array) $cookie as $c) {
358  $p = $parser->parseCookie($c, $host, $path);
359 
360  // Remove expires values from the assertion if they are relatively equal by allowing a 5 minute difference
361  if ($p['expires'] != $parsed['expires']) {
362  if (abs($p['expires'] - $parsed['expires']) < 300) {
363  unset($p['expires']);
364  unset($parsed['expires']);
365  }
366  }
367 
368  if (is_array($parsed)) {
369  foreach ($parsed as $key => $value) {
370  $this->assertEquals($parsed[$key], $p[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
371  }
372 
373  foreach ($p as $key => $value) {
374  $this->assertEquals($p[$key], $parsed[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
375  }
376  } else {
377  $this->assertEquals($parsed, $p);
378  }
379  }
380  }
381 }
Guzzle\Tests\Parser\Cookie\CookieParserProvider\cookieParserDataProvider
cookieParserDataProvider()
Definition: CookieParserProvider.php:17
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Http\Url
Definition: Url.php:10
Guzzle\Tests\Parser\Cookie\CookieParserProvider\testParseCookie
testParseCookie($cookie, $parsed, $url=null)
Definition: CookieParserProvider.php:342
Guzzle\Tests\Parser\Cookie\CookieParserProvider
Definition: CookieParserProvider.php:10
Guzzle\Tests\Parser\Cookie
Definition: CookieParserProvider.php:3
Guzzle\Http\Url\factory
static factory($url)
Definition: Url.php:34