Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API endpoint #363

Merged
merged 3 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Client\Http\Controllers\ClearLogsController;
use App\Client\Http\Controllers\CreateTunnelController;
use App\Client\Http\Controllers\DashboardController;
use App\Client\Http\Controllers\GetTunnelsController;
use App\Client\Http\Controllers\LogController;
use App\Client\Http\Controllers\PushLogsToDashboardController;
use App\Client\Http\Controllers\ReplayLogController;
Expand Down Expand Up @@ -143,6 +144,7 @@ protected function addRoutes()
{
$this->router->get('/', DashboardController::class);

$this->router->get('/api/tunnels', GetTunnelsController::class);
$this->router->post('/api/tunnel', CreateTunnelController::class);
$this->router->get('/api/logs', LogController::class);
$this->router->post('/api/logs', PushLogsToDashboardController::class);
Expand Down
18 changes: 18 additions & 0 deletions app/Client/Http/Controllers/GetTunnelsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Client\Http\Controllers;

use App\Client\Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;

class GetTunnelsController extends Controller
{
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(respond_json([
'tunnels' => Client::$subdomains,
], 200));
}
}
79 changes: 79 additions & 0 deletions tests/Feature/Client/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Tests\Feature\Client;

use App\Client\Client;
use App\Client\Configuration;
use App\Client\Factory;
use App\Logger\RequestLogger;
use Psr\Http\Message\ResponseInterface;
use React\Http\Browser;
use Tests\Feature\TestCase;

class ApiTest extends TestCase
{
/** @var Browser */
protected $browser;

/** @var Factory */
protected $dashboardFactory;

/** @var RequestLogger */
protected $requestLogger;

public function setUp(): void
{
parent::setUp();

$this->browser = new Browser($this->loop);
$this->requestLogger = $this->app->make(RequestLogger::class);
}

public function tearDown(): void
{
parent::tearDown();

$this->dashboardFactory->getApp()->close();
}

/** @test */
public function accessing_the_available_tunnels_works()
{
$this->startDashboard();

/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://127.0.0.1:4040/api/tunnels'));

$this->assertSame(200, $response->getStatusCode());
}

/** @test */
public function it_returns_the_connected_subdomain_urls()
{
Client::$subdomains = [
'https://test.eu-1.sharedwithexpose.com',
];

$this->startDashboard();

/** @var ResponseInterface $response */
$response = $this->await($this->browser->get('http://127.0.0.1:4040/api/tunnels'));

$json = json_decode($response->getBody()->getContents());

$this->assertIsArray($json->tunnels);
$this->assertCount(1, $json->tunnels);
$this->assertSame('https://test.eu-1.sharedwithexpose.com', $json->tunnels[0]);
}

protected function startDashboard()
{
app()->singleton(Configuration::class, function ($app) {
return new Configuration('localhost', '8080', false);
});

$this->dashboardFactory = (new Factory())
->setLoop($this->loop)
->createHttpServer();
}
}