bundler/server.test.ts

111 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, it, mockfn } from "./deps.testing.ts";
import { processRequest } from "./server.ts";
2020-05-14 09:24:38 +00:00
2021-07-27 20:17:38 +00:00
describe("serveBundles", () => {
it("calls deno bundle if asking for js", async () => {
const runner = mockfn(() => {
2021-07-27 20:17:38 +00:00
return {
output: () => Promise.resolve(new TextEncoder().encode("abc")),
status: () => Promise.resolve({ code: 0, success: true }),
} as any;
});
const { response } = await call("/greatlib@1.0.0/reader/file.js", {
runner,
});
await checkResponse(response, 200, "abc");
expect(runner).toHaveBeenCalledTimes(1);
expect(runner).toHaveBeenCalledWith({
2021-07-27 20:17:38 +00:00
cmd: [
"deno",
"bundle",
"https://git.example.com/libs/greatlib/raw/1.0.0/reader/file.ts",
],
stdout: "piped",
});
2020-05-14 09:24:38 +00:00
});
it("serves raw file if asking for anything other than js", async () => {
const fetcher = mockfn(() => new Response("abc"));
const { response, runner } = await call("/greatlib@1.0.0/reader/file.ts", {
fetcher,
2021-07-27 20:17:38 +00:00
});
await checkResponse(response, 200, "abc");
expect(runner).not.toHaveBeenCalled();
expect(fetcher).toHaveBeenCalledTimes(1);
expect(fetcher).toHaveBeenCalledWith(
"https://git.example.com/libs/greatlib/raw/1.0.0/reader/file.ts",
{ redirect: "follow" },
);
2020-05-14 09:24:38 +00:00
});
2021-07-27 20:17:38 +00:00
it("handles bad method", async () => {
const { response, runner } = await call("/greatlib@1.0.0/reader/file.ts", {
2021-07-27 20:17:38 +00:00
method: "POST",
});
await checkResponse(response, 405);
expect(runner).not.toHaveBeenCalled();
2020-05-14 09:24:38 +00:00
});
2021-07-27 20:17:38 +00:00
it("handles bad path", async () => {
const { response, runner } = await call("/greatlib@1.0.0/reader{}.ts");
await checkResponse(
response,
400,
'console.error("bundler error - Invalid path https://git.example.com/libs/greatlib/raw/1.0.0/reader{}.ts");',
2021-07-27 20:17:38 +00:00
);
expect(runner).not.toHaveBeenCalled();
2021-07-27 20:17:38 +00:00
});
2020-05-14 09:24:38 +00:00
2021-07-27 20:17:38 +00:00
it("handles bundle failure", async () => {
const runner = mockfn(() => {
2021-07-27 20:17:38 +00:00
return {
output: () => Promise.resolve(undefined),
status: () => Promise.resolve({ code: 1, success: false }),
} as any;
});
const { response } = await call("/great_lib@1.0.0-dev1/reader.js", {
runner,
2021-07-27 20:17:38 +00:00
});
await checkResponse(
response,
500,
'console.error("bundler error - Failed to bundle https://git.example.com/libs/great_lib/raw/1.0.0-dev1/reader.ts");',
2021-07-27 20:17:38 +00:00
);
2020-05-14 09:24:38 +00:00
});
2021-07-27 20:17:38 +00:00
});
2020-05-14 09:24:38 +00:00
type CallContext = {
runner: ReturnType<typeof mockfn>;
fetcher: ReturnType<typeof mockfn>;
};
type CallOptions = CallContext & {
method: string;
};
type CallResult = CallContext & {
response: Response;
};
2021-07-27 20:17:38 +00:00
async function call(
path: string,
2021-07-27 20:17:38 +00:00
options: Partial<CallOptions> = {},
): Promise<CallResult> {
2021-07-27 20:17:38 +00:00
const method = options.method ?? "GET";
const runner = options.runner ?? mockfn();
const fetcher = options.fetcher ?? mockfn();
2020-05-14 09:24:38 +00:00
const response = await processRequest(
{ method, url: "http://localhost:8000" + path } as any,
2021-07-27 20:17:38 +00:00
"git.example.com/libs",
runner,
fetcher,
2020-05-14 09:24:38 +00:00
);
return { response, runner, fetcher };
}
async function checkResponse(res: Response, status = 200, body?: string) {
expect(res.status).toEqual(status);
if (body) {
expect(await res.text()).toEqual(body);
} else {
expect(res.body).toBeNull();
}
2021-07-27 20:17:38 +00:00
}