Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Unit tests for HTTP_Request2 package
4
 *
5
 * PHP version 5
6
 *
7
 * LICENSE:
8
 *
9
 * Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
10
 * All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 *
16
 *    * Redistributions of source code must retain the above copyright
17
 *      notice, this list of conditions and the following disclaimer.
18
 *    * Redistributions in binary form must reproduce the above copyright
19
 *      notice, this list of conditions and the following disclaimer in the
20
 *      documentation and/or other materials provided with the distribution.
21
 *    * The names of the authors may not be used to endorse or promote products
22
 *      derived from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category   HTTP
37
 * @package    HTTP_Request2
38
 * @author     Alexey Borzov <avb@php.net>
39
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
40
 * @version    SVN: $Id: CookieJarTest.php 309665 2011-03-24 21:03:48Z avb $
41
 * @link       http://pear.php.net/package/HTTP_Request2
42
 */
43
 
44
/** Stores cookies and passes them between HTTP requests */
45
require_once 'HTTP/Request2/CookieJar.php';
46
/** Helper for PHPUnit includes */
47
require_once dirname(dirname(__FILE__)) . '/TestHelper.php';
48
 
49
/**
50
 * Unit test for HTTP_Request2_CookieJar class
51
 */
52
class HTTP_Request2_CookieJarTest extends PHPUnit_Framework_TestCase
53
{
54
   /**
55
    * Cookie jar instance being tested
56
    * @var HTTP_Request2_CookieJar
57
    */
58
    protected $jar;
59
 
60
    protected function setUp()
61
    {
62
        $this->jar = new HTTP_Request2_CookieJar();
63
    }
64
 
65
   /**
66
    * Test that we can't store junk "cookies" in jar
67
    *
68
    * @dataProvider invalidCookieProvider
69
    * @expectedException HTTP_Request2_LogicException
70
    */
71
    public function testStoreInvalid($cookie)
72
    {
73
        $this->jar->store($cookie);
74
    }
75
 
76
   /**
77
    *
78
    * @dataProvider noPSLDomainsProvider
79
    */
80
    public function testDomainMatchNoPSL($requestHost, $cookieDomain, $expected)
81
    {
82
        $this->jar->usePublicSuffixList(false);
83
        $this->assertEquals($expected, $this->jar->domainMatch($requestHost, $cookieDomain));
84
    }
85
 
86
   /**
87
    *
88
    * @dataProvider PSLDomainsProvider
89
    */
90
    public function testDomainMatchPSL($requestHost, $cookieDomain, $expected)
91
    {
92
        $this->jar->usePublicSuffixList(true);
93
        $this->assertEquals($expected, $this->jar->domainMatch($requestHost, $cookieDomain));
94
    }
95
 
96
    public function testConvertExpiresToISO8601()
97
    {
98
        $dt = new DateTime();
99
        $dt->setTimezone(new DateTimeZone('UTC'));
100
        $dt->modify('+1 day');
101
 
102
        $this->jar->store(array(
103
            'name'    => 'foo',
104
            'value'   => 'bar',
105
            'domain'  => '.example.com',
106
            'path'    => '/',
107
            'expires' => $dt->format(DateTime::COOKIE),
108
            'secure'  => false
109
        ));
110
        $cookies = $this->jar->getAll();
111
        $this->assertEquals($cookies[0]['expires'], $dt->format(DateTime::ISO8601));
112
    }
113
 
114
    public function testProblem2038()
115
    {
116
        $this->jar->store(array(
117
            'name'    => 'foo',
118
            'value'   => 'bar',
119
            'domain'  => '.example.com',
120
            'path'    => '/',
121
            'expires' => 'Sun, 01 Jan 2040 03:04:05 GMT',
122
            'secure'  => false
123
        ));
124
        $cookies = $this->jar->getAll();
125
        $this->assertEquals(array(array(
126
            'name'    => 'foo',
127
            'value'   => 'bar',
128
            'domain'  => '.example.com',
129
            'path'    => '/',
130
            'expires' => '2040-01-01T03:04:05+0000',
131
            'secure'  => false
132
        )), $cookies);
133
    }
134
 
135
    public function testStoreExpired()
136
    {
137
        $base = array(
138
            'name'    => 'foo',
139
            'value'   => 'bar',
140
            'domain'  => '.example.com',
141
            'path'    => '/',
142
            'secure'  => false
143
        );
144
 
145
        $dt = new DateTime();
146
        $dt->setTimezone(new DateTimeZone('UTC'));
147
        $dt->modify('-1 day');
148
        $yesterday = $dt->format(DateTime::COOKIE);
149
 
150
        $dt->modify('+2 days');
151
        $tomorrow = $dt->format(DateTime::COOKIE);
152
 
153
        $this->jar->store($base + array('expires' => $yesterday));
154
        $this->assertEquals(0, count($this->jar->getAll()));
155
 
156
        $this->jar->store($base + array('expires' => $tomorrow));
157
        $this->assertEquals(1, count($this->jar->getAll()));
158
        $this->jar->store($base + array('expires' => $yesterday));
159
        $this->assertEquals(0, count($this->jar->getAll()));
160
    }
161
 
162
   /**
163
    *
164
    * @dataProvider cookieAndSetterProvider
165
    */
166
    public function testGetDomainAndPathFromSetter($cookie, $setter, $expected)
167
    {
168
        $this->jar->store($cookie, $setter);
169
        $expected = array_merge($cookie, $expected);
170
        $cookies  = $this->jar->getAll();
171
        $this->assertEquals($expected, $cookies[0]);
172
    }
173
 
174
   /**
175
    *
176
    * @dataProvider cookieMatchProvider
177
    */
178
    public function testGetMatchingCookies($url, $expectedCount)
179
    {
180
        $cookies = array(
181
            array('domain' => '.example.com', 'path' => '/', 'secure' => false),
182
            array('domain' => '.example.com', 'path' => '/', 'secure' => true),
183
            array('domain' => '.example.com', 'path' => '/path', 'secure' => false),
184
            array('domain' => '.example.com', 'path' => '/other', 'secure' => false),
185
            array('domain' => 'example.com', 'path' => '/', 'secure' => false),
186
            array('domain' => 'www.example.com', 'path' => '/', 'secure' => false),
187
            array('domain' => 'specific.example.com', 'path' => '/path', 'secure' => false),
188
            array('domain' => 'nowww.example.com', 'path' => '/', 'secure' => false),
189
        );
190
 
191
        for ($i = 0; $i < count($cookies); $i++) {
192
            $this->jar->store($cookies[$i] + array('expires' => null, 'name' => "cookie{$i}", 'value' => "cookie_{$i}_value"));
193
        }
194
 
195
        $this->assertEquals($expectedCount, count($this->jar->getMatching(new Net_URL2($url))));
196
    }
197
 
198
    public function testLongestPathFirst()
199
    {
200
        $cookie = array(
201
            'name'    => 'foo',
202
            'domain'  => '.example.com',
203
        );
204
        foreach (array('/', '/specific/path/', '/specific/') as $path) {
205
            $this->jar->store($cookie + array('path' => $path, 'value' => str_replace('/', '_', $path)));
206
        }
207
        $this->assertEquals(
208
            'foo=_specific_path_; foo=_specific_; foo=_',
209
            $this->jar->getMatching(new Net_URL2('http://example.com/specific/path/file.php'), true)
210
        );
211
    }
212
 
213
    public function testSerializable()
214
    {
215
        $dt = new DateTime();
216
        $dt->setTimezone(new DateTimeZone('UTC'));
217
        $dt->modify('+1 day');
218
        $cookie = array('domain' => '.example.com', 'path' => '/', 'secure' => false, 'value' => 'foo');
219
 
220
        $this->jar->store($cookie + array('name' => 'session', 'expires' => null));
221
        $this->jar->store($cookie + array('name' => 'long', 'expires' => $dt->format(DateTime::COOKIE)));
222
 
223
        $newJar  = unserialize(serialize($this->jar));
224
        $cookies = $newJar->getAll();
225
        $this->assertEquals(1, count($cookies));
226
        $this->assertEquals('long', $cookies[0]['name']);
227
 
228
        $this->jar->serializeSessionCookies(true);
229
        $newJar = unserialize(serialize($this->jar));
230
        $this->assertEquals($this->jar->getAll(), $newJar->getAll());
231
    }
232
 
233
    public function testRemoveExpiredOnUnserialize()
234
    {
235
        $dt = new DateTime();
236
        $dt->setTimezone(new DateTimeZone('UTC'));
237
        $dt->modify('+2 seconds');
238
 
239
        $this->jar->store(array(
240
            'name'    => 'foo',
241
            'value'   => 'bar',
242
            'domain'  => '.example.com',
243
            'path'    => '/',
244
            'expires' => $dt->format(DateTime::COOKIE),
245
        ));
246
 
247
        $serialized = serialize($this->jar);
248
        sleep(2);
249
        $newJar = unserialize($serialized);
250
        $this->assertEquals(array(), $newJar->getAll());
251
    }
252
 
253
    public static function invalidCookieProvider()
254
    {
255
        return array(
256
            array(array()),
257
            array(array('name' => 'foo')),
258
            array(array(
259
                'name'    => 'a name',
260
                'value'   => 'bar',
261
                'domain'  => '.example.com',
262
                'path'    => '/',
263
            )),
264
            array(array(
265
                'name'    => 'foo',
266
                'value'   => 'a value',
267
                'domain'  => '.example.com',
268
                'path'    => '/',
269
            )),
270
            array(array(
271
                'name'    => 'foo',
272
                'value'   => 'bar',
273
                'domain'  => '.example.com',
274
                'path'    => null,
275
            )),
276
            array(array(
277
                'name'    => 'foo',
278
                'value'   => 'bar',
279
                'domain'  => null,
280
                'path'    => '/',
281
            )),
282
            array(array(
283
                'name'    => 'foo',
284
                'value'   => 'bar',
285
                'domain'  => '.example.com',
286
                'path'    => '/',
287
                'expires' => 'invalid date',
288
            )),
289
        );
290
    }
291
 
292
    public static function noPSLdomainsProvider()
293
    {
294
        return array(
295
            array('localhost', 'localhost', true),
296
            array('www.example.com', 'www.example.com', true),
297
            array('127.0.0.1', '127.0.0.1', true),
298
            array('127.0.0.1', '.0.0.1', false),
299
            array('www.example.com', '.example.com', true),
300
            array('deep.within.example.com', '.example.com', true),
301
            array('example.com', '.com', false),
302
            array('anotherexample.com', 'example.com', false),
303
            array('whatever.msk.ru', '.msk.ru', true),
304
            array('whatever.co.uk', '.co.uk', true),
305
            array('whatever.uk', '.whatever.uk', true),
306
            array('whatever.tokyo.jp', '.whatever.tokyo.jp', true),
307
            array('metro.tokyo.jp', '.metro.tokyo.jp', true),
308
            array('foo.bar', '.foo.bar', true)
309
        );
310
    }
311
 
312
    public static function PSLdomainsProvider()
313
    {
314
        return array(
315
            array('localhost', 'localhost', true),
316
            array('www.example.com', 'www.example.com', true),
317
            array('127.0.0.1', '127.0.0.1', true),
318
            array('127.0.0.1', '.0.0.1', false),
319
            array('www.example.com', '.example.com', true),
320
            array('deep.within.example.com', '.example.com', true),
321
            array('example.com', '.com', false),
322
            array('anotherexample.com', 'example.com', false),
323
            array('whatever.msk.ru', '.msk.ru', false),
324
            array('whatever.co.uk', '.co.uk', false),
325
            array('whatever.uk', '.whatever.uk', false),
326
            array('whatever.tokyo.jp', '.whatever.tokyo.jp', false),
327
            array('metro.tokyo.jp', '.metro.tokyo.jp', true),
328
            array('foo.bar', '.foo.bar', true)
329
        );
330
    }
331
 
332
    public static function cookieAndSetterProvider()
333
    {
334
        return array(
335
            array(
336
                array(
337
                    'name'    => 'foo',
338
                    'value'   => 'bar',
339
                    'domain'  => null,
340
                    'path'    => null,
341
                    'expires' => null,
342
                    'secure'  => false
343
                ),
344
                new Net_Url2('http://example.com/directory/file.php'),
345
                array(
346
                    'domain'  => 'example.com',
347
                    'path'    => '/directory/'
348
                )
349
            ),
350
            array(
351
                array(
352
                    'name'    => 'foo',
353
                    'value'   => 'bar',
354
                    'domain'  => '.example.com',
355
                    'path'    => null,
356
                    'expires' => null,
357
                    'secure'  => false
358
                ),
359
                new Net_Url2('http://example.com/path/to/file.php'),
360
                array(
361
                    'path'    => '/path/to/'
362
                )
363
            ),
364
            array(
365
                array(
366
                    'name'    => 'foo',
367
                    'value'   => 'bar',
368
                    'domain'  => null,
369
                    'path'    => '/',
370
                    'expires' => null,
371
                    'secure'  => false
372
                ),
373
                new Net_Url2('http://example.com/another/file.php'),
374
                array(
375
                    'domain'  => 'example.com'
376
                )
377
            )
378
        );
379
    }
380
 
381
    public static function cookieMatchProvider()
382
    {
383
        return array(
384
            array('http://www.example.com/path/file.php', 4),
385
            array('https://www.example.com/path/file.php', 5),
386
            array('http://example.com/path/file.php', 3),
387
            array('http://specific.example.com/path/file.php', 4),
388
            array('http://specific.example.com/other/file.php', 3),
389
            array('http://another.example.com/another', 2)
390
        );
391
    }
392
}
393
?>