import { assertEquals, assert } from "../src/deps_test.ts";import { startServer, killServer, itLog } from "./test.utils.ts";const { test } = Deno;
const ContentTypeJson = "application/json; charset=utf-8";
test({ name: "[http] basic server, requests to home controller", async fn(): Promise<void> { await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/app/home";
itLog("/app/home", true);
try { itLog("\t /text?name=john&test=test");
let response = await fetch(baseUrl + "/text?name=john&test=test"); let text = await response.text();
assertEquals(response.status, 200); assertEquals(text, "Hello world, john test undefined");
itLog("\t /json");
response = await fetch(baseUrl + "/json"); let json = await response.json();
assertEquals(response.headers.get("content-type"), ContentTypeJson); assert(Object.keys(json)[0] == "headers");
itLog("\t /error");
response = await fetch(baseUrl + "/error"); console.log(response);
json = await response.json();
assertEquals(response.status, 403); assertEquals(response.headers.get("content-type"), ContentTypeJson); assertEquals(json.httpCode, 403); assertEquals(json.name, "ForbiddenError"); assertEquals(json.message, "error");
itLog("\t /query?b=b&c=c&a=a");
response = await fetch(baseUrl + "/query?b=b&c=c&a=a"); json = await response.json();
assertEquals(response.status, 200); assertEquals(response.headers.get("content-type"), ContentTypeJson); assertEquals(json.a, "a"); assertEquals(json.b, "b"); assertEquals(json.c, "c");
itLog("\t /test");
response = await fetch(baseUrl + "/test"); text = await response.text();
assertEquals(text, "test");
itLog("\t /test/1");
response = await fetch(baseUrl + "/test/1"); text = await response.text();
assertEquals(text, "1");
itLog("\t /test/1/john");
response = await fetch(baseUrl + "/test/1/john"); text = await response.text();
assertEquals(text, "1 john");
itLog("\t /test/1/john/detail");
response = await fetch(baseUrl + "/test/1/john/detail"); text = await response.text();
assertEquals(text, "1 john this is details page");
itLog("\t /post");
let body = JSON.stringify({ username: "john" }); response = await fetch(baseUrl + "/post", { method: "POST", body }); json = await response.json();
assertEquals(response.headers.get("content-type"), ContentTypeJson); } finally { killServer(); } },});
test({ name: "[http] basic server, requests to info controller", async fn(): Promise<void> { await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/test/info";
itLog("/test/info", true);
try { itLog("\t ''");
let response = await fetch(baseUrl); let text = await response.text();
assertEquals(response.status, 200); assertEquals(text, "Hello info");
itLog("\t '/'");
response = await fetch(baseUrl + "/"); text = await response.text();
assertEquals(response.status, 200); assertEquals(text, "Hello info"); } finally { killServer(); } },});
test({ name: "[http] basic server, request to health controller to test undefined controller and action route", async fn(): Promise<void> { await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/health";
itLog("/health", true);
try { itLog("\t ''"); const response = await fetch(baseUrl); const json = await response.json();
assertEquals(response.status, 200); assertEquals(json.status, "pass"); } finally { killServer(); } },});
test({ name: "[http] basic server, request to root controller to test empty full route", async fn(): Promise<void> { await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000";
itLog("root ''", true);
try { itLog("\t ''"); let response = await fetch(baseUrl); let text = await response.text(); assertEquals(response.status, 200); assertEquals(text, "root page");
itLog("\t '/'"); response = await fetch(`${baseUrl}/`); text = await response.text(); assertEquals(response.status, 200); assertEquals(text, "root page"); } finally { killServer(); } },});