Skip to content

Commit

Permalink
Add a unique token to route names (#210)
Browse files Browse the repository at this point in the history
* Adds unique key to route name

* Test mulitple routes for a configuration are unique
  • Loading branch information
ryanlholt authored Apr 24, 2024
1 parent ca27d95 commit 6ed76b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function packageBooted()
throw InvalidMethod::make($method);
}

return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')->name("webhook-client-{$name}");
return Route::{$method}($url, '\Spatie\WebhookClient\Http\Controllers\WebhookController')
->name("webhook-client-{$name}." . Str::random(8));
});

$this->app->scoped(WebhookConfigRepository::class, function () {
Expand All @@ -43,7 +44,9 @@ public function packageBooted()
$this->app->bind(WebhookConfig::class, function () {
$routeName = Route::currentRouteName() ?? '';

$configName = Str::after($routeName, 'webhook-client-');
$routeNameSuffix = Str::after($routeName, 'webhook-client-');

$configName = Str::before($routeNameSuffix, '.');

$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);

Expand Down
15 changes: 15 additions & 0 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ public function it_can_store_none_of_the_headers()
$this->assertEquals(0, count(WebhookCall::first()->headers));
}

/** @test */
public function multiple_routes_can_share_configuration()
{
Route::webhooks('incoming-webhooks-additional');

$this->refreshWebhookConfigRepository();

$routeNames = collect(Route::getRoutes())
->map(fn ($route) => $route->getName());

$uniqueRouteNames = $routeNames->unique();

$this->assertEquals($routeNames->count(), $uniqueRouteNames->count());
}

protected function determineSignature(array $payload): string
{
$secret = config('webhook-client.configs.0.signing_secret');
Expand Down

0 comments on commit 6ed76b0

Please sign in to comment.