Skip to content

Commit

Permalink
Adds a unit test to serialize and decrypt cookies with string expires.
Browse files Browse the repository at this point in the history
This test complements the pull request #571 (related to issue #568).
  • Loading branch information
shello authored and = committed Jul 18, 2013
1 parent dd6aa9a commit 8af44d9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/Http/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,50 @@ public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAnd
$this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat . '; secure; HttpOnly', $header['Set-Cookie']);
}

/**
* 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);
}

public function testDeleteCookieHeaderWithSurvivingCookie()
{
$header = array('Set-Cookie' => "foo=bar\none=two");
Expand Down

0 comments on commit 8af44d9

Please sign in to comment.