Skip to content
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
20 changes: 16 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public function allowedMethodsForPath(string $path): array
* A URI is redirectable if it can be handled by a GET route within the
* router. The URI can either be a URL containing the application domain,
* or an absolute path.
*
* The ?query and #fragment parts, if any, are ignored.
*/
public function isRedirectable(string $uri): bool
{
Expand All @@ -196,12 +198,22 @@ public function isRedirectable(string $uri): bool
$uri = substr($uri, strlen($base_url));
}

if (str_starts_with($uri, '/')) {
$allowed_methods = $this->allowedMethodsForPath($uri);
return in_array('GET', $allowed_methods);
} else {
if (!str_starts_with($uri, '/')) {
return false;
}

$position_hash = strpos($uri, '#');
if ($position_hash !== false) {
$uri = substr($uri, 0, $position_hash);
}

$position_query = strpos($uri, '?');
if ($position_query !== false) {
$uri = substr($uri, 0, $position_query);
}

$allowed_methods = $this->allowedMethodsForPath($uri);
return in_array('GET', $allowed_methods);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ public function testIsRedirectableWithNotSupportedUrl(): void
$this->assertFalse($is_redirectable);
}

public function testIsRedirectableWithQueryPart(): void
{
$router = new Router();
$router->addRoute('GET', '/rabbits/new', 'rabbits#new');

$is_redirectable = $router->isRedirectable('/rabbits/new?foo=bar');

$this->assertTrue($is_redirectable);
}

public function testIsRedirectableWithFragmentPart(): void
{
$router = new Router();
$router->addRoute('GET', '/rabbits/new', 'rabbits#new');

$is_redirectable = $router->isRedirectable('/rabbits/new#foo');

$this->assertTrue($is_redirectable);
}

public function testUriByName(): void
{
$router = new Router();
Expand Down