From 22613835e4cb8bdc917507ce0f7c18dd6fbe41d3 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 30 Apr 2020 00:12:54 +0200 Subject: [PATCH] wip --- app/Server/Factory.php | 12 +++++- composer.json | 11 +++-- tests/Feature/Server/AdminTest.php | 67 +++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/app/Server/Factory.php b/app/Server/Factory.php index 051ad03..3ad800e 100644 --- a/app/Server/Factory.php +++ b/app/Server/Factory.php @@ -49,6 +49,9 @@ class Factory /** @var RouteGenerator */ protected $router; + /** @var Server */ + protected $socket; + public function __construct() { $this->loop = LoopFactory::create(); @@ -145,7 +148,7 @@ class Factory public function createServer() { - $socket = new Server("{$this->host}:{$this->port}", $this->loop); + $this->socket = new Server("{$this->host}:{$this->port}", $this->loop); $this->bindConfiguration() ->bindSubdomainGenerator() @@ -164,13 +167,18 @@ class Factory $http = new HttpServer($router); - $server = new IoServer($http, $socket, $this->loop); + $server = new IoServer($http, $this->socket, $this->loop); $controlConnection->enableKeepAlive($this->loop); return $server; } + public function getSocket(): Server + { + return $this->socket; + } + protected function bindDatabase() { app()->singleton(DatabaseInterface::class, function() { diff --git a/composer.json b/composer.json index e1877b6..b2873cd 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,14 @@ }, "authors": [ { - "name": "Nuno Maduro", - "email": "enunomaduro@gmail.com" + "name": "Marcel Pociot", + "email": "marcel@beyondco.de" + } + ], + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/pestphp/pest" } ], "require": { @@ -38,7 +44,6 @@ "ratchet/pawl": "^0.3.4", "react/socket": "^1.4", "riverline/multipart-parser": "^2.0", - "seregazhuk/react-promise-testing": "^0.4.0", "symfony/expression-language": "^5.0", "symfony/http-kernel": "^4.0|^5.0", "symfony/psr-http-message-bridge": "^2.0", diff --git a/tests/Feature/Server/AdminTest.php b/tests/Feature/Server/AdminTest.php index 6a82edc..2238aaf 100644 --- a/tests/Feature/Server/AdminTest.php +++ b/tests/Feature/Server/AdminTest.php @@ -2,7 +2,70 @@ namespace Tests\Feature\Server; -class AdminTest -{ +use App\Http\Server; +use App\Server\Factory; +use Clue\React\Buzz\Browser; +use Clue\React\Buzz\Message\ResponseException; +use Psr\Http\Message\ResponseInterface; +use React\Socket\Connector; +use Tests\Feature\TestCase; +class AdminTest extends TestCase +{ + /** @var Browser */ + protected $browser; + + /** @var Factory */ + protected $serverFactory; + + public function setUp(): void + { + parent::setUp(); + + $this->browser = new Browser($this->loop); + + $this->startServer(); + } + + public function tearDown(): void + { + $this->serverFactory->getSocket()->close(); + + parent::tearDown(); + } + + /** @test */ + public function it_is_protected_using_basic_authentication() + { + $this->expectException(ResponseException::class); + $this->expectExceptionMessage(401); + + /** @var ResponseInterface $response */ + $this->await($this->browser->get('http://127.0.0.1:8080', [ + 'Host' => 'expose.localhost' + ])); + } + + /** @test */ + public function it_accepts_valid_credentials() + { + $this->app['config']['expose.admin.users'] = [ + 'username' => 'secret', + ]; + + /** @var ResponseInterface $response */ + $response = $this->await($this->browser->get('http://127.0.0.1:8080', [ + 'Host' => 'expose.localhost', + 'Authorization' => base64_encode("username:secret"), + ])); + $this->assertSame(200, $response->getStatusCode()); + } + + protected function startServer() + { + $this->serverFactory = new Factory(); + + $this->serverFactory->setLoop($this->loop) + ->createServer(); + } }