123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- namespace GuzzleHttp\Tests\CookieJar;
- use GuzzleHttp\Cookie\CookieJar;
- use GuzzleHttp\Cookie\SetCookie;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Response;
- use PHPUnit\Framework\TestCase;
- /**
- * @covers GuzzleHttp\Cookie\CookieJar
- */
- class CookieJarTest extends TestCase
- {
- /** @var CookieJar */
- private $jar;
- public function setUp()
- {
- $this->jar = new CookieJar();
- }
- protected function getTestCookies()
- {
- return [
- new SetCookie(['Name' => 'foo', 'Value' => 'bar', 'Domain' => 'foo.com', 'Path' => '/', 'Discard' => true]),
- new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2]),
- new SetCookie(['Name' => 'you', 'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => time() + 1000])
- ];
- }
- public function testCreatesFromArray()
- {
- $jar = CookieJar::fromArray([
- 'foo' => 'bar',
- 'baz' => 'bam'
- ], 'example.com');
- $this->assertCount(2, $jar);
- }
- public function testEmptyJarIsCountable()
- {
- $this->assertCount(0, new CookieJar());
- }
- public function testGetsCookiesByName()
- {
- $cookies = $this->getTestCookies();
- foreach ($this->getTestCookies() as $cookie) {
- $this->jar->setCookie($cookie);
- }
- $testCookie = $cookies[0];
- $this->assertEquals($testCookie, $this->jar->getCookieByName($testCookie->getName()));
- $this->assertNull($this->jar->getCookieByName("doesnotexist"));
- $this->assertNull($this->jar->getCookieByName(""));
- }
- /**
- * Provides test data for cookie cookieJar retrieval
- */
- public function getCookiesDataProvider()
- {
- return [
- [['foo', 'baz', 'test', 'muppet', 'googoo'], '', '', '', false],
- [['foo', 'baz', 'muppet', 'googoo'], '', '', '', true],
- [['googoo'], 'www.example.com', '', '', false],
- [['muppet', 'googoo'], 'test.y.example.com', '', '', false],
- [['foo', 'baz'], 'example.com', '', '', false],
- [['muppet'], 'x.y.example.com', '/acme/', '', false],
- [['muppet'], 'x.y.example.com', '/acme/test/', '', false],
- [['googoo'], 'x.y.example.com', '/test/acme/test/', '', false],
- [['foo', 'baz'], 'example.com', '', '', false],
- [['baz'], 'example.com', '', 'baz', false],
- ];
- }
- public function testStoresAndRetrievesCookies()
- {
- $cookies = $this->getTestCookies();
- foreach ($cookies as $cookie) {
- $this->assertTrue($this->jar->setCookie($cookie));
- }
- $this->assertCount(3, $this->jar);
- $this->assertCount(3, $this->jar->getIterator());
- $this->assertEquals($cookies, $this->jar->getIterator()->getArrayCopy());
- }
- public function testRemovesTemporaryCookies()
- {
- $cookies = $this->getTestCookies();
- foreach ($this->getTestCookies() as $cookie) {
- $this->jar->setCookie($cookie);
- }
- $this->jar->clearSessionCookies();
- $this->assertEquals(
- [$cookies[1], $cookies[2]],
- $this->jar->getIterator()->getArrayCopy()
- );
- }
- public function testRemovesSelectively()
- {
- foreach ($this->getTestCookies() as $cookie) {
- $this->jar->setCookie($cookie);
- }
- // Remove foo.com cookies
- $this->jar->clear('foo.com');
- $this->assertCount(2, $this->jar);
- // Try again, removing no further cookies
- $this->jar->clear('foo.com');
- $this->assertCount(2, $this->jar);
- // Remove bar.com cookies with path of /boo
- $this->jar->clear('bar.com', '/boo');
- $this->assertCount(1, $this->jar);
- // Remove cookie by name
- $this->jar->clear(null, null, 'test');
- $this->assertCount(0, $this->jar);
- }
- public function testDoesNotAddIncompleteCookies()
- {
- $this->assertFalse($this->jar->setCookie(new SetCookie()));
- $this->assertFalse($this->jar->setCookie(new SetCookie(array(
- 'Name' => 'foo'
- ))));
- $this->assertFalse($this->jar->setCookie(new SetCookie(array(
- 'Name' => false
- ))));
- $this->assertFalse($this->jar->setCookie(new SetCookie(array(
- 'Name' => true
- ))));
- $this->assertFalse($this->jar->setCookie(new SetCookie(array(
- 'Name' => 'foo',
- 'Domain' => 'foo.com'
- ))));
- }
- public function testDoesNotAddEmptyCookies()
- {
- $this->assertFalse($this->jar->setCookie(new SetCookie(array(
- 'Name' => '',
- 'Domain' => 'foo.com',
- 'Value' => 0
- ))));
- }
- public function testDoesAddValidCookies()
- {
- $this->assertTrue($this->jar->setCookie(new SetCookie(array(
- 'Name' => '0',
- 'Domain' => 'foo.com',
- 'Value' => 0
- ))));
- $this->assertTrue($this->jar->setCookie(new SetCookie(array(
- 'Name' => 'foo',
- 'Domain' => 'foo.com',
- 'Value' => 0
- ))));
- $this->assertTrue($this->jar->setCookie(new SetCookie(array(
- 'Name' => 'foo',
- 'Domain' => 'foo.com',
- 'Value' => 0.0
- ))));
- $this->assertTrue($this->jar->setCookie(new SetCookie(array(
- 'Name' => 'foo',
- 'Domain' => 'foo.com',
- 'Value' => '0'
- ))));
- }
- public function testOverwritesCookiesThatAreOlderOrDiscardable()
- {
- $t = time() + 1000;
- $data = array(
- 'Name' => 'foo',
- 'Value' => 'bar',
- 'Domain' => '.example.com',
- 'Path' => '/',
- 'Max-Age' => '86400',
- 'Secure' => true,
- 'Discard' => true,
- 'Expires' => $t
- );
- // Make sure that the discard cookie is overridden with the non-discard
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $this->assertCount(1, $this->jar);
- $data['Discard'] = false;
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $this->assertCount(1, $this->jar);
- $c = $this->jar->getIterator()->getArrayCopy();
- $this->assertFalse($c[0]->getDiscard());
- // Make sure it doesn't duplicate the cookie
- $this->jar->setCookie(new SetCookie($data));
- $this->assertCount(1, $this->jar);
- // Make sure the more future-ful expiration date supersede the other
- $data['Expires'] = time() + 2000;
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $this->assertCount(1, $this->jar);
- $c = $this->jar->getIterator()->getArrayCopy();
- $this->assertNotEquals($t, $c[0]->getExpires());
- }
- public function testOverwritesCookiesThatHaveChanged()
- {
- $t = time() + 1000;
- $data = array(
- 'Name' => 'foo',
- 'Value' => 'bar',
- 'Domain' => '.example.com',
- 'Path' => '/',
- 'Max-Age' => '86400',
- 'Secure' => true,
- 'Discard' => true,
- 'Expires' => $t
- );
- // Make sure that the discard cookie is overridden with the non-discard
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $data['Value'] = 'boo';
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $this->assertCount(1, $this->jar);
- // Changing the value plus a parameter also must overwrite the existing one
- $data['Value'] = 'zoo';
- $data['Secure'] = false;
- $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
- $this->assertCount(1, $this->jar);
- $c = $this->jar->getIterator()->getArrayCopy();
- $this->assertSame('zoo', $c[0]->getValue());
- }
- public function testAddsCookiesFromResponseWithRequest()
- {
- $response = new Response(200, array(
- 'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
- ));
- $request = new Request('GET', 'http://www.example.com');
- $this->jar->extractCookies($request, $response);
- $this->assertCount(1, $this->jar);
- }
- public function getMatchingCookiesDataProvider()
- {
- return array(
- array('https://example.com', 'foo=bar; baz=foobar'),
- array('http://example.com', ''),
- array('https://example.com:8912', 'foo=bar; baz=foobar'),
- array('https://foo.example.com', 'foo=bar; baz=foobar'),
- array('http://foo.example.com/test/acme/', 'googoo=gaga')
- );
- }
- /**
- * @dataProvider getMatchingCookiesDataProvider
- */
- public function testReturnsCookiesMatchingRequests($url, $cookies)
- {
- $bag = [
- new SetCookie([
- 'Name' => 'foo',
- 'Value' => 'bar',
- 'Domain' => 'example.com',
- 'Path' => '/',
- 'Max-Age' => '86400',
- 'Secure' => true
- ]),
- new SetCookie([
- 'Name' => 'baz',
- 'Value' => 'foobar',
- 'Domain' => 'example.com',
- 'Path' => '/',
- 'Max-Age' => '86400',
- 'Secure' => true
- ]),
- new SetCookie([
- 'Name' => 'test',
- 'Value' => '123',
- 'Domain' => 'www.foobar.com',
- 'Path' => '/path/',
- 'Discard' => true
- ]),
- new SetCookie([
- 'Name' => 'muppet',
- 'Value' => 'cookie_monster',
- 'Domain' => '.y.example.com',
- 'Path' => '/acme/',
- 'Expires' => time() + 86400
- ]),
- new SetCookie([
- 'Name' => 'googoo',
- 'Value' => 'gaga',
- 'Domain' => '.example.com',
- 'Path' => '/test/acme/',
- 'Max-Age' => 1500
- ])
- ];
- foreach ($bag as $cookie) {
- $this->jar->setCookie($cookie);
- }
- $request = new Request('GET', $url);
- $request = $this->jar->withCookieHeader($request);
- $this->assertSame($cookies, $request->getHeaderLine('Cookie'));
- }
- /**
- * @expectedException \RuntimeException
- * @expectedExceptionMessage Invalid cookie: Cookie name must not contain invalid characters: ASCII Control characters (0-31;127), space, tab and the following characters: ()<>@,;:\"/?={}
- */
- public function testThrowsExceptionWithStrictMode()
- {
- $a = new CookieJar(true);
- $a->setCookie(new SetCookie(['Name' => "abc\n", 'Value' => 'foo', 'Domain' => 'bar']));
- }
- public function testDeletesCookiesByName()
- {
- $cookies = $this->getTestCookies();
- $cookies[] = new SetCookie([
- 'Name' => 'other',
- 'Value' => '123',
- 'Domain' => 'bar.com',
- 'Path' => '/boo',
- 'Expires' => time() + 1000
- ]);
- $jar = new CookieJar();
- foreach ($cookies as $cookie) {
- $jar->setCookie($cookie);
- }
- $this->assertCount(4, $jar);
- $jar->clear('bar.com', '/boo', 'other');
- $this->assertCount(3, $jar);
- $names = array_map(function (SetCookie $c) {
- return $c->getName();
- }, $jar->getIterator()->getArrayCopy());
- $this->assertSame(['foo', 'test', 'you'], $names);
- }
- public function testCanConvertToAndLoadFromArray()
- {
- $jar = new CookieJar(true);
- foreach ($this->getTestCookies() as $cookie) {
- $jar->setCookie($cookie);
- }
- $this->assertCount(3, $jar);
- $arr = $jar->toArray();
- $this->assertCount(3, $arr);
- $newCookieJar = new CookieJar(false, $arr);
- $this->assertCount(3, $newCookieJar);
- $this->assertSame($jar->toArray(), $newCookieJar->toArray());
- }
- public function testAddsCookiesWithEmptyPathFromResponse()
- {
- $response = new Response(200, array(
- 'Set-Cookie' => "fpc=foobar; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=;"
- ));
- $request = new Request('GET', 'http://www.example.com');
- $this->jar->extractCookies($request, $response);
- $newRequest = $this->jar->withCookieHeader(new Request('GET', 'http://www.example.com/foo'));
- $this->assertTrue($newRequest->hasHeader('Cookie'));
- }
- public function getCookiePathsDataProvider()
- {
- return [
- ['', '/'],
- ['/', '/'],
- ['/foo', '/'],
- ['/foo/bar', '/foo'],
- ['/foo/bar/', '/foo/bar'],
- ['foo', '/'],
- ['foo/bar', '/'],
- ['foo/bar/', '/'],
- ];
- }
- /**
- * @dataProvider getCookiePathsDataProvider
- */
- public function testCookiePathWithEmptySetCookiePath($uriPath, $cookiePath)
- {
- $response = (new Response(200))
- ->withAddedHeader(
- 'Set-Cookie',
- "foo=bar; expires=Fri, 02-Mar-2019 02:17:40 GMT; domain=www.example.com; path=;"
- )
- ->withAddedHeader(
- 'Set-Cookie',
- "bar=foo; expires=Fri, 02-Mar-2019 02:17:40 GMT; domain=www.example.com; path=foobar;"
- )
- ;
- $request = (new Request('GET', $uriPath))->withHeader('Host', 'www.example.com');
- $this->jar->extractCookies($request, $response);
- $this->assertSame($cookiePath, $this->jar->toArray()[0]['Path']);
- $this->assertSame($cookiePath, $this->jar->toArray()[1]['Path']);
- }
- }
|