This commit is contained in:
Marcel Pociot
2020-04-30 00:12:54 +02:00
parent b515a55325
commit 22613835e4
3 changed files with 83 additions and 7 deletions

View File

@@ -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() {

View File

@@ -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",

View File

@@ -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();
}
}