<commit_before><?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         3.5.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\Routing\Middleware;

use Cake\Http\ServerRequestFactory;
use Cake\Routing\Middleware\SecurityMiddleware;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\Response;

/**
 * Test for SecurityMiddleware
 */
class SecurityMiddlewareTest extends TestCase
{

    /**
     * Test adding the security headers
     *
     * @return void
     */
    public function testAddingSecurityHeaders()
    {
        $request = ServerRequestFactory::fromGlobals([
            'REQUEST_URI' => '/',
        ]);
        $response = new Response();
        $next = function ($req, $res) {
            return $res;
        };

        $middleware = new SecurityMiddleware();
        $middleware
            ->setCrossDomainPolicy()
            ->setReferrerPolicy()
            ->setXFrameOptions()
            ->setXssProtection()
            ->noOpen()
            ->noSniff();

        $expected = [
            'x-permitted-cross-domain-policies' => [
                0 => '1; mode=block'
            ],
            'referrer-policy' => [
                0 => 'same-origin'
            ],
            'x-frame-options' => [
                0 => 'sameorigin'
            ],
            'x-download-options' => [
                0 => 'noopen'
            ],
            'x-content-type-options' => [
                0 => 'nosniff'
            ]

        ];

        $result = $middleware($request, $response, $next);
        $this->assertEquals($expected, $result->getHeaders());
    }
}
<commit_msg>Adding more tests for the SecurityMiddleware<commit_after><?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         3.5.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Test\TestCase\Routing\Middleware;

use Cake\Http\ServerRequestFactory;
use Cake\Routing\Middleware\SecurityMiddleware;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\Response;

/**
 * Test for SecurityMiddleware
 */
class SecurityMiddlewareTest extends TestCase
{

    /**
     * Test adding the security headers
     *
     * @return void
     */
    public function testAddingSecurityHeaders()
    {
        $request = ServerRequestFactory::fromGlobals([
            'REQUEST_URI' => '/',
        ]);
        $response = new Response();
        $next = function ($req, $res) {
            return $res;
        };

        $middleware = new SecurityMiddleware();
        $middleware
            ->setCrossDomainPolicy()
            ->setReferrerPolicy()
            ->setXFrameOptions()
            ->setXssProtection()
            ->noOpen()
            ->noSniff();

        $expected = [
            'x-permitted-cross-domain-policies' => [
                0 => '1; mode=block'
            ],
            'referrer-policy' => [
                0 => 'same-origin'
            ],
            'x-frame-options' => [
                0 => 'sameorigin'
            ],
            'x-download-options' => [
                0 => 'noopen'
            ],
            'x-content-type-options' => [
                0 => 'nosniff'
            ]

        ];

        $result = $middleware($request, $response, $next);
        $this->assertEquals($expected, $result->getHeaders());
    }

    /**
     * Testing that the URL is required when option is `allow-from`
     *
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage The 2nd arg $url can not be empty when `allow-from` is used
     * @return void
     */
    public function testInvalidArgumentExceptionForsetXFrameOptionsUrl()
    {
        $middleware = new SecurityMiddleware();
        $middleware->setXFrameOptions('allow-from');
    }

    /**
     * Testing the protected checkValues() method that is used by most of the
     * methods in the test to avoid passing an invalid argument.
     *
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Invalid arg `INVALID-VALUE!`, use one of these: all, none, master-only, by-content-type, by-ftp-filename
     * @return void
     */
    public function testCheckValues()
    {
        $middleware = new SecurityMiddleware();
        $middleware->setCrossDomainPolicy('INVALID-VALUE!');
    }
}

{'fraction_non_alphanumeric': 0.12280366692131398, 'fraction_numerical': 0.003628724216959511, 'mean_word_length': 2.0824014125956447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415756', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1399, 'n_words': 339}