lithium\test\Unit::_cookieMatch()
Match an $expected
cookie with the given headers. If no headers are provided, then
the value of headers_list()
will be used.
Parameters
-
array
$expected
-
array
$headers
When empty, value of
headers_list()
will be used.
Returns
booleantrue
if the assertion succeeded, false
otherwise.
Source
protected function _cookieMatch($expected, $headers) {
$defaults = ['path' => '/', 'name' => '[\w.-]+'];
$expected += $defaults;
$headers = ($headers) ?: headers_list();
$value = preg_quote(urlencode($expected['value']), '/');
$key = explode('.', $expected['key']);
$key = (count($key) === 1) ? '[' . current($key) . ']' : ('[' . join('][', $key) . ']');
$key = preg_quote($key, '/');
if (isset($expected['expires'])) {
$expectedExpires = strtotime($expected['expires']);
$expires = gmdate('D, d-M-Y H:i:s \G\M\T', $expectedExpires);
$expires = preg_quote($expires, '/');
$maxAge = $expectedExpires - time();
} else {
$expires = '(?:.+?)';
$maxAge = '([0-9]+)';
}
$path = preg_quote($expected['path'], '/');
$pattern = "/^Set\-Cookie:\s{$expected['name']}$key=$value;";
$pattern .= "\sexpires=$expires;";
$pattern .= "\sMax-Age=$maxAge;";
$pattern .= "\spath=$path/";
$match = false;
foreach ($headers as $header) {
if (preg_match($pattern, $header)) {
$match = true;
continue;
}
}
return compact('match', 'pattern');
}