deno.land / x / opine@2.3.4 / test / units / app.use.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568// deno-lint-ignore-file no-explicit-anyimport opine from "../../mod.ts";import { expect, superdeno } from "../deps.ts";import { after, describe, it } from "../utils.ts";import type { NextFunction, OpineRequest, OpineResponse,} from "../../src/types.ts";
describe("app", function () { it('should emit "mount" when mounted', function (done) { const blog = opine(); const app = opine();
blog.on("mount", function (arg) { expect(arg).toEqual(app); done(); });
app.use(blog); });
describe(".use(app)", function () { it("should mount the app", function (done) { const blog = opine(); const app = opine();
blog.get("/blog", function (req, res) { res.end("blog"); });
app.use(blog);
superdeno(app) .get("/blog") .expect("blog", done); });
it("should support mount-points", function (done) { const blog = opine(); const forum = opine(); const app = opine();
blog.get("/", function (req, res) { res.end("blog"); });
forum.get("/", function (req, res) { res.end("forum"); });
app.use("/blog", blog); app.use("/forum", forum);
superdeno(app) .get("/blog") .expect("blog", function () { superdeno(app) .get("/forum") .expect("forum", done); }); });
it("should set the child's .parent", function () { const blog = opine(); const app = opine();
app.use("/blog", blog); expect(blog.parent).toEqual(app); });
it("should support dynamic routes", function (done) { const blog = opine(); const app = opine();
blog.get("/", function (req, res) { res.end("success"); });
app.use("/post/:article", blog);
superdeno(app) .get("/post/once-upon-a-time") .expect("success", done); });
it("should support mounted app anywhere", function (done) { const cb = after(3, done); const blog = opine(); const other = opine(); const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
blog.get("/", function (req, res) { res.end("success"); });
blog.on("mount", function (parent: any) { expect(parent).toEqual(app); cb(); }); other.on("mount", function (parent: any) { expect(parent).toEqual(app); cb(); });
app.use("/post/:article", fn1, other, fn2, blog);
superdeno(app) .get("/post/once-upon-a-time") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("success", cb); }); });
describe(".use(middleware)", function () { it("should accept multiple arguments", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
app.use(fn1, fn2, function fn3(req, res) { res.set("x-fn-3", "hit"); res.end(); });
superdeno(app) .get("/") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should invoke middleware for all requests", function (done) { const app = opine(); const cb = after(3, done);
app.use(function (req, res) { res.send("saw " + req.method + " " + req.url); });
superdeno(app) .get("/") .expect(200, "saw GET /", cb);
superdeno(app) .options("/") .expect(200, "saw OPTIONS /", cb);
superdeno(app) .post("/foo") .expect(200, "saw POST /foo", cb); });
it("should accept array of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
app.use([fn1, fn2, fn3]);
superdeno(app) .get("/") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should accept multiple arrays of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
app.use([fn1, fn2], [fn3]);
superdeno(app) .get("/") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should accept nested arrays of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
(app.use as any)([[fn1], fn2], [fn3]);
superdeno(app) .get("/") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); }); });
describe(".use(path, middleware)", function () { it("should require middleware", function () { const app = opine(); expect(function () { app.use("/"); }).toThrow(/requires a middleware function/); });
it("should reject string as middleware", function () { const app = opine(); expect(function () { app.use("/", "foo" as any); }).toThrow(/requires a middleware function but got a string/); });
it("should reject number as middleware", function () { const app = opine(); expect(function () { app.use("/", 42 as any); }).toThrow(/requires a middleware function but got a number/); });
it("should reject null as middleware", function () { const app = opine(); expect(function () { app.use("/", null as any); }).toThrow(/requires a middleware function but got a Null/); });
it("should reject Date as middleware", function () { const app = opine(); expect(function () { app.use("/", new Date() as any); }).toThrow(/requires a middleware function but got a Date/); });
it("should strip path from req.url", function (done) { const app = opine();
app.use("/foo", function (req, res) { res.send("saw " + req.method + " " + req.url); });
superdeno(app) .get("/foo/bar") .expect(200, "saw GET /bar", done); });
it("should accept multiple arguments", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
app.use("/foo", fn1, fn2, function fn3(req, res) { res.set("x-fn-3", "hit"); res.end(); });
superdeno(app) .get("/foo") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it( "should invoke middleware for all requests starting with path", function ( done, ) { const app = opine(); const cb = after(3, done);
app.use("/foo", function (req, res) { res.send("saw " + req.method + " " + req.url); });
superdeno(app) .get("/") .expect(404, cb);
superdeno(app) .post("/foo") .expect(200, "saw POST /", cb);
superdeno(app) .post("/foo/bar") .expect(200, "saw POST /bar", cb); }, );
it("should work if path has trailing slash", function (done) { const app = opine(); const cb = after(3, done);
app.use("/foo/", function (req, res) { res.send("saw " + req.method + " " + req.url); });
superdeno(app) .get("/") .expect(404, cb);
superdeno(app) .post("/foo") .expect(200, "saw POST /", cb);
superdeno(app) .post("/foo/bar") .expect(200, "saw POST /bar", cb); });
it("should accept array of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
app.use("/foo", [fn1, fn2, fn3]);
superdeno(app) .get("/foo") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should accept multiple arrays of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
app.use("/foo", [fn1, fn2], [fn3]);
superdeno(app) .get("/foo") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should accept nested arrays of middleware", function (done) { const app = opine();
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.end(); }
(app.use as any)("/foo", [fn1, [fn2]], [fn3]);
superdeno(app) .get("/foo") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, done); });
it("should support array of paths", function (done) { const app = opine(); const cb = after(3, done);
app.use(["/foo/", "/bar"], function (req, res) { res.send( "saw " + req.method + " " + req.url + " through " + req.originalUrl, ); });
superdeno(app) .get("/") .expect(404, cb);
superdeno(app) .get("/foo") .expect(200, "saw GET / through /foo", cb);
superdeno(app) .get("/bar") .expect(200, "saw GET / through /bar", cb); });
it("should support array of paths with middleware array", function (done) { const app = opine(); const cb = after(2, done);
function fn1(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-1", "hit"); next(); }
function fn2(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-2", "hit"); next(); }
function fn3(req: OpineRequest, res: OpineResponse, next: NextFunction) { res.set("x-fn-3", "hit"); res.send( "saw " + req.method + " " + req.url + " through " + req.originalUrl, ); }
(app.use as any)(["/foo/", "/bar"], [[fn1], fn2], [fn3]);
superdeno(app) .get("/foo") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, "saw GET / through /foo", cb);
superdeno(app) .get("/bar") .expect("x-fn-1", "hit") .expect("x-fn-2", "hit") .expect("x-fn-3", "hit") .expect(200, "saw GET / through /bar", cb); });
it("should support regexp path", function (done) { const app = opine(); const cb = after(4, done);
app.use(/^\/[a-z]oo/, function (req, res) { res.send( "saw " + req.method + " " + req.url + " through " + req.originalUrl, ); });
superdeno(app) .get("/") .expect(404, cb);
superdeno(app) .get("/foo") .expect(200, "saw GET / through /foo", cb);
superdeno(app) .get("/zoo/bear") .expect(200, "saw GET /bear through /zoo/bear", cb);
superdeno(app) .get("/get/zoo") .expect(404, cb); });
it("should support empty string path", function (done) { const app = opine();
app.use("", function (req, res) { res.send( "saw " + req.method + " " + req.url + " through " + req.originalUrl, ); });
superdeno(app) .get("/") .expect(200, "saw GET / through /", done); }); });});
Version Info