Skip to main content
Module

x/fastro/server_test.ts

Fast and simple web application framework for deno
Go to Latest
File
const { test } = Deno;import { assertEquals } from "./dev_deps.ts";import { decode, ServerRequest } from "./deps.ts";import { Fastro } from "./server.ts";const { readAll } = Deno;const addr = `http://localhost:8000`;
test({ name: "GET", async fn() { const server = new Fastro(); server.route({ method: "GET", url: "/", handler(req: ServerRequest) { req.respond({ body: "hello" }); }, }); const p = server.listen(); const result = await fetch(addr); const text = await result.text(); assertEquals(text, "hello"); server.close(); },});
test({ name: "POST", async fn() { const server = new Fastro(); server.route({ method: "POST", url: "/", async handler(req: ServerRequest) { const payload = decode(await readAll(req.body)); req.respond({ body: payload }); }, }); const p = server.listen(); 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/:name", handler(req: ServerRequest) { req.respond({ body: "hello" }); }, }); const p = server.listen(); const result = await fetch(addr + "/login/user/agus"); const text = await result.text(); assertEquals(text, "hello"); server.close(); },});