-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make router implement handler for Deno.serve
- Loading branch information
Showing
5 changed files
with
73 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type { HttpMethod, HttpRouteHandler } from "./src/router.ts"; | ||
export { HttpRouter } from "./src/router.ts"; | ||
export type { HttpRouter, HttpMethod, HttpRouteHandler } from "./src/router.ts"; | ||
export { createRouter } from "./src/router.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
} from "https://deno.land/[email protected]/assert/mod.ts"; | ||
import { assertEquals, assertRejects } from "https://deno.land/[email protected]/assert/mod.ts"; | ||
|
||
import { HttpRouter } from "./router.ts"; | ||
import { createRouter } from "./router.ts"; | ||
|
||
const baseurl = "http://example.com"; | ||
|
||
|
@@ -12,46 +9,44 @@ function createRequest(path: string) { | |
} | ||
|
||
Deno.test("get random json body", async () => { | ||
const router = new HttpRouter(); | ||
const router = createRouter(); | ||
const body = { message: crypto.randomUUID() }; | ||
router.get("/", () => Response.json(body)); | ||
|
||
const response = await router.handleRequest( | ||
new Request(new URL("/", baseurl)), | ||
); | ||
const response = await router(new Request(new URL("/", baseurl))); | ||
|
||
assertEquals(await response.json(), body); | ||
}); | ||
|
||
Deno.test("match path parameter", async () => { | ||
const router = new HttpRouter(); | ||
const router = createRouter(); | ||
|
||
router.get("/", () => new Response("not found")); | ||
router.get("/:id", (_, match) => new Response(match.pathname.groups.id)); | ||
|
||
const id = crypto.randomUUID(); | ||
const response1 = await router.handleRequest(createRequest("/")); | ||
const response2 = await router.handleRequest(createRequest("/" + id)); | ||
const response1 = await router(createRequest("/")); | ||
const response2 = await router(createRequest("/" + id)); | ||
|
||
assertEquals(await response1.text(), "not found"); | ||
assertEquals(await response2.text(), id); | ||
}); | ||
|
||
Deno.test("rejects if no match", async () => { | ||
const router = new HttpRouter(); | ||
const router = createRouter(); | ||
|
||
router.get("/", () => new Response("ok")); | ||
|
||
await assertRejects(() => router.handleRequest(createRequest("/abc"))); | ||
await assertRejects(() => router(createRequest("/abc"))); | ||
}); | ||
|
||
Deno.test("can use fallback if no match", async () => { | ||
const router = new HttpRouter(); | ||
const router = createRouter(); | ||
|
||
router.get("/", () => new Response("ok")); | ||
router.get("/abcd", () => new Response("ok")); | ||
router.all("*", () => new Response("no match")); | ||
|
||
const response = await router.handleRequest(createRequest("/abc")); | ||
const response = await router(createRequest("/abc")); | ||
assertEquals(await response.text(), "no match"); | ||
}); |