Skip to main content
Module

x/servest/router_test.ts

🌾A progressive http server for Deno🌾
Go to Latest
File
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.import { createRouter, findLongestAndNearestMatch } from "./router.ts";import { runIfMain, test } from "./vendor/https/deno.land/std/testing/mod.ts";import { defer } from "./promises.ts";import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
([ ["/foo", ["/foo", "/bar", "/f"], 0], ["/foo", ["/foo", "/foo/bar"], 0], ["/foo/bar", ["/", "/foo", "/hoo", "/hoo/foo/bar", "/foo/bar"], 4], ["/foo/bar/foo", ["/foo", "/foo/bar", "/bar/foo"], 1], ["/foo", ["/", "/hoo", "/hoo/foo"], 0], ["/deno/land", [/d(.+?)o/, /d(.+?)d/], 1], ["/foo", ["/", "/a/foo"], 0], ["/foo", [/\/foo/, /\/bar\/foo/], 0], ["/foo", [/\/a\/foo/, /\/foo/], 1]] as [string, (string | RegExp)[], number][]).forEach(([path, pat, idx]) => { test("findLongestAndNearestMatch:" + path, () => { assertEquals(findLongestAndNearestMatch(path, pat).index, idx); });});
(function() { const router = createRouter(); router.handle("/index", async req => { await req.respond({ status: 200, body: "ok" }); }); router.handle(new RegExp("/foo/(?<id>.+)"), async req => { const { id } = req.match.groups; await req.respond({ status: 200, headers: new Headers({ "content-type": "application/json" }), body: JSON.stringify({ id }) }); }); router.handle("/no-response", async req => {}); router.handle("/throw", async req => { throw new Error("throw"); }); let errorHandled = false; router.handleError((e, req) => { errorHandled = true; }); const cancel = defer<void>(); router.listen( { hostname: "127.0.0.1", port: 8898 }, { cancel: cancel.promise } ); const suiteDefer = defer(); let testCnt = 0; function _test(str: string, func: () => Promise<void>) { testCnt++; func().finally(() => { testCnt--; test(str, func); if (testCnt === 0) { suiteDefer.resolve(); } }); } _test("router should respond string path", async () => { { const res1 = await fetch("http://127.0.0.1:8898/index"); const text = await res1.body.text(); assertEquals(res1.status, 200); assertEquals(text, "ok"); } }); _test("router should respond regexp path", async () => { const res2 = await fetch("http://127.0.0.1:8898/foo/123"); const json = await res2.body.json(); assertEquals(res2.status, 200); assertEquals(res2.headers.get("content-type"), "application/json"); assertEquals(json["id"], "123"); }); _test("router should respond even if req.respond wasn't called", async () => { const res = await fetch("http://127.0.0.1:8898/no-response"); // assertEquals(res.status, 404); const text = await res.body.text(); assertEquals(text, "Not Found"); }); _test("router should respond for unknown path", async () => { const res = await fetch("http://127.0.0.1:8898/not-found"); const text = await res.body.text(); assertEquals(res.status, 404); assertEquals(text, "Not Found"); }); _test("router should handle global error", async () => { const res = await fetch("http://127.0.0.1:8898/throw"); const text = await res.body.text(); assertEquals(res.status, 500); assertEquals(text, "Internal Server Error"); assertEquals(errorHandled, true); }); suiteDefer.promise.finally(() => { cancel.resolve(); });})();runIfMain(import.meta);