This commit is contained in:
Marcel Pociot
2020-04-30 15:17:25 +02:00
parent 0dfda0825c
commit a972c8581c
24 changed files with 444 additions and 29 deletions

View File

@@ -2,11 +2,15 @@
namespace Tests\Feature\Server;
use App\Contracts\ConnectionManager;
use App\Http\Server;
use App\Server\Factory;
use Clue\React\Buzz\Browser;
use Clue\React\Buzz\Message\ResponseException;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Ratchet\Server\IoConnection;
use React\Socket\Connector;
use Tests\Feature\TestCase;
@@ -49,10 +53,6 @@ class AdminTest extends TestCase
/** @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',
@@ -61,8 +61,67 @@ class AdminTest extends TestCase
$this->assertSame(200, $response->getStatusCode());
}
/** @test */
public function it_allows_saving_settings()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = false;
/** @var ResponseInterface $response */
$this->await($this->browser->post('http://127.0.0.1:8080/settings', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode("username:secret"),
'Content-Type' => 'application/json'
], json_encode([
'validate_auth_tokens' => true,
])));
$this->assertTrue(config('expose.admin.validate_auth_tokens'));
}
/** @test */
public function it_can_create_users()
{
$this->await($this->browser->post('http://127.0.0.1:8080/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode("username:secret"),
'Content-Type' => 'application/json'
], json_encode([
'name' => 'Marcel',
])));
$this->assertDatabaseHasResults('SELECT * FROM users WHERE name = "Marcel"');
}
/** @test */
public function it_can_list_all_currently_connected_sites()
{
/** @var ConnectionManager $connectionManager */
$connectionManager = app(ConnectionManager::class);
$connection = \Mockery::mock(IoConnection::class);
$connectionManager->storeConnection('some-host.text', 'fixed-subdomain', $connection);
/** @var Response $response */
$response = $this->await($this->browser->get('http://127.0.0.1:8080/sites', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode("username:secret"),
'Content-Type' => 'application/json'
]));
$body = $response->getBody()->getContents();
$this->assertTrue(Str::contains($body, 'some-host.text'));
$this->assertTrue(Str::contains($body, 'fixed-subdomain'));
}
protected function startServer()
{
$this->app['config']['expose.admin.database'] = ':memory:';
$this->app['config']['expose.admin.users'] = [
'username' => 'secret',
];
$this->serverFactory = new Factory();
$this->serverFactory->setLoop($this->loop)

View File

@@ -0,0 +1,103 @@
<?php
namespace Tests\Feature\Server;
use App\Client\Client;
use App\Contracts\ConnectionManager;
use App\Server\Factory;
use Clue\React\Buzz\Browser;
use Clue\React\Buzz\Message\ResponseException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ServerRequestInterface;
use Ratchet\Client\WebSocket;
use Ratchet\ConnectionInterface;
use React\EventLoop\LoopInterface;
use React\Http\Server;
use Tests\Feature\TestCase;
use function Ratchet\Client\connect;
class TunnelTest extends TestCase
{
/** @var Browser */
protected $browser;
/** @var Factory */
protected $serverFactory;
public function setUp(): void
{
parent::setUp();
$this->browser = new Browser($this->loop);
$this->startServer();
}
/** @test */
public function it_returns_404_for_non_existing_clients()
{
$this->expectException(ResponseException::class);
$this->expectExceptionMessage(404);
$response = $this->await($this->browser->get('http://127.0.0.1:8080/', [
'Host' => 'tunnel.localhost'
]));
}
/** @test */
public function it_sends_incoming_requests_to_the_connected_client()
{
$this->createTestHttpServer();
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server
*/
$client = $this->createClient();
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel'));
/**
* Once the client is connected, we perform a GET request on the
* created tunnel.
*/
$response = $this->await($this->browser->get('http://127.0.0.1:8080/', [
'Host' => 'tunnel.localhost'
]));
$this->assertSame('Hello World!', $response->getBody()->getContents());
}
protected function startServer()
{
$this->app['config']['expose.admin.database'] = ':memory:';
$this->serverFactory = new Factory();
$this->serverFactory->setLoop($this->loop)
->setHost('127.0.0.1')
->setHostname('localhost')
->createServer();
}
protected function createClient()
{
(new \App\Client\Factory())
->setLoop($this->loop)
->setHost('127.0.0.1')
->setPort(8080)
->createClient();
return app(Client::class);
}
protected function createTestHttpServer()
{
$server = new Server(function (ServerRequestInterface $request) {
return new Response(200, ['Content-Type' => 'text/plain'], "Hello World!");
});
$socket = new \React\Socket\Server(8085, $this->loop);
$server->listen($socket);
}
}

View File

@@ -2,6 +2,10 @@
namespace Tests\Feature;
use Clue\React\SQLite\DatabaseInterface;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
use React\EventLoop\StreamSelectLoop;
@@ -33,4 +37,13 @@ abstract class TestCase extends \Tests\TestCase
{
return await($promise, $loop ?? $this->loop, $timeout ?? static::AWAIT_TIMEOUT);
}
protected function assertDatabaseHasResults($query)
{
$database = app(DatabaseInterface::class);
$result = $this->await($database->query($query));
$this->assertGreaterThanOrEqual(1, count($result->rows));
}
}

View File

@@ -2,6 +2,7 @@
namespace Tests;
use Clue\React\SQLite\DatabaseInterface;
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase