diff --git a/.changeset/clear-items-send.md b/.changeset/clear-items-send.md new file mode 100644 index 00000000000..4d2792ad571 --- /dev/null +++ b/.changeset/clear-items-send.md @@ -0,0 +1,5 @@ +--- +"@effect/platform": patch +--- + +`HttpRouter.mountApp` prefix matching fixed diff --git a/packages/platform-node/test/HttpServer.test.ts b/packages/platform-node/test/HttpServer.test.ts index 5ae3117b219..59801f77d51 100644 --- a/packages/platform-node/test/HttpServer.test.ts +++ b/packages/platform-node/test/HttpServer.test.ts @@ -190,6 +190,14 @@ describe("HttpServer", () => { expect(todo).toEqual("/1") const root = yield* client.get("/child").pipe(Effect.flatMap((_) => _.text)) expect(root).toEqual("/") + const rootSearch = yield* client.get("/child?foo=bar").pipe(Effect.flatMap((_) => _.text)) + expect(rootSearch).toEqual("?foo=bar") + const rootSlash = yield* client.get("/child/").pipe(Effect.flatMap((_) => _.text)) + expect(rootSlash).toEqual("/") + const invalid = yield* client.get("/child1/", { + urlParams: { foo: "bar" } + }).pipe(Effect.map((_) => _.status)) + expect(invalid).toEqual(404) }).pipe(Effect.provide(NodeHttpServer.layerTest))) it.scoped("mountApp/includePrefix", () => diff --git a/packages/platform/src/internal/httpRouter.ts b/packages/platform/src/internal/httpRouter.ts index e51148eddca..a8ba4bd281b 100644 --- a/packages/platform/src/internal/httpRouter.ts +++ b/packages/platform/src/internal/httpRouter.ts @@ -243,9 +243,12 @@ const toHttpApp = ( const context = Context.unsafeMake(new Map(fiber.getFiberRef(FiberRef.currentContext).unsafeMap)) const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest) if (mountsLen > 0) { + const searchIndex = request.url.indexOf("?") + const pathname = searchIndex === -1 ? request.url : request.url.slice(0, searchIndex) + for (let i = 0; i < mountsLen; i++) { const [path, routeContext, options] = mounts[i] - if (request.url.startsWith(path)) { + if (pathname === path || pathname.startsWith(path + "/")) { context.unsafeMap.set(RouteContext.key, routeContext) if (options?.includePrefix !== true) { context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, path))