koel/tests/Unit/Http/Middleware/ForceHttpsTest.php

65 lines
1.8 KiB
PHP
Raw Normal View History

2019-08-05 10:56:05 +00:00
<?php
namespace Tests\Unit\Http\Middleware;
use App\Http\Middleware\ForceHttps;
use Illuminate\Http\Request;
use Illuminate\Routing\UrlGenerator;
use Mockery;
2022-07-29 06:47:10 +00:00
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
2020-12-22 23:01:49 +00:00
use Symfony\Component\HttpFoundation\Response;
2019-08-05 10:56:05 +00:00
use Tests\TestCase;
class ForceHttpsTest extends TestCase
{
2022-07-29 06:47:10 +00:00
private LegacyMockInterface|UrlGenerator|MockInterface $url;
2021-06-05 10:47:56 +00:00
private ForceHttps $middleware;
2019-08-05 10:56:05 +00:00
public function setUp(): void
{
parent::setUp();
$this->url = Mockery::mock(UrlGenerator::class);
$this->middleware = new ForceHttps($this->url);
}
public function testHandle(): void
{
config(['koel.force_https' => true]);
$this->url->shouldReceive('forceScheme')->with('https');
$request = Mockery::mock(Request::class);
$request->shouldReceive('getClientIp')->andReturn('127.0.0.1');
$request->shouldReceive('setTrustedProxies')
2021-06-05 10:47:56 +00:00
->with(
['127.0.0.1'],
Request::HEADER_X_FORWARDED_FOR
| Request::HEADER_X_FORWARDED_HOST
| Request::HEADER_X_FORWARDED_PORT
| Request::HEADER_X_FORWARDED_PROTO
);
2019-08-05 10:56:05 +00:00
2020-12-22 23:01:49 +00:00
$response = Mockery::mock(Response::class);
2022-07-29 06:47:10 +00:00
$next = static fn () => $response;
2019-08-05 10:56:05 +00:00
2020-12-22 23:01:49 +00:00
self::assertSame($response, $this->middleware->handle($request, $next));
2019-08-05 10:56:05 +00:00
}
2019-08-05 11:05:10 +00:00
public function testNotHandle(): void
{
config(['koel.force_https' => false]);
$this->url->shouldReceive('forceScheme')->with('https')->never();
$request = Mockery::mock(Request::class);
$request->shouldReceive('setTrustedProxies')->never();
2020-12-22 23:01:49 +00:00
$response = Mockery::mock(Response::class);
2022-07-29 06:47:10 +00:00
$next = static fn () => $response;
2019-08-05 11:05:10 +00:00
2020-12-22 23:01:49 +00:00
self::assertSame($response, $this->middleware->handle($request, $next));
2019-08-05 11:05:10 +00:00
}
2019-08-05 10:56:05 +00:00
}