This commit merges API routes and pages into a single concept. The pages/api folder is now not special anymore.
126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
import { router as r, ServerContext } from "../server.ts";
|
|
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
|
import routes from "./fixture/routes.gen.ts";
|
|
|
|
const router = r.router(ServerContext.fromRoutes(routes).routes());
|
|
|
|
Deno.test("/ page prerender", async () => {
|
|
const resp = await router(new Request("https://fresh.deno.dev/"));
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
|
const body = await resp.text();
|
|
assertStringIncludes(body, `<html lang="en">`);
|
|
assertStringIncludes(body, "index.js");
|
|
assertStringIncludes(body, "<p>Hello!</p>");
|
|
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
|
|
assertStringIncludes(
|
|
body,
|
|
`<script id="__FRSH_PROPS" type="application/json">`,
|
|
);
|
|
assertStringIncludes(
|
|
body,
|
|
`"data":[["home","Hello!"]]`,
|
|
);
|
|
});
|
|
|
|
Deno.test("/props/123 page prerender", async () => {
|
|
const resp = await router(new Request("https://fresh.deno.dev/props/123"));
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
|
const body = await resp.text();
|
|
assertStringIncludes(
|
|
body,
|
|
`{"props":{"params":{"id":"123"},"url":"https://fresh.deno.dev/props/123","route":"/props/:id"}}`,
|
|
);
|
|
});
|
|
|
|
Deno.test("/[name] page prerender", async () => {
|
|
const resp = await router(new Request("https://fresh.deno.dev/foo"));
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
|
const body = await resp.text();
|
|
assertStringIncludes(body, "[name].js");
|
|
assertStringIncludes(body, "<div>Hello foo</div>");
|
|
assertStringIncludes(
|
|
body,
|
|
`<script id="__FRSH_PROPS" type="application/json">`,
|
|
);
|
|
assertStringIncludes(
|
|
body,
|
|
`"params":{"name":"foo"}`,
|
|
);
|
|
});
|
|
|
|
Deno.test("/intercept - GET html", async () => {
|
|
const req = new Request("https://fresh.deno.dev/intercept", {
|
|
headers: { "accept": "text/html" },
|
|
});
|
|
const resp = await router(req);
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
const body = await resp.text();
|
|
assert(body.includes("<div>This is HTML</div>"));
|
|
});
|
|
|
|
Deno.test("/intercept - GET text", async () => {
|
|
const req = new Request("https://fresh.deno.dev/intercept", {
|
|
headers: { "accept": "text/plain" },
|
|
});
|
|
const resp = await router(req);
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
const body = await resp.text();
|
|
assertEquals(body, "This is plain text");
|
|
});
|
|
|
|
Deno.test("/intercept - POST", async () => {
|
|
const req = new Request("https://fresh.deno.dev/intercept", {
|
|
method: "POST",
|
|
});
|
|
const resp = await router(req);
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
const body = await resp.text();
|
|
assertEquals(body, "POST response");
|
|
});
|
|
|
|
Deno.test("/intercept - DELETE", async () => {
|
|
const req = new Request("https://fresh.deno.dev/intercept", {
|
|
method: "DELETE",
|
|
});
|
|
const resp = await router(req);
|
|
assert(resp);
|
|
assertEquals(resp.status, 405);
|
|
});
|
|
|
|
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
|
const resp = await router(
|
|
new Request("https://fresh.deno.dev/api/get_only", {
|
|
method: "NOTAMETHOD",
|
|
}),
|
|
);
|
|
assert(resp);
|
|
assertEquals(resp.status, 405);
|
|
});
|
|
|
|
Deno.test("/api/xyz not found", async () => {
|
|
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
|
|
assert(resp);
|
|
assertEquals(resp.status, 404);
|
|
});
|
|
|
|
Deno.test("/static page prerender", async () => {
|
|
const resp = await router(new Request("https://fresh.deno.dev/static"));
|
|
assert(resp);
|
|
assertEquals(resp.status, 200);
|
|
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
|
|
const body = await resp.text();
|
|
assert(!body.includes(`static.js`));
|
|
assert(!body.includes(`</script>`));
|
|
assertStringIncludes(body, "<p>This is a static page.</p>");
|
|
assert(!body.includes("__FRSH_PROPS"));
|
|
});
|