Repository: pmcelhaney/counterfact (ts)

Issue:
Trailing slashes on an endpoint?
### Discussed in https://github.com/pmcelhaney/counterfact/discussions/702

<div type='discussions-op-text'>

<sup>Originally posted by **dethell-jh** January  5, 2024</sup>
Some of our codebase has endpoint references written with a trailing slash such as: `${myApiUrl}/someEndPoint/`. This throws off Counterfact because even though I have a file at `paths/someEndPoint.ts` that handles the need, Counterfact is expecting a subpath after the trailing slash. If I update the codebase to remove the trailing slash it works, but I'm trying to get my Counterfact setup to be a drop-in for the API that doesn't require the UX side to make changes just to accommodate Counterfact. Ideas?</div>

Pull request description:
fix(server): Trailing slashes on an endpoint #703
fix #703

Patch:
diff --git a/.changeset/selfish-cameras-wave.md b/.changeset/selfish-cameras-wave.md
new file mode 100644
index 0000000..37db5d5
--- /dev/null
+++ b/.changeset/selfish-cameras-wave.md
@@ -0,0 +1,5 @@
+---
+"counterfact": patch
+---
+
+Fix issue 703: Endpoint with trailing slash
diff --git a/src/server/module-tree.ts b/src/server/module-tree.ts
index 5ddb24f..7e1c218 100644
--- a/src/server/module-tree.ts
+++ b/src/server/module-tree.ts
@@ -168,7 +168,10 @@ export class ModuleTree {
       );
     }
 
-    if (remainingSegments.length === 0) {
+    if (
+      remainingSegments.length === 0 ||
+      (remainingSegments.length === 1 && remainingSegments[0] === "")
+    ) {
       return this.buildMatch(directory, segment, pathVariables, matchedPath);
     }
 


Test patch:
diff --git a/test/server/module-tree.test.ts b/test/server/module-tree.test.ts
index c644ffe..5295f0e 100644
--- a/test/server/module-tree.test.ts
+++ b/test/server/module-tree.test.ts
@@ -33,6 +33,12 @@ it("finds a file under a subdirectory", () => {
   expect(match(moduleTree, "/a/b")).toBe("b");
 });
 
+it("finds a file with a trailing slash in the path", () => {
+  const moduleTree = new ModuleTree();
+  add(moduleTree, "/a", "a");
+  expect(match(moduleTree, "/a/")).toBe("a");
+});
+
 it("finds a file with a wildcard match", () => {
   const moduleTree = new ModuleTree();
   add(moduleTree, "/a", "a");