EncryptedCookieMiddlewareTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\CookieCollection;
  17. use Cake\Http\Middleware\EncryptedCookieMiddleware;
  18. use Cake\Http\Response;
  19. use Cake\Http\ServerRequest;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\CookieCryptTrait;
  22. /**
  23. * Test for EncryptedCookieMiddleware
  24. */
  25. class EncryptedCookieMiddlewareTest extends TestCase
  26. {
  27. use CookieCryptTrait;
  28. protected $middleware;
  29. protected function _getCookieEncryptionKey()
  30. {
  31. return 'super secret key that no one can guess';
  32. }
  33. /**
  34. * Setup
  35. */
  36. public function setUp()
  37. {
  38. $this->middleware = new EncryptedCookieMiddleware(
  39. ['secret', 'ninja'],
  40. $this->_getCookieEncryptionKey(),
  41. 'aes'
  42. );
  43. }
  44. /**
  45. * Test decoding request cookies
  46. *
  47. * @return void
  48. */
  49. public function testDecodeRequestCookies()
  50. {
  51. $request = new ServerRequest(['url' => '/cookies/nom']);
  52. $request = $request->withCookieParams([
  53. 'plain' => 'always plain',
  54. 'secret' => $this->_encrypt('decoded', 'aes')
  55. ]);
  56. $this->assertNotEquals('decoded', $request->getCookie('decoded'));
  57. $response = new Response();
  58. $next = function ($req, $res) {
  59. $this->assertSame('decoded', $req->getCookie('secret'));
  60. $this->assertSame('always plain', $req->getCookie('plain'));
  61. return $res->withHeader('called', 'yes');
  62. };
  63. $middleware = $this->middleware;
  64. $response = $middleware($request, $response, $next);
  65. $this->assertSame('yes', $response->getHeaderLine('called'), 'Inner middleware not invoked');
  66. }
  67. /**
  68. * Test encoding cookies in the set-cookie header.
  69. *
  70. * @return void
  71. */
  72. public function testEncodeResponseSetCookieHeader()
  73. {
  74. $request = new ServerRequest(['url' => '/cookies/nom']);
  75. $response = new Response();
  76. $next = function ($req, $res) {
  77. return $res->withAddedHeader('Set-Cookie', 'secret=be%20quiet')
  78. ->withAddedHeader('Set-Cookie', 'plain=in%20clear')
  79. ->withAddedHeader('Set-Cookie', 'ninja=shuriken');
  80. };
  81. $middleware = $this->middleware;
  82. $response = $middleware($request, $response, $next);
  83. $this->assertNotContains('ninja=shuriken', $response->getHeaderLine('Set-Cookie'));
  84. $this->assertContains('plain=in%20clear', $response->getHeaderLine('Set-Cookie'));
  85. $cookies = CookieCollection::createFromHeader($response->getHeader('Set-Cookie'));
  86. $this->assertTrue($cookies->has('ninja'));
  87. $this->assertEquals(
  88. 'shuriken',
  89. $this->_decrypt($cookies->get('ninja')->getValue(), 'aes')
  90. );
  91. }
  92. /**
  93. * Test encoding cookies in the cookie collection.
  94. *
  95. * @return void
  96. */
  97. public function testEncodeResponseCookieData()
  98. {
  99. $request = new ServerRequest(['url' => '/cookies/nom']);
  100. $response = new Response();
  101. $next = function ($req, $res) {
  102. return $res->withCookie('secret', 'be quiet')
  103. ->withCookie('plain', 'in clear')
  104. ->withCookie('ninja', 'shuriken');
  105. };
  106. $middleware = $this->middleware;
  107. $response = $middleware($request, $response, $next);
  108. $this->assertNotSame('shuriken', $response->getCookie('ninja'));
  109. $this->assertEquals(
  110. 'shuriken',
  111. $this->_decrypt($response->getCookie('ninja')['value'], 'aes')
  112. );
  113. }
  114. }