Skip to content

Commit

Permalink
Merge pull request #162 from tanmaymishu/feature/dynamic-domain-resol…
Browse files Browse the repository at this point in the history
…ution

Allow host as a fallback domain when using variable domain
  • Loading branch information
davidhsianturi authored Jun 13, 2021
2 parents 8f3dee1 + 10e84db commit fc027d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Storage/DatabaseRequestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ protected function routeResult(?array $route, ?array $responses)
$route['description'],
$route['content'],
[
'domain' => $route['domain'],
'domain' => $this->hasVariableFragment($route['domain'])
? request()->getHost()
: $route['domain'],
'methods' => $route['methods'],
'uri' => $route['uri'],
'name' => $route['name'],
Expand Down Expand Up @@ -144,4 +146,17 @@ protected function table($table)
{
return DB::connection($this->connection)->table($table);
}

/**
* Determines if a given domain has any dynamic fragment.
*
* @param string|null $domain
* @return bool
*/
private function hasVariableFragment(?string $domain)
{
return collect(explode('.', $domain))->filter(function ($fragment) {
return Str::startsWith($fragment, '{') && Str::endsWith($fragment, '}');
})->count() > 0;
}
}
14 changes: 14 additions & 0 deletions tests/Http/RoutesRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Davidhsianturi\Compass\Tests\Http;

use Davidhsianturi\Compass\Compass;
use Illuminate\Support\Facades\URL;
use Davidhsianturi\Compass\Tests\TestCase;
use Davidhsianturi\Compass\Storage\RouteModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -54,6 +55,19 @@ public function test_show_route_request_by_id()
->assertExactJson($route);
}

public function test_show_route_request_fallbacks_to_host_for_variable_domain()
{
$host = 'test.tld.test';
URL::forceRootUrl('http://'.$host);
$this->registerVariableRoute();

$route = $this->repository->get()->first()->jsonSerialize();

$this->getJson(route('compass.request.show', $route['id']))
->assertSuccessful()
->assertJsonFragment(['domain' => $host]);
}

public function test_store_the_route_request_to_storage()
{
$this->registerAppRoutes();
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ protected function registerAppRoutes()
});
});
}

protected function registerVariableRoute()
{
RouteFacade::prefix('api/v1')->group(function () {
RouteFacade::group(['domain' => '{account}.tld.test'], function () {
RouteFacade::get('/test', function () {
return 'test';
})->name('vardomain');
});
});
}
}

0 comments on commit fc027d1

Please sign in to comment.