lithium\test\Unit::_cookieMatch()

protected method

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

boolean

true 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(rawurlencode($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', $expectedExpires);
			$expires .= ' ' . gmdate('H:i:s', $expectedExpires) . ' GMT';
			$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');
	}