diff options
author | 2022-11-22 04:00:55 -0800 | |
---|---|---|
committer | 2022-11-22 04:00:55 -0800 | |
commit | 2b4dcc8c817e2597f2de079c27302f9970fbebb6 (patch) | |
tree | 1259946f391e7624203c20936e2156a72c20af75 /test/bun.js/filesystem_router.test.ts | |
parent | 15a5aa1a682c81cb017795e9489dbb279e7b6717 (diff) | |
download | bun-2b4dcc8c817e2597f2de079c27302f9970fbebb6.tar.gz bun-2b4dcc8c817e2597f2de079c27302f9970fbebb6.tar.zst bun-2b4dcc8c817e2597f2de079c27302f9970fbebb6.zip |
[FileSystemRouter] base_dir, Request, Response
Diffstat (limited to 'test/bun.js/filesystem_router.test.ts')
-rw-r--r-- | test/bun.js/filesystem_router.test.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/bun.js/filesystem_router.test.ts b/test/bun.js/filesystem_router.test.ts index 9aba52a30..8e3e34d9a 100644 --- a/test/bun.js/filesystem_router.test.ts +++ b/test/bun.js/filesystem_router.test.ts @@ -109,3 +109,71 @@ it("should support dynamic routes", () => { expect(name).toBe("/posts/[id]"); expect(filePath).toBe(`${tempdir}fs-router-test-02/posts/[id].tsx`); }); + +it("should support Request & Response", async () => { + // set up the test + const tempdir = realpathSync(tmpdir()) + "/"; + rmSync(tempdir + "fs-router-test-03", { recursive: true, force: true }); + createTree(tempdir + "fs-router-test-03", [ + "index.tsx", + "posts/[id].tsx", + "posts.tsx", + ]); + + const router = new Bun.FileSystemRouter({ + dir: tempdir + "fs-router-test-03/", + style: "nextjs", + }); + + for (let current of [ + // Reuqest + new Request({ url: "/posts/hello-world" }), + new Request({ url: "http://example.com/posts/hello-world" }), + // Response + // when there's http:// it will be parsed as a URL and only the pathname is used + await fetch("http://example.com/posts/hello-world"), + ]) { + const { + name, + params: { id }, + filePath, + } = router.match(current); + expect(id).toBe("hello-world"); + expect(name).toBe("/posts/[id]"); + expect(filePath).toBe(`${tempdir}fs-router-test-03/posts/[id].tsx`); + } +}); + +it("assetPrefix, src, and origin", async () => { + // set up the test + const tempdir = realpathSync(tmpdir()) + "/"; + rmSync(tempdir + "fs-router-test-04", { recursive: true, force: true }); + createTree(tempdir + "fs-router-test-04", [ + "index.tsx", + "posts/[id].tsx", + "posts.tsx", + ]); + + const router = new Bun.FileSystemRouter({ + dir: tempdir + "fs-router-test-04/", + style: "nextjs", + assetPrefix: "/_next/static/", + origin: "https://nextjs.org", + }); + + for (let current of [ + // Reuqest + new Request({ url: "/posts/hello-world" }), + new Request({ url: "https://nextjs.org/posts/hello-world" }), + ]) { + const { + name, + params: { id }, + src, + filePath, + } = router.match(current); + expect(name).toBe("/posts/[id]"); + expect(src).toBe("https://nextjs.org/_next/static/posts/[id].tsx"); + expect(filePath).toBe(`${tempdir}fs-router-test-04/posts/[id].tsx`); + } +}); |