SecurityHeadersMiddlewareTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.5.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\Middleware\SecurityHeadersMiddleware;
  17. use Cake\Http\ServerRequestFactory;
  18. use Cake\TestSuite\TestCase;
  19. use Zend\Diactoros\Response;
  20. /**
  21. * Test for SecurityMiddleware
  22. */
  23. class SecurityHeadersMiddlewareTest extends TestCase
  24. {
  25. /**
  26. * Test adding the security headers
  27. *
  28. * @return void
  29. */
  30. public function testAddingSecurityHeaders()
  31. {
  32. $request = ServerRequestFactory::fromGlobals([
  33. 'REQUEST_URI' => '/',
  34. ]);
  35. $response = new Response();
  36. $next = function ($req, $res) {
  37. return $res;
  38. };
  39. $middleware = new SecurityHeadersMiddleware();
  40. $middleware
  41. ->setCrossDomainPolicy()
  42. ->setReferrerPolicy()
  43. ->setXFrameOptions()
  44. ->setXssProtection()
  45. ->noOpen()
  46. ->noSniff();
  47. $expected = [
  48. 'x-permitted-cross-domain-policies' => [
  49. 0 => '1; mode=block'
  50. ],
  51. 'referrer-policy' => [
  52. 0 => 'same-origin'
  53. ],
  54. 'x-frame-options' => [
  55. 0 => 'sameorigin'
  56. ],
  57. 'x-download-options' => [
  58. 0 => 'noopen'
  59. ],
  60. 'x-content-type-options' => [
  61. 0 => 'nosniff'
  62. ]
  63. ];
  64. $result = $middleware($request, $response, $next);
  65. $this->assertEquals($expected, $result->getHeaders());
  66. }
  67. /**
  68. * Testing that the URL is required when option is `allow-from`
  69. *
  70. * @expectedException \InvalidArgumentException
  71. * @expectedExceptionMessage The 2nd arg $url can not be empty when `allow-from` is used
  72. * @return void
  73. */
  74. public function testInvalidArgumentExceptionForsetXFrameOptionsUrl()
  75. {
  76. $middleware = new SecurityHeadersMiddleware();
  77. $middleware->setXFrameOptions('allow-from');
  78. }
  79. /**
  80. * Testing the protected checkValues() method that is used by most of the
  81. * methods in the test to avoid passing an invalid argument.
  82. *
  83. * @expectedException \InvalidArgumentException
  84. * @expectedExceptionMessage Invalid arg `INVALID-VALUE!`, use one of these: all, none, master-only, by-content-type, by-ftp-filename
  85. * @return void
  86. */
  87. public function testCheckValues()
  88. {
  89. $middleware = new SecurityHeadersMiddleware();
  90. $middleware->setCrossDomainPolicy('INVALID-VALUE!');
  91. }
  92. }