You've already forked Traefik-Redis-Api
113 lines
4.3 KiB
PHP
113 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\Tests\Traefik;
|
|
|
|
use Mockery;
|
|
use Predis\Command\FactoryInterface;
|
|
use Siteworxpro\App\Services\Facades\Redis;
|
|
use Siteworxpro\App\Traefik\ProtocolEnum;
|
|
use Siteworxpro\App\Traefik\RedisClient;
|
|
use Siteworxpro\Tests\Unit;
|
|
|
|
class MiddlewareServiceTest extends Unit
|
|
{
|
|
public function testGerMiddlewareHttpAll(): void
|
|
{
|
|
$this->getAllMiddleware(ProtocolEnum::HTTP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testGetMiddlewareTcpAll(): void
|
|
{
|
|
$this->getAllMiddleware(ProtocolEnum::TCP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
private function getAllMiddleware(ProtocolEnum $protocolEnum): void
|
|
{
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocolEnum->getValue() . '/middlewares/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/Host',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Host',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Port',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Proto',
|
|
]]);
|
|
|
|
$services = new RedisClient()->getAllMiddlewares($protocolEnum);
|
|
$this->assertCount(1, $services);
|
|
$this->assertSame('foo', $services[0]);
|
|
}
|
|
|
|
|
|
public function testGetMiddlewareHttp()
|
|
{
|
|
$this->getIndMiddleware(ProtocolEnum::HTTP);
|
|
}
|
|
|
|
private function getIndMiddleware(ProtocolEnum $protocolEnum): void
|
|
{
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocolEnum->getValue() . '/middlewares/foo/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocolEnum->getValue() . '/middlewares/foo/headers/customrequestheaders/Host',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Host',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Port',
|
|
'traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Proto',
|
|
]]);
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/Host')
|
|
->andReturn('foo.localhost');
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Host')
|
|
->andReturn('foo.localhost');
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Port')
|
|
->andReturn('80');
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() .
|
|
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Proto')
|
|
->andReturn('http');
|
|
|
|
$middleware = new RedisClient()->getMiddleware('foo', $protocolEnum);
|
|
|
|
$this->assertCount(4, $middleware['headers']['customrequestheaders']);
|
|
$this->assertSame('foo.localhost', $middleware['headers']['customrequestheaders']['Host']);
|
|
$this->assertSame('foo.localhost', $middleware['headers']['customrequestheaders']['X-Forwarded-Host']);
|
|
$this->assertSame('80', $middleware['headers']['customrequestheaders']['X-Forwarded-Port']);
|
|
$this->assertSame('http', $middleware['headers']['customrequestheaders']['X-Forwarded-Proto']);
|
|
}
|
|
}
|