serverus/tests/11_test_spa.test.ts

142 lines
3 KiB
TypeScript

import * as asserts from "@std/assert";
import { EPHEMERAL_SERVER, get_ephemeral_listen_server } from "./helpers.ts";
Deno.test({
name: "check that .spa files work",
permissions: {
env: true,
read: true,
write: true,
net: true,
},
fn: async () => {
let test_server_info: EPHEMERAL_SERVER | null = null;
const cwd = Deno.cwd();
try {
Deno.chdir("./tests/www/spa");
test_server_info = await get_ephemeral_listen_server();
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/foo/bar/baz`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(response.ok);
asserts.assert(body);
asserts.assertMatch(body, /SPA PAGE/s);
asserts.assertMatch(body, /test include/s);
}
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/testing`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(response.ok);
asserts.assert(body);
asserts.assertMatch(body, /Hello World - Testing/s);
}
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/testing/blah.html`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(response.ok);
asserts.assert(body);
asserts.assertMatch(body, /Hello World - Blah/s);
}
} finally {
Deno.chdir(cwd);
if (test_server_info) {
await test_server_info?.server?.stop();
}
}
},
});
Deno.test({
name: "check that .spa.static files work",
permissions: {
env: true,
read: true,
write: true,
net: true,
},
fn: async () => {
let test_server_info: EPHEMERAL_SERVER | null = null;
const cwd = Deno.cwd();
try {
Deno.chdir("./tests/www/spa");
test_server_info = await get_ephemeral_listen_server();
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/foo/bar/baz`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(response.ok);
asserts.assert(body);
asserts.assertMatch(body, /SPA PAGE/s);
asserts.assertMatch(body, /test include/s);
}
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/possibly_missing/here.html`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(response.ok);
asserts.assert(body);
asserts.assertMatch(body, /here/s);
}
{
const response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/possibly_missing/missing.html`,
{
method: "GET",
},
);
const body = await response.text();
asserts.assert(!response.ok);
asserts.assert(body);
asserts.assertEquals(response.status, 404);
}
} finally {
Deno.chdir(cwd);
if (test_server_info) {
await test_server_info?.server?.stop();
}
}
},
});