Skip to main content
Module

x/fastro/test/post_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: "POST JSON", async fn() { const server = new Fastro({ payload: true }); server.route({ method: "POST", url: "/", async handler(req) { const payload = req.payload; req.send(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: "POST FORM MULTIPART", async fn() { const server = new Fastro({ payload: true }); server.route({ method: "POST", url: "/", async handler(req) { const payload = req.payload; req.send(payload); }, }); server.listen({ port });
// post a file let data = new FormData(); const file = await Deno.readFile("public/text"); const blob = new Blob([file.buffer]); data.append("file", blob, "file_name.txt"); data.append("name", "agus"); const result = await fetch(addr, { method: "POST", body: data, }); const text = await result.text(); const [v] = JSON.parse(text); assertEquals(v.value, "ok"); server.close(); },});
test({ name: "POST FORM URL ENCODED", async fn() { const server = new Fastro({ payload: true }); server.route({ method: "POST", url: "/", async handler(req) { const payload = req.payload; req.send(payload); }, }); server.listen({ port });
var data = new URLSearchParams(); data.append("userName", "test@gmail.com"); data.append("password", "Password"); data.append("grant_type", "password");
const result = await fetch(addr, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: data, }); const text = await result.text(); const [c] = JSON.parse(text); assertEquals(c, { userName: "test@gmail.com" }); server.close(); },});