diff --git a/src/routes.ts b/src/routes.ts index 2c351d6..1b64d16 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -107,11 +107,10 @@ export function ProcessCallback( ); } const meta = GetMetadata(controller, ControllerMeta); - const fullLocation = - `${controller.location}/${location ?? handlerSegment(controller.name, key)}`.replace( - /\/+/g, - "/", - ); + const segment = location ?? handlerSegment(controller.name, key); + const joined = + segment === "" ? controller.location : `${controller.location}/${segment}`; + const fullLocation = (joined || "/").replace(/\/+/g, "/"); return { id: RegisterRoute({ diff --git a/src/tests/route-decorators.test.ts b/src/tests/route-decorators.test.ts index 01c40b9..1de459a 100644 --- a/src/tests/route-decorators.test.ts +++ b/src/tests/route-decorators.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert"; -import { Controller, Get } from "../index"; +import { Controller, Get, getRegisteredRoutes } from "../index"; function decorateAccessor() { class AccessorController extends Controller("/accessor") { @@ -44,7 +44,43 @@ function decorateSymbolKeyWithLocation() { return SymbolLocationController; } +function registeredLocations(callbackName: string): string[] { + return getRegisteredRoutes() + .filter((route) => route.callbackName === callbackName) + .map((route) => route.location); +} + describe("Route decorators", () => { + it("register an empty location at the controller root, without a trailing slash", () => { + class EmptyLocationController extends Controller("/empty-location") { + @Get("") + emptyLocationRoot() { + return "ok"; + } + } + assert.deepStrictEqual( + registeredLocations( + EmptyLocationController.prototype.emptyLocationRoot.name, + ), + ["/empty-location"], + ); + }); + + it("register an empty location under the root controller at /", () => { + class SlashRootController extends Controller("/") { + @Get("") + slashControllerRoot() { + return "ok"; + } + } + assert.deepStrictEqual( + registeredLocations( + SlashRootController.prototype.slashControllerRoot.name, + ), + ["/"], + ); + }); + it("refuse an accessor, which has no method to register", () => { assert.throws(decorateAccessor, { name: "TypeError",