Skip to content

Commit ed51444

Browse files
committed
Allow variable domain to use host as a fallback
1 parent f53435d commit ed51444

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Storage/DatabaseRequestRepository.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ protected function routeResult(?array $route, ?array $responses)
108108
$route['description'],
109109
$route['content'],
110110
[
111-
'domain' => $route['domain'],
111+
'domain' => $this->hasVariableFragment($route['domain'])
112+
? request()->getHost()
113+
: $route['domain'],
112114
'methods' => $route['methods'],
113115
'uri' => $route['uri'],
114116
'name' => $route['name'],
@@ -144,4 +146,17 @@ protected function table($table)
144146
{
145147
return DB::connection($this->connection)->table($table);
146148
}
149+
150+
/**
151+
* Determines if a given domain has any dynamic fragment.
152+
*
153+
* @param string|null $domain
154+
* @return bool
155+
*/
156+
private function hasVariableFragment(?string $domain)
157+
{
158+
return collect(explode('.', $domain))->filter(function ($fragment) {
159+
return Str::startsWith($fragment, '{') && Str::endsWith($fragment,'}');
160+
})->count() > 0;
161+
}
147162
}

tests/Http/RoutesRequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Davidhsianturi\Compass\Storage\RouteModel;
88
use Illuminate\Foundation\Testing\RefreshDatabase;
99
use Davidhsianturi\Compass\Storage\DatabaseRequestRepository;
10+
use Illuminate\Support\Facades\URL;
1011

1112
class RoutesRequestTest extends TestCase
1213
{
@@ -54,6 +55,19 @@ public function test_show_route_request_by_id()
5455
->assertExactJson($route);
5556
}
5657

58+
public function test_show_route_request_fallbacks_to_host_for_variable_domain()
59+
{
60+
$host = 'test.tld.test';
61+
URL::forceRootUrl('http://' . $host);
62+
$this->registerVariableRoute();
63+
64+
$route = $this->repository->get()->first()->jsonSerialize();
65+
66+
$this->getJson(route('compass.request.show', $route['id']))
67+
->assertSuccessful()
68+
->assertJsonFragment(['domain' => $host]);
69+
}
70+
5771
public function test_store_the_route_request_to_storage()
5872
{
5973
$this->registerAppRoutes();

tests/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,15 @@ protected function registerAppRoutes()
9393
});
9494
});
9595
}
96+
97+
protected function registerVariableRoute()
98+
{
99+
RouteFacade::prefix('api/v1')->group(function () {
100+
RouteFacade::group(['domain' => '{account}.tld.test'], function () {
101+
RouteFacade::get('/test', function () {
102+
return 'test';
103+
})->name('vardomain');
104+
});
105+
});
106+
}
96107
}

0 commit comments

Comments
 (0)