EncryptedCookieMiddlewareTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Middleware;
  16. use Cake\Http\Cookie\Cookie;
  17. use Cake\Http\Cookie\CookieCollection;
  18. use Cake\Http\Middleware\EncryptedCookieMiddleware;
  19. use Cake\Http\Response;
  20. use Cake\Http\ServerRequest;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\Utility\CookieCryptTrait;
  23. /**
  24. * Test for EncryptedCookieMiddleware
  25. */
  26. class EncryptedCookieMiddlewareTest extends TestCase
  27. {
  28. use CookieCryptTrait;
  29. protected $middleware;
  30. protected function _getCookieEncryptionKey()
  31. {
  32. return 'super secret key that no one can guess';
  33. }
  34. /**
  35. * Setup
  36. */
  37. public function setUp()
  38. {
  39. $this->middleware = new EncryptedCookieMiddleware(
  40. ['secret', 'ninja'],
  41. $this->_getCookieEncryptionKey(),
  42. 'aes'
  43. );
  44. }
  45. /**
  46. * Test decoding request cookies
  47. *
  48. * @return void
  49. */
  50. public function testDecodeRequestCookies()
  51. {
  52. $request = new ServerRequest(['url' => '/cookies/nom']);
  53. $request = $request->withCookieParams([
  54. 'plain' => 'always plain',
  55. 'secret' => $this->_encrypt('decoded', 'aes')
  56. ]);
  57. $this->assertNotEquals('decoded', $request->getCookie('decoded'));
  58. $response = new Response();
  59. $next = function ($req, $res) {
  60. $this->assertSame('decoded', $req->getCookie('secret'));
  61. $this->assertSame('always plain', $req->getCookie('plain'));
  62. return $res->withHeader('called', 'yes');
  63. };
  64. $middleware = $this->middleware;
  65. $response = $middleware($request, $response, $next);
  66. $this->assertSame('yes', $response->getHeaderLine('called'), 'Inner middleware not invoked');
  67. }
  68. /**
  69. * Test encoding cookies in the set-cookie header.
  70. *
  71. * @return void
  72. */
  73. public function testEncodeResponseSetCookieHeader()
  74. {
  75. $request = new ServerRequest(['url' => '/cookies/nom']);
  76. $response = new Response();
  77. $next = function ($req, $res) {
  78. return $res->withAddedHeader('Set-Cookie', 'secret=be%20quiet')
  79. ->withAddedHeader('Set-Cookie', 'plain=in%20clear')
  80. ->withAddedHeader('Set-Cookie', 'ninja=shuriken');
  81. };
  82. $middleware = $this->middleware;
  83. $response = $middleware($request, $response, $next);
  84. $this->assertNotContains('ninja=shuriken', $response->getHeaderLine('Set-Cookie'));
  85. $this->assertContains('plain=in%20clear', $response->getHeaderLine('Set-Cookie'));
  86. $cookies = CookieCollection::createFromHeader($response->getHeader('Set-Cookie'));
  87. $this->assertTrue($cookies->has('ninja'));
  88. $this->assertEquals(
  89. 'shuriken',
  90. $this->_decrypt($cookies->get('ninja')->getValue(), 'aes')
  91. );
  92. }
  93. /**
  94. * Test encoding cookies in the cookie collection.
  95. *
  96. * @return void
  97. */
  98. public function testEncodeResponseCookieData()
  99. {
  100. $request = new ServerRequest(['url' => '/cookies/nom']);
  101. $response = new Response();
  102. $next = function ($req, $res) {
  103. return $res->withCookie(new Cookie('secret', 'be quiet'))
  104. ->withCookie(new Cookie('plain', 'in clear'))
  105. ->withCookie(new Cookie('ninja', 'shuriken'));
  106. };
  107. $middleware = $this->middleware;
  108. $response = $middleware($request, $response, $next);
  109. $this->assertNotSame('shuriken', $response->getCookie('ninja'));
  110. $this->assertEquals(
  111. 'shuriken',
  112. $this->_decrypt($response->getCookie('ninja')['value'], 'aes')
  113. );
  114. }
  115. }