Skip to main content
Module

x/fastro/test/server_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: "GET", async fn() { const server = new Fastro(); server.route({ method: "GET", url: "/", handler(req) { req.respond({ body: "hello" }); }, }); server.listen({ port }); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "hello"); server.close(); },});
test({ name: "POST", async fn() { const server = new Fastro({ payload: true }); server.route({ method: "POST", url: "/", async handler(req) { const payload = req.payload; req.respond({ body: payload }); }, }); server.listen({ port }); const result = await fetch(addr, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ msg: "hello" }), }); const text = await result.text(); assertEquals(text, '{"msg":"hello"}'); server.close(); },});
test({ name: "GET USER", async fn() { const server = new Fastro(); server.route({ method: "GET", url: "/login/user", handler(req) { const [, , name] = req.parameter; req.respond({ body: `hello, ${name}` }); }, }); server.listen({ port }); const result = await fetch(addr + "/login/user/agus"); const text = await result.text(); assertEquals(text, "hello, agus"); server.close(); },});
test({ name: "MIDDLEWARE", 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 WITH URL", async fn() { const server = new Fastro(); server .use("/ok", (req, done) => { req.sendOk = (payload: string) => { req.send(payload); }; done(); }) .get("/ok", (req) => { req.sendOk("MIDDLEWARE"); }); server.listen({ port }); const result = await fetch(addr + "/ok"); const text = await result.text(); assertEquals(text, "MIDDLEWARE"); server.close(); },});
test({ name: "MIDDLEWARE WITH 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(); },});
test({ name: "PLUGIN", async fn() { const server = new Fastro(); server .register((fastro) => { fastro.get("/ok", (req) => { req.send("PLUGIN"); }); }); server.listen({ port }); const result = await fetch(addr + "/ok"); const text = await result.text(); assertEquals(text, "PLUGIN"); server.close(); },});
test({ name: "PLUGIN WITH PREFIX", async fn() { const server = new Fastro(); server .register("v1", (fastro, done) => { fastro.get("/ok", (req) => { req.send("PLUGIN"); }); done(); }); server.listen({ port }); const result = await fetch(addr + "/v1/ok"); const text = await result.text(); assertEquals(text, "PLUGIN"); server.close(); },});
test({ name: "STATIC FILE", async fn() { const server = new Fastro({ static: true }); server.static("/", "public"); server.listen({ port }); const result = await fetch(addr + "/text"); const text = await result.text(); assertEquals(text, "ok"); server.close(); },});