mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-12 14:31:35 +00:00
Added more tests
This commit is contained in:
parent
f3f93a8205
commit
7d27bff062
8 changed files with 756 additions and 1 deletions
101
tests/EventSubscriber/RedirectToHttpsSubscriberTest.php
Normal file
101
tests/EventSubscriber/RedirectToHttpsSubscriberTest.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\EventSubscriber;
|
||||
|
||||
use App\EventSubscriber\RedirectToHttpsSubscriber;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\Security\Http\HttpUtils;
|
||||
|
||||
final class RedirectToHttpsSubscriberTest extends TestCase
|
||||
{
|
||||
private function makeEvent(string $url, bool $isMainRequest = true): RequestEvent
|
||||
{
|
||||
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||
$request = Request::create($url);
|
||||
return new RequestEvent($kernel, $request, $isMainRequest ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::SUB_REQUEST);
|
||||
}
|
||||
|
||||
public function testHttpRequestIsRedirectedToHttpsWhenEnabled(): void
|
||||
{
|
||||
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||
$event = $this->makeEvent('http://example.com/some/path');
|
||||
|
||||
$subscriber->onKernelRequest($event);
|
||||
|
||||
$this->assertTrue($event->hasResponse());
|
||||
$response = $event->getResponse();
|
||||
$this->assertStringStartsWith('https://', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testHttpsRequestIsNotRedirectedWhenEnabled(): void
|
||||
{
|
||||
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||
$event = $this->makeEvent('https://example.com/some/path');
|
||||
|
||||
$subscriber->onKernelRequest($event);
|
||||
|
||||
$this->assertFalse($event->hasResponse());
|
||||
}
|
||||
|
||||
public function testHttpRequestIsNotRedirectedWhenDisabled(): void
|
||||
{
|
||||
$subscriber = new RedirectToHttpsSubscriber(false, new HttpUtils());
|
||||
$event = $this->makeEvent('http://example.com/some/path');
|
||||
|
||||
$subscriber->onKernelRequest($event);
|
||||
|
||||
$this->assertFalse($event->hasResponse());
|
||||
}
|
||||
|
||||
public function testSubRequestIsNotRedirected(): void
|
||||
{
|
||||
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||
$event = $this->makeEvent('http://example.com/', false);
|
||||
|
||||
$subscriber->onKernelRequest($event);
|
||||
|
||||
$this->assertFalse($event->hasResponse());
|
||||
}
|
||||
|
||||
public function testRedirectUrlPreservesPath(): void
|
||||
{
|
||||
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||
$event = $this->makeEvent('http://example.com/admin/parts?q=test');
|
||||
|
||||
$subscriber->onKernelRequest($event);
|
||||
|
||||
$this->assertTrue($event->hasResponse());
|
||||
$this->assertStringContainsString('/admin/parts', $event->getResponse()->getTargetUrl());
|
||||
$this->assertStringContainsString('q=test', $event->getResponse()->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testSubscriberListensToKernelRequestEvent(): void
|
||||
{
|
||||
$events = RedirectToHttpsSubscriber::getSubscribedEvents();
|
||||
$this->assertArrayHasKey('kernel.request', $events);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue