Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/util/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,30 @@ async function startFailureServer(errors: string[]): Promise<void> {
const pathname = new URL(req.url /* v8 ignore next */ ?? "/", serverUrl).pathname;

if (req.method === "GET" && pathname === "/data") {
// Also serve error code when underlying data fetched shows an error.
const payload: OnboardFailureData = {page: "failure", errors};

res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.writeHead(500);
res.end(stringify(payload));

return;
}

if (req.method === "POST" && pathname === "/submit") {
res.writeHead(200);
res.writeHead(500);
res.end(() => {
resolve();
});

return;
}

if (req.method === "GET" && (pathname === "/" || pathname === "/index.html")) {
// Also serve error code when underlying data fetched shows an error.
res.statusCode = 500;
}

const next = finalhandler(req, res);

fileServer(req, res, next);
Expand Down
52 changes: 45 additions & 7 deletions test/onboarding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ describe("Onboarding", () => {
await vi.advanceTimersByTimeAsync(100); // flush

expect(resSetHeader).toHaveBeenNthCalledWith(1, "Content-Type", "application/json");
expect(resWriteHead).toHaveBeenNthCalledWith(1, 200);
expect(resWriteHead).toHaveBeenNthCalledWith(1, 500);
expect(resEnd).toHaveBeenCalledTimes(1);

mockHttpListener(
Expand All @@ -361,6 +361,7 @@ describe("Onboarding", () => {
);
await responsePromise;

expect(resWriteHead).toHaveBeenNthCalledWith(2, 500);
expect(resEnd).toHaveBeenCalledTimes(2);

const serverUrl = new URL(process.env.Z2M_ONBOARD_URL ?? "http://0.0.0.0:8080");
Expand Down Expand Up @@ -506,7 +507,7 @@ describe("Onboarding", () => {
return JSON.parse(resEnd.mock.calls[0][0]) as OnboardSubmitResponse;
};

const requestUnhandledRoute = async (url: string): Promise<void> => {
const requestUnhandledRoute = async (url: string): Promise<{statusCode: number}> => {
let resolveResponse: () => void = () => {};
const responsePromise = new Promise<void>((resolve) => {
resolveResponse = resolve;
Expand All @@ -520,6 +521,12 @@ describe("Onboarding", () => {

resolveResponse();
});
const res = {
end: resEnd,
setHeader: vi.fn(),
writeHead: vi.fn(),
statusCode: 200,
};

mockHttpListener(
{
Expand All @@ -528,14 +535,11 @@ describe("Onboarding", () => {
// @ts-expect-error return not used
on: () => {},
},
{
end: resEnd,
setHeader: vi.fn(),
writeHead: vi.fn(),
},
res,
);

await responsePromise;
return res;
};

const createZipRestore = (): Awaited<ReturnType<typeof JSZip.loadAsync>> => {
Expand Down Expand Up @@ -838,6 +842,40 @@ describe("Onboarding", () => {
expect(mockStaticFileServer).toHaveBeenCalled();
});

it("serves failure page routes with HTTP 500", async () => {
settings.testing.clear();

const configFile = join(data.mockDir, "configuration.yaml");

writeFileSync(
configFile,
`
good: 9
\t wrong
`,
);

let p;
await new Promise<void>((resolve, reject) => {
mockHttpOnListen.mockImplementationOnce(async () => {
try {
expect((await requestUnhandledRoute("/")).statusCode).toBe(500);
expect((await requestUnhandledRoute("/index.html")).statusCode).toBe(500);
await runFailure();
resolve();
} catch (error) {
reject(error);
}
});

p = onboard();
});

await expect(p).resolves.toStrictEqual(false);

data.removeConfiguration();
});

it("returns false when onboarding server emits an error", async () => {
data.removeConfiguration();

Expand Down
Loading