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

Ensure trailing slashes do not result in 404 #70

Merged
merged 1 commit into from
Feb 17, 2024
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
5 changes: 3 additions & 2 deletions src/Web/Router/FastRouteRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public function addRoute(
$controller = new MiddlewareController($controller, ...$middlewares);
}
$route = new Route($requestMapping, $controller);
$path = $requestMapping->getPath();
$this->routes[] = $route;
$this->collector->addRoute(
$requestMapping->getHttpMethod()->value,
$requestMapping->getPath(),
$path === '/' ? $path : rtrim($requestMapping->getPath(), '/'),
$route
);

Expand All @@ -62,7 +63,7 @@ public function addRoute(
*/
public function match(Request $request) : RoutingResolution {
$uri = $request->getUri();
$path = empty($uri->getPath()) ? '/' : $uri->getPath();
$path = empty($uri->getPath()) ? '/' : rtrim($uri->getPath(), '/');
$routeData = $this->getDispatcher()->dispatch($request->getMethod(), $path);
$status = (int) array_shift($routeData);

Expand Down
41 changes: 41 additions & 0 deletions test/Unit/Web/Router/FastRouteRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Labrador\Test\Unit\Web\Router;

use Amp\Http\HttpStatus;
use Amp\Http\Server\Driver\Client;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
Expand Down Expand Up @@ -253,4 +254,44 @@ public function testAddingMiddlewareToSingleRoute(RequestMapping $requestMapping

$this->assertSame('decorated value: foobar', $body);
}

public function testTrailingSlashWithMatchedPathDoesNotResultIn404() : void {
$request = $this->getRequest(HttpMethod::Get->value, 'http://example.com/found-controller/');
$router = $this->getRouter();
$responseController = new ResponseControllerStub(new Response(200, [], 'found controller with trailing slash'));
$router->addRoute(
new GetMapping('/found-controller'),
$responseController,
);

$controller = $router->match($request)->controller;

self::assertNotNull($controller);

$response = $controller->handleRequest($request);


self::assertSame(HttpStatus::OK, $response->getStatus());
self::assertSame('found controller with trailing slash', $response->getBody()->read());
}

public function testTrailingSlashInAddedRouteDoesNotResultIn404() : void {
$request = $this->getRequest(HttpMethod::Get->value, 'http://example.com/found-controller');
$router = $this->getRouter();
$responseController = new ResponseControllerStub(new Response(200, [], 'found controller with trailing slash'));
$router->addRoute(
new GetMapping('/found-controller/'),
$responseController,
);

$controller = $router->match($request)->controller;

self::assertNotNull($controller);

$response = $controller->handleRequest($request);


self::assertSame(HttpStatus::OK, $response->getStatus());
self::assertSame('found controller with trailing slash', $response->getBody()->read());
}
}
Loading