Skip to content

Instantly share code, notes, and snippets.

@einkoro
Created May 25, 2014 02:23
Show Gist options
  • Save einkoro/4f843a952c8a412fde28 to your computer and use it in GitHub Desktop.
Save einkoro/4f843a952c8a412fde28 to your computer and use it in GitHub Desktop.
HHVM vs PHP setcookie tests
<?php
ini_set('date.timezone', 'UTC');
setcookie('name');
setcookie('name', 'value');
setcookie('name', 'value');
setcookie('name', 'space value');
setcookie('name', 'value', 0);
setcookie('name', 'value', $tsp = time() + 5);
setcookie('name', 'value', $tsn = time() - 6);
setcookie('name', 'value', $tsc = time());
setcookie('name', 'value', 0, '/path/');
setcookie('name', 'value', 0, '', 'domain.tld');
setcookie('name', 'value', 0, '', '', TRUE);
setcookie('name', 'value', 0, '', '', FALSE, TRUE);
setcookie('name', 'v1');
setcookie('name', 'v2');
setcookie('name', 'v3');
$expected = array(
'Set-Cookie: name=deleted; expires='.date('D, d-M-Y H:i:s', 1).' GMT; Max-Age=0',
'Set-Cookie: name=value',
'Set-Cookie: name=value',
'Set-Cookie: name=space+value',
'Set-Cookie: name=value',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsp).' GMT; Max-Age=5',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsn).' GMT; Max-Age=-6',
'Set-Cookie: name=value; expires='.date('D, d-M-Y H:i:s', $tsc).' GMT; Max-Age=0',
'Set-Cookie: name=value; path=/path/',
'Set-Cookie: name=value; domain=domain.tld',
'Set-Cookie: name=value; secure',
'Set-Cookie: name=value; httponly',
'Set-Cookie: name=v1',
'Set-Cookie: name=v2',
'Set-Cookie: name=v3'
);
$headers = headers_list();
echo "<pre>";
print_r($headers);
echo "</pre>";
if (($i = count($expected)) > count($headers))
{
echo "Less headers are being sent than expected - aborting";
return;
}
do
{
if (strncmp(current($headers), 'Set-Cookie:', 11) !== 0)
{
continue;
}
if (current($headers) === current($expected))
{
$i--;
}
else
{
echo "Header mismatch:\n\tExpected: "
.current($expected)
."\n\tReceived: ".current($headers)."\n";
}
next($expected);
}
while (next($headers) !== FALSE);
echo ($i === 0)
? 'OK'
: 'A total of '.$i.' errors found.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment