Very Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207import { assert, assertEquals } from "../src/deps_test.ts";import { killServer, startServer } from "./test.utils.ts";const { test } = Deno;
const ContentTypeJson = "application/json; charset=utf-8";
/** * Test cases */test({ name: "[http] basic server, requests to home controller", async fn(t): Promise<void> { const process = await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/app/home";
try { // It await t.step("/text?name=john&test=test", async () => { 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"); });
// It await t.step("/json", async () => { let response = await fetch(baseUrl + "/json"); let json = await response.json();
assertEquals(response.headers.get("content-type"), ContentTypeJson); assert(Object.keys(json)[0] == "headers"); });
// It await t.step("/error", async () => { const response = await fetch(baseUrl + "/error");
const 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"); });
// It await t.step("/query?b=b&c=c&a=a", async () => { const response = await fetch(baseUrl + "/query?b=b&c=c&a=a"); const 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"); assertEquals(json.all.a, "a"); assertEquals(json.all.b, "b"); assertEquals(json.all.c, "c"); });
// It await t.step("/test", async () => { const response = await fetch(baseUrl + "/test"); const text = await response.text();
assertEquals(text, "test"); });
// It await t.step("/test/1", async () => { const response = await fetch(baseUrl + "/test/1"); const text = await response.text();
assertEquals(text, "1"); });
// It await t.step("/test/1/john", async () => { const response = await fetch(baseUrl + "/test/1/john"); const text = await response.text(); assertEquals(text, "1 john"); });
// It await t.step("/test/1/john/detail", async () => { const response = await fetch(baseUrl + "/test/1/john/detail"); const text = await response.text();
assertEquals(text, "1 john this is details page"); });
// It await t.step("/post", async () => { let body = JSON.stringify({ username: "john" }); const response = await fetch(baseUrl + "/post", { method: "POST", body }); const json = await response.json();
assertEquals(response.headers.get("content-type"), ContentTypeJson); // TODO fix it // assertEquals(json.username, 'john'); }); } finally { killServer(process); } },});
/** * Test cases */test({ name: "[http] basic server, requests to info controller", async fn(t): Promise<void> { const process = await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/test/info";
try { await t.step("/test/info", async () => { const response = await fetch(baseUrl); const text = await response.text();
assertEquals(response.status, 200); assertEquals(text, "Hello info"); });
await t.step("/test/info/", async () => { const response = await fetch(baseUrl + "/"); const text = await response.text();
assertEquals(response.status, 200); assertEquals(text, "Hello info"); }); } finally { killServer(process); } },});
test({ name: "[http] basic server, request to health controller to test undefined controller and action route", async fn(t): Promise<void> { const process = await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000/health";
try { await t.step("/health", async () => { const response = await fetch(baseUrl); const json = await response.json();
assertEquals(response.status, 200); assertEquals(json.status, "pass"); }); } finally { killServer(process); } },});
test({ name: "[http] basic server, request to root controller to test empty full route", async fn(t): Promise<void> { const process = await startServer("./examples/basic/app.ts"); const baseUrl = "http://localhost:8000";
try { await t.step("", async () => { const response = await fetch(baseUrl); const text = await response.text(); assertEquals(response.status, 200); assertEquals(text, "root page"); });
await t.step("/", async () => { const response = await fetch(`${baseUrl}/`); const text = await response.text(); assertEquals(response.status, 200); assertEquals(text, "root page"); }); } finally { killServer(process); } },});
test({ name: "[http] basic server, return native response", async fn(t): Promise<void> { const process = await startServer("./examples/basic/app.ts");
try { await t.step("app/home/response-test", async () => { let response = await fetch( "http://localhost:8000/app/home/response-test", ); let text = await response.text(); assertEquals(response.status, 201); assertEquals(response.headers.get("x-alosaur-header"), "test"); assertEquals(text, "Object created"); }); } finally { killServer(process); } },});