1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
<?php
namespace Illuminate\Tests\Cookie\Middleware;
use Illuminate\Container\Container;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
use Illuminate\Cookie\CookieJar;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Encryption\Encrypter;
use Illuminate\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Routing\Router;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
class EncryptCookiesTest extends TestCase
{
/**
* @var Router
*/
protected $router;
protected $setCookiePath = 'cookie/set';
protected $queueCookiePath = 'cookie/queue';
protected function setUp(): void
{
parent::setUp();
$container = new Container;
$container->singleton(EncrypterContract::class, function () {
return new Encrypter(str_repeat('a', 16));
});
$this->router = new Router(new Dispatcher, $container);
}
public function testSetCookieEncryption()
{
$this->router->get($this->setCookiePath, [
'middleware' => EncryptCookiesTestMiddleware::class,
'uses' => EncryptCookiesTestController::class.'@setCookies',
]);
$response = $this->router->dispatch(Request::create($this->setCookiePath, 'GET'));
$cookies = $response->headers->getCookies();
$this->assertCount(2, $cookies);
$this->assertSame('encrypted_cookie', $cookies[0]->getName());
$this->assertNotSame('value', $cookies[0]->getValue());
$this->assertSame('unencrypted_cookie', $cookies[1]->getName());
$this->assertSame('value', $cookies[1]->getValue());
}
public function testQueuedCookieEncryption()
{
$this->router->get($this->queueCookiePath, [
'middleware' => [EncryptCookiesTestMiddleware::class, AddQueuedCookiesToResponseTestMiddleware::class],
'uses' => EncryptCookiesTestController::class.'@queueCookies',
]);
$response = $this->router->dispatch(Request::create($this->queueCookiePath, 'GET'));
$cookies = $response->headers->getCookies();
$this->assertCount(2, $cookies);
$this->assertSame('encrypted_cookie', $cookies[0]->getName());
$this->assertNotSame('value', $cookies[0]->getValue());
$this->assertSame('unencrypted_cookie', $cookies[1]->getName());
$this->assertSame('value', $cookies[1]->getValue());
}
}
class EncryptCookiesTestController extends Controller
{
public function setCookies()
{
$response = new Response;
$response->headers->setCookie(new Cookie('encrypted_cookie', 'value'));
$response->headers->setCookie(new Cookie('unencrypted_cookie', 'value'));
return $response;
}
public function queueCookies()
{
return new Response;
}
}
class EncryptCookiesTestMiddleware extends EncryptCookies
{
protected $except = [
'unencrypted_cookie',
];
}
class AddQueuedCookiesToResponseTestMiddleware extends AddQueuedCookiesToResponse
{
public function __construct()
{
$cookie = new CookieJar;
$cookie->queue(new Cookie('encrypted_cookie', 'value'));
$cookie->queue(new Cookie('unencrypted_cookie', 'value'));
$this->cookies = $cookie;
}
}
|