~ballot/wordpress/openstack-objectstorage-breaking-insight

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/tests/Cookie/CookieJarTest.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace GuzzleHttp\Tests\CookieJar;
 
4
 
 
5
use GuzzleHttp\Cookie\CookieJar;
 
6
use GuzzleHttp\Cookie\SetCookie;
 
7
use GuzzleHttp\Message\Request;
 
8
use GuzzleHttp\Message\Response;
 
9
 
 
10
/**
 
11
 * @covers GuzzleHttp\Cookie\CookieJar
 
12
 */
 
13
class CookieJarTest extends \PHPUnit_Framework_TestCase
 
14
{
 
15
    /** @var CookieJar */
 
16
    private $jar;
 
17
 
 
18
    public function setUp()
 
19
    {
 
20
        $this->jar = new CookieJar();
 
21
    }
 
22
 
 
23
    protected function getTestCookies()
 
24
    {
 
25
        return [
 
26
            new SetCookie(['Name' => 'foo',  'Value' => 'bar', 'Domain' => 'foo.com', 'Path' => '/',    'Discard' => true]),
 
27
            new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2]),
 
28
            new SetCookie(['Name' => 'you',  'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => time() + 1000])
 
29
        ];
 
30
    }
 
31
 
 
32
    public function testQuotesBadCookieValues()
 
33
    {
 
34
        $this->assertEquals('foo', CookieJar::getCookieValue('foo'));
 
35
        $this->assertEquals('"foo,bar"', CookieJar::getCookieValue('foo,bar'));
 
36
    }
 
37
 
 
38
    public function testCreatesFromArray()
 
39
    {
 
40
        $jar = CookieJar::fromArray([
 
41
            'foo' => 'bar',
 
42
            'baz' => 'bam'
 
43
        ], 'example.com');
 
44
        $this->assertCount(2, $jar);
 
45
    }
 
46
 
 
47
    /**
 
48
     * Provides test data for cookie cookieJar retrieval
 
49
     */
 
50
    public function getCookiesDataProvider()
 
51
    {
 
52
        return [
 
53
            [['foo', 'baz', 'test', 'muppet', 'googoo'], '', '', '', false],
 
54
            [['foo', 'baz', 'muppet', 'googoo'], '', '', '', true],
 
55
            [['googoo'], 'www.example.com', '', '', false],
 
56
            [['muppet', 'googoo'], 'test.y.example.com', '', '', false],
 
57
            [['foo', 'baz'], 'example.com', '', '', false],
 
58
            [['muppet'], 'x.y.example.com', '/acme/', '', false],
 
59
            [['muppet'], 'x.y.example.com', '/acme/test/', '', false],
 
60
            [['googoo'], 'x.y.example.com', '/test/acme/test/', '', false],
 
61
            [['foo', 'baz'], 'example.com', '', '', false],
 
62
            [['baz'], 'example.com', '', 'baz', false],
 
63
        ];
 
64
    }
 
65
 
 
66
    public function testStoresAndRetrievesCookies()
 
67
    {
 
68
        $cookies = $this->getTestCookies();
 
69
        foreach ($cookies as $cookie) {
 
70
            $this->assertTrue($this->jar->setCookie($cookie));
 
71
        }
 
72
 
 
73
        $this->assertEquals(3, count($this->jar));
 
74
        $this->assertEquals(3, count($this->jar->getIterator()));
 
75
        $this->assertEquals($cookies, $this->jar->getIterator()->getArrayCopy());
 
76
    }
 
77
 
 
78
    public function testRemovesTemporaryCookies()
 
79
    {
 
80
        $cookies = $this->getTestCookies();
 
81
        foreach ($this->getTestCookies() as $cookie) {
 
82
            $this->jar->setCookie($cookie);
 
83
        }
 
84
        $this->jar->clearSessionCookies();
 
85
        $this->assertEquals(
 
86
            [$cookies[1], $cookies[2]],
 
87
            $this->jar->getIterator()->getArrayCopy()
 
88
        );
 
89
    }
 
90
 
 
91
    public function testRemovesSelectively()
 
92
    {
 
93
        foreach ($this->getTestCookies() as $cookie) {
 
94
            $this->jar->setCookie($cookie);
 
95
        }
 
96
 
 
97
        // Remove foo.com cookies
 
98
        $this->jar->clear('foo.com');
 
99
        $this->assertEquals(2, count($this->jar));
 
100
        // Try again, removing no further cookies
 
101
        $this->jar->clear('foo.com');
 
102
        $this->assertEquals(2, count($this->jar));
 
103
 
 
104
        // Remove bar.com cookies with path of /boo
 
105
        $this->jar->clear('bar.com', '/boo');
 
106
        $this->assertEquals(1, count($this->jar));
 
107
 
 
108
        // Remove cookie by name
 
109
        $this->jar->clear(null, null, 'test');
 
110
        $this->assertEquals(0, count($this->jar));
 
111
    }
 
112
 
 
113
    public function testDoesNotAddIncompleteCookies()
 
114
    {
 
115
        $this->assertEquals(false, $this->jar->setCookie(new SetCookie()));
 
116
        $this->assertFalse($this->jar->setCookie(new SetCookie(array(
 
117
            'Name' => 'foo'
 
118
        ))));
 
119
        $this->assertFalse($this->jar->setCookie(new SetCookie(array(
 
120
            'Name' => false
 
121
        ))));
 
122
        $this->assertFalse($this->jar->setCookie(new SetCookie(array(
 
123
            'Name' => true
 
124
        ))));
 
125
        $this->assertFalse($this->jar->setCookie(new SetCookie(array(
 
126
            'Name'   => 'foo',
 
127
            'Domain' => 'foo.com'
 
128
        ))));
 
129
    }
 
130
 
 
131
    public function testDoesAddValidCookies()
 
132
    {
 
133
        $this->assertTrue($this->jar->setCookie(new SetCookie(array(
 
134
            'Name'   => 'foo',
 
135
            'Domain' => 'foo.com',
 
136
            'Value'  => 0
 
137
        ))));
 
138
        $this->assertTrue($this->jar->setCookie(new SetCookie(array(
 
139
            'Name'   => 'foo',
 
140
            'Domain' => 'foo.com',
 
141
            'Value'  => 0.0
 
142
        ))));
 
143
        $this->assertTrue($this->jar->setCookie(new SetCookie(array(
 
144
            'Name'   => 'foo',
 
145
            'Domain' => 'foo.com',
 
146
            'Value'  => '0'
 
147
        ))));
 
148
    }
 
149
 
 
150
    public function testOverwritesCookiesThatAreOlderOrDiscardable()
 
151
    {
 
152
        $t = time() + 1000;
 
153
        $data = array(
 
154
            'Name'    => 'foo',
 
155
            'Value'   => 'bar',
 
156
            'Domain'  => '.example.com',
 
157
            'Path'    => '/',
 
158
            'Max-Age' => '86400',
 
159
            'Secure'  => true,
 
160
            'Discard' => true,
 
161
            'Expires' => $t
 
162
        );
 
163
 
 
164
        // Make sure that the discard cookie is overridden with the non-discard
 
165
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
166
        $this->assertEquals(1, count($this->jar));
 
167
 
 
168
        $data['Discard'] = false;
 
169
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
170
        $this->assertEquals(1, count($this->jar));
 
171
 
 
172
        $c = $this->jar->getIterator()->getArrayCopy();
 
173
        $this->assertEquals(false, $c[0]->getDiscard());
 
174
 
 
175
        // Make sure it doesn't duplicate the cookie
 
176
        $this->jar->setCookie(new SetCookie($data));
 
177
        $this->assertEquals(1, count($this->jar));
 
178
 
 
179
        // Make sure the more future-ful expiration date supersede the other
 
180
        $data['Expires'] = time() + 2000;
 
181
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
182
        $this->assertEquals(1, count($this->jar));
 
183
        $c = $this->jar->getIterator()->getArrayCopy();
 
184
        $this->assertNotEquals($t, $c[0]->getExpires());
 
185
    }
 
186
 
 
187
    public function testOverwritesCookiesThatHaveChanged()
 
188
    {
 
189
        $t = time() + 1000;
 
190
        $data = array(
 
191
            'Name'    => 'foo',
 
192
            'Value'   => 'bar',
 
193
            'Domain'  => '.example.com',
 
194
            'Path'    => '/',
 
195
            'Max-Age' => '86400',
 
196
            'Secure'  => true,
 
197
            'Discard' => true,
 
198
            'Expires' => $t
 
199
        );
 
200
 
 
201
        // Make sure that the discard cookie is overridden with the non-discard
 
202
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
203
 
 
204
        $data['Value'] = 'boo';
 
205
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
206
        $this->assertEquals(1, count($this->jar));
 
207
 
 
208
        // Changing the value plus a parameter also must overwrite the existing one
 
209
        $data['Value'] = 'zoo';
 
210
        $data['Secure'] = false;
 
211
        $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
 
212
        $this->assertEquals(1, count($this->jar));
 
213
 
 
214
        $c = $this->jar->getIterator()->getArrayCopy();
 
215
        $this->assertEquals('zoo', $c[0]->getValue());
 
216
    }
 
217
 
 
218
    public function testAddsCookiesFromResponseWithRequest()
 
219
    {
 
220
        $response = new Response(200, array(
 
221
            'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
 
222
        ));
 
223
        $request = new Request('GET', 'http://www.example.com');
 
224
        $this->jar->extractCookies($request, $response);
 
225
        $this->assertEquals(1, count($this->jar));
 
226
    }
 
227
 
 
228
    public function getMatchingCookiesDataProvider()
 
229
    {
 
230
        return array(
 
231
            array('https://example.com', 'foo=bar; baz=foobar'),
 
232
            array('http://example.com', ''),
 
233
            array('https://example.com:8912', 'foo=bar; baz=foobar'),
 
234
            array('https://foo.example.com', 'foo=bar; baz=foobar'),
 
235
            array('http://foo.example.com/test/acme/', 'googoo=gaga')
 
236
        );
 
237
    }
 
238
 
 
239
    /**
 
240
     * @dataProvider getMatchingCookiesDataProvider
 
241
     */
 
242
    public function testReturnsCookiesMatchingRequests($url, $cookies)
 
243
    {
 
244
        $bag = [
 
245
            new SetCookie([
 
246
                'Name'    => 'foo',
 
247
                'Value'   => 'bar',
 
248
                'Domain'  => 'example.com',
 
249
                'Path'    => '/',
 
250
                'Max-Age' => '86400',
 
251
                'Secure'  => true
 
252
            ]),
 
253
            new SetCookie([
 
254
                'Name'    => 'baz',
 
255
                'Value'   => 'foobar',
 
256
                'Domain'  => 'example.com',
 
257
                'Path'    => '/',
 
258
                'Max-Age' => '86400',
 
259
                'Secure'  => true
 
260
            ]),
 
261
            new SetCookie([
 
262
                'Name'    => 'test',
 
263
                'Value'   => '123',
 
264
                'Domain'  => 'www.foobar.com',
 
265
                'Path'    => '/path/',
 
266
                'Discard' => true
 
267
            ]),
 
268
            new SetCookie([
 
269
                'Name'    => 'muppet',
 
270
                'Value'   => 'cookie_monster',
 
271
                'Domain'  => '.y.example.com',
 
272
                'Path'    => '/acme/',
 
273
                'Expires' => time() + 86400
 
274
            ]),
 
275
            new SetCookie([
 
276
                'Name'    => 'googoo',
 
277
                'Value'   => 'gaga',
 
278
                'Domain'  => '.example.com',
 
279
                'Path'    => '/test/acme/',
 
280
                'Max-Age' => 1500
 
281
            ])
 
282
        ];
 
283
 
 
284
        foreach ($bag as $cookie) {
 
285
            $this->jar->setCookie($cookie);
 
286
        }
 
287
 
 
288
        $request = new Request('GET', $url);
 
289
        $this->jar->addCookieHeader($request);
 
290
        $this->assertEquals($cookies, $request->getHeader('Cookie'));
 
291
    }
 
292
 
 
293
    /**
 
294
     * @expectedException \RuntimeException
 
295
     * @expectedExceptionMessage Invalid cookie: Cookie name must not cannot invalid characters:
 
296
     */
 
297
    public function testThrowsExceptionWithStrictMode()
 
298
    {
 
299
        $a = new CookieJar(true);
 
300
        $a->setCookie(new SetCookie(['Name' => "abc\n", 'Value' => 'foo', 'Domain' => 'bar']));
 
301
    }
 
302
 
 
303
    public function testDeletesCookiesByName()
 
304
    {
 
305
        $cookies = $this->getTestCookies();
 
306
        $cookies[] = new SetCookie([
 
307
            'Name' => 'other',
 
308
            'Value' => '123',
 
309
            'Domain' => 'bar.com',
 
310
            'Path' => '/boo',
 
311
            'Expires' => time() + 1000
 
312
        ]);
 
313
        $jar = new CookieJar();
 
314
        foreach ($cookies as $cookie) {
 
315
            $jar->setCookie($cookie);
 
316
        }
 
317
        $this->assertCount(4, $jar);
 
318
        $jar->clear('bar.com', '/boo', 'other');
 
319
        $this->assertCount(3, $jar);
 
320
        $names = array_map(function (SetCookie $c) {
 
321
            return $c->getName();
 
322
        }, $jar->getIterator()->getArrayCopy());
 
323
        $this->assertEquals(['foo', 'test', 'you'], $names);
 
324
    }
 
325
 
 
326
    public function testCanConvertToAndLoadFromArray()
 
327
    {
 
328
        $jar = new CookieJar(true);
 
329
        foreach ($this->getTestCookies() as $cookie) {
 
330
            $jar->setCookie($cookie);
 
331
        }
 
332
        $this->assertCount(3, $jar);
 
333
        $arr = $jar->toArray();
 
334
        $this->assertCount(3, $arr);
 
335
        $newCookieJar = new CookieJar(false, $arr);
 
336
        $this->assertCount(3, $newCookieJar);
 
337
        $this->assertSame($jar->toArray(), $newCookieJar->toArray());
 
338
    }
 
339
}