protected function createResponse(array $headerData = array(), array $cookieData = array(), $body = '', $status = 200) { $headers = new \Slim\Http\Headers(); $headers->replace($headerData); $cookies = new \Slim\Http\Cookies(); $cookies->replace($cookieData); return new \Slim\Http\Response($headers, $cookies, $body, $status); }
public function testRemove() { $c = new \Slim\Http\Cookies(); $c->remove('foo'); $prop = new \ReflectionProperty($c, 'data'); $prop->setAccessible(true); $cValue = $prop->getValue($c); $this->assertEquals('', $cValue['foo']['value']); $this->assertLessThan(time(), $cValue['foo']['expires']); }
/** * Test serializeCookies and decrypt with string expires * * In this test a cookie with a string typed value for 'expires' is set, * which should be parsed by `strtotime` to a timestamp when it's added to * the headers; this timestamp should then be correctly parsed, and the * value correctly decrypted, by `decodeSecureCookie`. */ public function testSerializeCookiesAndDecryptWithStringExpires() { $value = 'bar'; $headers = new \Slim\Http\Headers(); $settings = array('cookies.encrypt' => true, 'cookies.secret_key' => 'secret', 'cookies.cipher' => MCRYPT_RIJNDAEL_256, 'cookies.cipher_mode' => MCRYPT_MODE_CBC); $cookies = new \Slim\Http\Cookies(); $cookies->set('foo', array('value' => $value, 'expires' => '1 hour')); \Slim\Http\Util::serializeCookies($headers, $cookies, $settings); $encrypted = $headers->get('Set-Cookie'); $encrypted = strstr($encrypted, ';', true); $encrypted = urldecode(substr(strstr($encrypted, '='), 1)); $decrypted = \Slim\Http\Util::decodeSecureCookie($encrypted, $settings['cookies.secret_key'], $settings['cookies.cipher'], $settings['cookies.cipher_mode']); $this->assertEquals($value, $decrypted); $this->assertTrue($value !== $encrypted); }
/** * Constructor * @param array $userSettings Associative array of application settings * @api */ public function __construct(array $userSettings = array()) { parent::__construct(); // Settings $this['settings'] = function ($c) use($userSettings) { $config = new \Slim\Configuration(new \Slim\ConfigurationHandler()); $config->setArray($userSettings); return $config; }; // Environment $this['environment'] = function ($c) { return new \Slim\Environment($_SERVER); }; // Request $this['request'] = function ($c) { $environment = $c['environment']; $headers = new \Slim\Http\Headers($environment); $cookies = new \Slim\Http\Cookies($headers); if ($c['settings']['cookies.encrypt'] === true) { $cookies->decrypt($c['crypt']); } return new \Slim\Http\Request($environment, $headers, $cookies); }; // Response $this['response'] = function ($c) { $headers = new \Slim\Http\Headers(); $cookies = new \Slim\Http\Cookies(); $response = new \Slim\Http\Response($headers, $cookies); $response->setProtocolVersion('HTTP/' . $c['settings']['http.version']); return $response; }; // Router $this['router'] = function ($c) { return new \Slim\Router(); }; // View $this['view'] = function ($c) { $view = $c['settings']['view']; if ($view instanceof \Slim\Interfaces\ViewInterface === false) { throw new \Exception('View class must be instance of \\Slim\\View'); } return $view; }; // Crypt $this['crypt'] = function ($c) { return new \Slim\Crypt($c['settings']['crypt.key'], $c['settings']['crypt.cipher'], $c['settings']['crypt.mode']); }; // Session $this['session'] = function ($c) { $session = new \Slim\Session($c['settings']['session.handler']); $session->start(); if ($c['settings']['session.encrypt'] === true) { $session->decrypt($c['crypt']); } return $session; }; // Flash $this['flash'] = function ($c) { $flash = new \Slim\Flash($c['session'], $c['settings']['session.flash_key']); if ($c['settings']['view'] instanceof \Slim\Interfaces\ViewInterface) { $c['view']->set('flash', $flash); } return $flash; }; // Mode $this['mode'] = function ($c) { $mode = $c['settings']['mode']; if (isset($_ENV['SLIM_MODE'])) { $mode = $_ENV['SLIM_MODE']; } else { $envMode = getenv('SLIM_MODE'); if ($envMode !== false) { $mode = $envMode; } } return $mode; }; // Middleware stack $this['middleware'] = array($this); }
public function testPrefersLeftmostCookieWhenManyCookiesWithSameName() { $header = 'foo=bar; foo=beer'; $cookies = new \Slim\Http\Cookies(); $result = $cookies->parseHeader($header); $this->assertEquals('bar', $result['foo']); }