first commit

This commit is contained in:
2025-01-06 20:47:25 +01:00
commit 3bdbd78c2f
25591 changed files with 3586440 additions and 0 deletions

View File

@@ -0,0 +1,339 @@
<?php
namespace GuzzleHttp\Tests\CookieJar;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
/**
* @covers GuzzleHttp\Cookie\CookieJar
*/
class CookieJarTest extends \PHPUnit_Framework_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 testQuotesBadCookieValues()
{
$this->assertEquals('foo', CookieJar::getCookieValue('foo'));
$this->assertEquals('"foo,bar"', CookieJar::getCookieValue('foo,bar'));
}
public function testCreatesFromArray()
{
$jar = CookieJar::fromArray([
'foo' => 'bar',
'baz' => 'bam'
], 'example.com');
$this->assertCount(2, $jar);
}
/**
* 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->assertEquals(3, count($this->jar));
$this->assertEquals(3, count($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->assertEquals(2, count($this->jar));
// Try again, removing no further cookies
$this->jar->clear('foo.com');
$this->assertEquals(2, count($this->jar));
// Remove bar.com cookies with path of /boo
$this->jar->clear('bar.com', '/boo');
$this->assertEquals(1, count($this->jar));
// Remove cookie by name
$this->jar->clear(null, null, 'test');
$this->assertEquals(0, count($this->jar));
}
public function testDoesNotAddIncompleteCookies()
{
$this->assertEquals(false, $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 testDoesAddValidCookies()
{
$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->assertEquals(1, count($this->jar));
$data['Discard'] = false;
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
$this->assertEquals(1, count($this->jar));
$c = $this->jar->getIterator()->getArrayCopy();
$this->assertEquals(false, $c[0]->getDiscard());
// Make sure it doesn't duplicate the cookie
$this->jar->setCookie(new SetCookie($data));
$this->assertEquals(1, count($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->assertEquals(1, count($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->assertEquals(1, count($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->assertEquals(1, count($this->jar));
$c = $this->jar->getIterator()->getArrayCopy();
$this->assertEquals('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->assertEquals(1, count($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);
$this->jar->addCookieHeader($request);
$this->assertEquals($cookies, $request->getHeader('Cookie'));
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Invalid cookie: Cookie name must not cannot invalid 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->assertEquals(['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());
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace GuzzleHttp\Tests\CookieJar;
use GuzzleHttp\Cookie\FileCookieJar;
use GuzzleHttp\Cookie\SetCookie;
/**
* @covers GuzzleHttp\Cookie\FileCookieJar
*/
class FileCookieJarTest extends \PHPUnit_Framework_TestCase
{
private $file;
public function setUp()
{
$this->file = tempnam('/tmp', 'file-cookies');
}
/**
* @expectedException \RuntimeException
*/
public function testValidatesCookieFile()
{
file_put_contents($this->file, 'true');
new FileCookieJar($this->file);
}
public function testLoadsFromFileFile()
{
$jar = new FileCookieJar($this->file);
$this->assertEquals([], $jar->getIterator()->getArrayCopy());
unlink($this->file);
}
public function testPersistsToFileFile()
{
$jar = new FileCookieJar($this->file);
$jar->setCookie(new SetCookie([
'Name' => 'foo',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'baz',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'boo',
'Value' => 'bar',
'Domain' => 'foo.com',
]));
$this->assertEquals(3, count($jar));
unset($jar);
// Make sure it wrote to the file
$contents = file_get_contents($this->file);
$this->assertNotEmpty($contents);
// Load the cookieJar from the file
$jar = new FileCookieJar($this->file);
// Weeds out temporary and session cookies
$this->assertEquals(2, count($jar));
unset($jar);
unlink($this->file);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace GuzzleHttp\Tests\CookieJar;
use GuzzleHttp\Cookie\SessionCookieJar;
use GuzzleHttp\Cookie\SetCookie;
/**
* @covers GuzzleHttp\Cookie\SessionCookieJar
*/
class SessionCookieJarTest extends \PHPUnit_Framework_TestCase
{
private $sessionVar;
public function setUp()
{
$this->sessionVar = 'sessionKey';
if (!isset($_SESSION)) {
$_SESSION = array();
}
}
/**
* @expectedException \RuntimeException
*/
public function testValidatesCookieSession()
{
$_SESSION[$this->sessionVar] = 'true';
new SessionCookieJar($this->sessionVar);
}
public function testLoadsFromSession()
{
$jar = new SessionCookieJar($this->sessionVar);
$this->assertEquals([], $jar->getIterator()->getArrayCopy());
unset($_SESSION[$this->sessionVar]);
}
public function testPersistsToSession()
{
$jar = new SessionCookieJar($this->sessionVar);
$jar->setCookie(new SetCookie([
'Name' => 'foo',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'baz',
'Value' => 'bar',
'Domain' => 'foo.com',
'Expires' => time() + 1000
]));
$jar->setCookie(new SetCookie([
'Name' => 'boo',
'Value' => 'bar',
'Domain' => 'foo.com',
]));
$this->assertEquals(3, count($jar));
unset($jar);
// Make sure it wrote to the sessionVar in $_SESSION
$contents = $_SESSION[$this->sessionVar];
$this->assertNotEmpty($contents);
// Load the cookieJar from the file
$jar = new SessionCookieJar($this->sessionVar);
// Weeds out temporary and session cookies
$this->assertEquals(2, count($jar));
unset($jar);
unset($_SESSION[$this->sessionVar]);
}
}

View File

@@ -0,0 +1,405 @@
<?php
namespace GuzzleHttp\Tests\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
/**
* @covers GuzzleHttp\Cookie\SetCookie
*/
class SetCookieTest extends \PHPUnit_Framework_TestCase
{
public function testInitializesDefaultValues()
{
$cookie = new SetCookie();
$this->assertEquals('/', $cookie->getPath());
}
public function testConvertsDateTimeMaxAgeToUnixTimestamp()
{
$cookie = new SetCookie(['Expires' => 'November 20, 1984']);
$this->assertInternalType('integer', $cookie->getExpires());
}
public function testAddsExpiresBasedOnMaxAge()
{
$t = time();
$cookie = new SetCookie(['Max-Age' => 100]);
$this->assertEquals($t + 100, $cookie->getExpires());
}
public function testHoldsValues()
{
$t = time();
$data = array(
'Name' => 'foo',
'Value' => 'baz',
'Path' => '/bar',
'Domain' => 'baz.com',
'Expires' => $t,
'Max-Age' => 100,
'Secure' => true,
'Discard' => true,
'HttpOnly' => true,
'foo' => 'baz',
'bar' => 'bam'
);
$cookie = new SetCookie($data);
$this->assertEquals($data, $cookie->toArray());
$this->assertEquals('foo', $cookie->getName());
$this->assertEquals('baz', $cookie->getValue());
$this->assertEquals('baz.com', $cookie->getDomain());
$this->assertEquals('/bar', $cookie->getPath());
$this->assertEquals($t, $cookie->getExpires());
$this->assertEquals(100, $cookie->getMaxAge());
$this->assertTrue($cookie->getSecure());
$this->assertTrue($cookie->getDiscard());
$this->assertTrue($cookie->getHttpOnly());
$this->assertEquals('baz', $cookie->toArray()['foo']);
$this->assertEquals('bam', $cookie->toArray()['bar']);
$cookie->setName('a');
$cookie->setValue('b');
$cookie->setPath('c');
$cookie->setDomain('bar.com');
$cookie->setExpires(10);
$cookie->setMaxAge(200);
$cookie->setSecure(false);
$cookie->setHttpOnly(false);
$cookie->setDiscard(false);
$this->assertEquals('a', $cookie->getName());
$this->assertEquals('b', $cookie->getValue());
$this->assertEquals('c', $cookie->getPath());
$this->assertEquals('bar.com', $cookie->getDomain());
$this->assertEquals(10, $cookie->getExpires());
$this->assertEquals(200, $cookie->getMaxAge());
$this->assertFalse($cookie->getSecure());
$this->assertFalse($cookie->getDiscard());
$this->assertFalse($cookie->getHttpOnly());
}
public function testDeterminesIfExpired()
{
$c = new SetCookie();
$c->setExpires(10);
$this->assertTrue($c->isExpired());
$c->setExpires(time() + 10000);
$this->assertFalse($c->isExpired());
}
public function testMatchesDomain()
{
$cookie = new SetCookie();
$this->assertTrue($cookie->matchesDomain('baz.com'));
$cookie->setDomain('baz.com');
$this->assertTrue($cookie->matchesDomain('baz.com'));
$this->assertFalse($cookie->matchesDomain('bar.com'));
$cookie->setDomain('.baz.com');
$this->assertTrue($cookie->matchesDomain('.baz.com'));
$this->assertTrue($cookie->matchesDomain('foo.baz.com'));
$this->assertFalse($cookie->matchesDomain('baz.bar.com'));
$this->assertTrue($cookie->matchesDomain('baz.com'));
$cookie->setDomain('.127.0.0.1');
$this->assertTrue($cookie->matchesDomain('127.0.0.1'));
$cookie->setDomain('127.0.0.1');
$this->assertTrue($cookie->matchesDomain('127.0.0.1'));
$cookie->setDomain('.com.');
$this->assertFalse($cookie->matchesDomain('baz.com'));
$cookie->setDomain('.local');
$this->assertTrue($cookie->matchesDomain('example.local'));
}
public function testMatchesPath()
{
$cookie = new SetCookie();
$this->assertTrue($cookie->matchesPath('/foo'));
$cookie->setPath('/foo');
$this->assertTrue($cookie->matchesPath('/foo'));
$this->assertTrue($cookie->matchesPath('/foo/bar'));
$this->assertFalse($cookie->matchesPath('/bar'));
}
public function cookieValidateProvider()
{
return array(
array('foo', 'baz', 'bar', true),
array('0', '0', '0', true),
array('', 'baz', 'bar', 'The cookie name must not be empty'),
array('foo', '', 'bar', 'The cookie value must not be empty'),
array('foo', 'baz', '', 'The cookie domain must not be empty'),
array("foo\r", 'baz', '0', 'Cookie name must not cannot invalid characters: =,; \t\r\n\013\014'),
);
}
/**
* @dataProvider cookieValidateProvider
*/
public function testValidatesCookies($name, $value, $domain, $result)
{
$cookie = new SetCookie(array(
'Name' => $name,
'Value' => $value,
'Domain' => $domain
));
$this->assertSame($result, $cookie->validate());
}
public function testDoesNotMatchIp()
{
$cookie = new SetCookie(['Domain' => '192.168.16.']);
$this->assertFalse($cookie->matchesDomain('192.168.16.121'));
}
public function testConvertsToString()
{
$t = 1382916008;
$cookie = new SetCookie([
'Name' => 'test',
'Value' => '123',
'Domain' => 'foo.com',
'Expires' => $t,
'Path' => '/abc',
'HttpOnly' => true,
'Secure' => true
]);
$this->assertEquals(
'test=123; Domain=foo.com; Path=/abc; Expires=Sun, 27 Oct 2013 23:20:08 GMT; Secure; HttpOnly',
(string) $cookie
);
}
/**
* Provides the parsed information from a cookie
*
* @return array
*/
public function cookieParserDataProvider()
{
return array(
array(
'ASIHTTPRequestTestCookie=This+is+the+value; expires=Sat, 26-Jul-2008 17:00:42 GMT; path=/tests; domain=allseeing-i.com; PHPSESSID=6c951590e7a9359bcedde25cda73e43c; path=/";',
array(
'Domain' => 'allseeing-i.com',
'Path' => '/',
'PHPSESSID' => '6c951590e7a9359bcedde25cda73e43c',
'Max-Age' => NULL,
'Expires' => 'Sat, 26-Jul-2008 17:00:42 GMT',
'Secure' => NULL,
'Discard' => NULL,
'Name' => 'ASIHTTPRequestTestCookie',
'Value' => 'This+is+the+value',
'HttpOnly' => false
)
),
array('', []),
array('foo', []),
// Test setting a blank value for a cookie
array(array(
'foo=', 'foo =', 'foo =;', 'foo= ;', 'foo =', 'foo= '),
array(
'Name' => 'foo',
'Value' => '',
'Discard' => null,
'Domain' => null,
'Expires' => null,
'Max-Age' => null,
'Path' => '/',
'Secure' => null,
'HttpOnly' => false
)
),
// Test setting a value and removing quotes
array(array(
'foo=1', 'foo =1', 'foo =1;', 'foo=1 ;', 'foo =1', 'foo= 1', 'foo = 1 ;', 'foo="1"', 'foo="1";', 'foo= "1";'),
array(
'Name' => 'foo',
'Value' => '1',
'Discard' => null,
'Domain' => null,
'Expires' => null,
'Max-Age' => null,
'Path' => '/',
'Secure' => null,
'HttpOnly' => false
)
),
// Some of the following tests are based on http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Http/CookieTest.php
array(
'justacookie=foo; domain=example.com',
array(
'Name' => 'justacookie',
'Value' => 'foo',
'Domain' => 'example.com',
'Discard' => null,
'Expires' => null,
'Max-Age' => null,
'Path' => '/',
'Secure' => null,
'HttpOnly' => false
)
),
array(
'expires=tomorrow; secure; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com',
array(
'Name' => 'expires',
'Value' => 'tomorrow',
'Domain' => '.example.com',
'Path' => '/Space Out/',
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
'Discard' => null,
'Secure' => true,
'Max-Age' => null,
'HttpOnly' => false
)
),
array(
'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com; path=/some value/',
array(
'Name' => 'domain',
'Value' => 'unittests',
'Domain' => 'example.com',
'Path' => '/some value/',
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
'Secure' => false,
'Discard' => null,
'Max-Age' => null,
'HttpOnly' => false
)
),
array(
'path=indexAction; path=/; domain=.foo.com; expires=Tue, 21-Nov-2006 08:33:44 GMT',
array(
'Name' => 'path',
'Value' => 'indexAction',
'Domain' => '.foo.com',
'Path' => '/',
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
'Secure' => false,
'Discard' => null,
'Max-Age' => null,
'HttpOnly' => false
)
),
array(
'secure=sha1; secure; SECURE; domain=some.really.deep.domain.com; version=1; Max-Age=86400',
array(
'Name' => 'secure',
'Value' => 'sha1',
'Domain' => 'some.really.deep.domain.com',
'Path' => '/',
'Secure' => true,
'Discard' => null,
'Expires' => time() + 86400,
'Max-Age' => 86400,
'HttpOnly' => false,
'version' => '1'
)
),
array(
'PHPSESSID=123456789+abcd%2Cef; secure; discard; domain=.localdomain; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT;',
array(
'Name' => 'PHPSESSID',
'Value' => '123456789+abcd%2Cef',
'Domain' => '.localdomain',
'Path' => '/foo/baz',
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
'Secure' => true,
'Discard' => true,
'Max-Age' => null,
'HttpOnly' => false
)
),
);
}
/**
* @dataProvider cookieParserDataProvider
*/
public function testParseCookie($cookie, $parsed)
{
foreach ((array) $cookie as $v) {
$c = SetCookie::fromString($v);
$p = $c->toArray();
if (isset($p['Expires'])) {
// Remove expires values from the assertion if they are relatively equal
if (abs($p['Expires'] != strtotime($parsed['Expires'])) < 40) {
unset($p['Expires']);
unset($parsed['Expires']);
}
}
if (!empty($parsed)) {
foreach ($parsed as $key => $value) {
$this->assertEquals($parsed[$key], $p[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
}
foreach ($p as $key => $value) {
$this->assertEquals($p[$key], $parsed[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
}
} else {
$this->assertEquals([
'Name' => null,
'Value' => null,
'Domain' => null,
'Path' => '/',
'Max-Age' => null,
'Expires' => null,
'Secure' => false,
'Discard' => false,
'HttpOnly' => false,
], $p);
}
}
}
/**
* Provides the data for testing isExpired
*
* @return array
*/
public function isExpiredProvider() {
return array(
array(
'FOO=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT;',
true,
),
array(
'FOO=bar; expires=Thu, 01 Jan 1970 00:00:01 GMT;',
true,
),
array(
'FOO=bar; expires='.date(\DateTime::RFC1123, time()+10).';',
false,
),
array(
'FOO=bar; expires='.date(\DateTime::RFC1123, time()-10).';',
true,
),
array(
'FOO=bar;',
false,
),
);
}
/**
* @dataProvider isExpiredProvider
*/
public function testIsExpired($cookie, $expired)
{
$this->assertEquals(
$expired,
SetCookie::fromString($cookie)->isExpired()
);
}
}