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
54 changes: 54 additions & 0 deletions src/server/workflows/site-audit-workflow-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,57 @@ describe("crawlPage", () => {
expect(result.status, result.stderr).toBe(0);
}, 25_000);
});

const LINKED_PAGE_HTML = `<!DOCTYPE html><html><head><title>A page title</title></head>
<body><h1>Hi</h1><p>Body text.</p><a href="/next">next</a></body></html>`;

function serveAs(contentType: string) {
vi.stubGlobal("fetch", () =>
Promise.resolve(
new Response(LINKED_PAGE_HTML, {
status: 200,
headers: { "content-type": contentType },
}),
),
);
}

function summarize(page: Awaited<ReturnType<typeof crawlPage>>) {
return {
isHtml: page?.isHtml,
title: page?.title,
linkCount: page?.links.length,
};
}

describe("crawlPage content-type classification", () => {
// The crawler sends `Accept: text/html,application/xhtml+xml`, and media
// types are case-insensitive, so both spellings must be analyzed.
it.each([
"text/html; charset=utf-8",
"application/xhtml+xml; charset=utf-8",
"TEXT/HTML",
])("analyzes a page served as %s", async (contentType) => {
serveAs(contentType);

const page = await crawl();

expect(summarize(page)).toEqual({
isHtml: true,
title: "A page title",
linkCount: 1,
});
});

it("records a non-HTML document without analyzing it", async () => {
serveAs("application/pdf");

const page = await crawl();

expect(summarize(page)).toEqual({
isHtml: false,
title: "",
linkCount: 0,
});
});
});
11 changes: 9 additions & 2 deletions src/server/workflows/site-audit-workflow-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ export async function crawlPage(
});
}

const contentType = response.headers.get("content-type") ?? "";
const isHtml = contentType.includes("text/html");
// Media types are case-insensitive, and the crawl Accept header asks for
// application/xhtml+xml as well — a page served as either is a document
// the analyzer can read.
const contentType = (
response.headers.get("content-type") ?? ""
).toLowerCase();
const isHtml =
contentType.includes("text/html") ||
contentType.includes("application/xhtml+xml");
// Cap what we read: the first 1 MiB still contains the SEO metadata and
// navigation needed by the audit in normal documents.
const body = isHtml ? await readTextUpTo(response, MAX_HTML_BYTES) : "";
Expand Down