Skip to main content
Module

x/fastro/test/middleware_test.ts

Fast and simple web application framework for deno
Go to Latest
File
const { test } = Deno;import { assertEquals } from "../deps.ts";import { Fastro } from "../mod.ts";const addr = "http://localhost:8000";const port = 8000;
test({ name: "NO MIDDLEWARE", async fn() { const server = new Fastro(); server.listen({ port }); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "not found"); server.close(); },});
test({ name: "MIDDLEWARE, ROOT NOT FOUND WHEN USE WITH NOT-ROOT ", async fn() { const server = new Fastro(); server.use("/oke", (req) => req.send("root")); server.use("/hi", (req) => req.send("root")); server.listen({ port }); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "not found"); server.close(); },});
test({ name: "MIDDLEWARE, SEND FROM ROOT", async fn() { const server = new Fastro(); server.use((req) => req.send("root")); server.listen({ port }); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "root"); server.close(); },});
test({ name: "MIDDLEWARE, SEND FROM NON-ROOT", async fn() { const server = new Fastro(); server.use("/ok", (req) => req.send("ok")); server.listen({ port }); const result = await fetch(addr + "/ok"); const text = await result.text(); assertEquals(text, "ok"); server.close(); },});
test({ name: "MIDDLEWARE, SEND FROM NON-ROOT 2", async fn() { const server = new Fastro(); server.use("/ok", (req) => req.send("ok")); server.use("/hi", (req) => req.send("hi")); server.listen({ port }); const result = await fetch(addr + "/ok"); const text = await result.text(); assertEquals(text, "ok"); server.close(); },});
test({ name: "MIDDLEWARE, USE GLOBAL MIDDLEWARE", async fn() { const server = new Fastro(); server .use((req, done) => { req.hi = "hi"; done(); }) .use("/hi", (req) => req.send(req.hi)) .use("/ok", (req) => req.send("ok")); server.listen({ port }); const result = await fetch(addr + "/hi"); const text = await result.text(); assertEquals(text, "hi"); server.close(); },});
test({ name: "MIDDLEWARE, MODIF REQUEST X ROUTER", async fn() { const server = new Fastro(); server .use((req, done) => { req.sendOk = (payload: string) => { req.send(payload); }; done(); }); server.get("/", (req) => req.sendOk("plugin")); server.listen({ port }); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "plugin"); server.close(); },});
test({ name: "MIDDLEWARE, GET URL PARAMETER", async fn() { const server = new Fastro(); server .use("/ok", (req, done) => { req.ok = req.parameter; done(); }) .get("/ok", (req) => { req.send(req.ok); }); server.listen({ port }); const result = await fetch(addr + "/ok/agus"); const text = await result.text(); const [, name] = JSON.parse(text); assertEquals(name, "agus"); server.close(); },});