|
1 | 1 | // Core Canvas document HTTP response coverage. |
2 | | -import { mkdtemp, rm } from "node:fs/promises"; |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
3 | 3 | import type { IncomingMessage, ServerResponse } from "node:http"; |
4 | 4 | import { tmpdir } from "node:os"; |
5 | 5 | import path from "node:path"; |
6 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
7 | | -import { createCanvasDocument } from "./documents.js"; |
| 7 | +import { createCanvasDocument, resolveCanvasDocumentsDir } from "./documents.js"; |
8 | 8 | import { handleCanvasDocumentHttpRequest } from "./serve.runtime.js"; |
9 | 9 |
|
10 | 10 | const tempDirs: string[] = []; |
@@ -81,6 +81,59 @@ describe("core canvas document host", () => { |
81 | 81 | expect(response.headers["content-security-policy"]).toBeUndefined(); |
82 | 82 | }); |
83 | 83 |
|
| 84 | + it("serves Content-Length on HEAD responses", async () => { |
| 85 | + const stateDir = await createStateDir(); |
| 86 | + const html = "<html><body>widget</body></html>"; |
| 87 | + const document = await createCanvasDocument( |
| 88 | + { |
| 89 | + id: "widget-1", |
| 90 | + kind: "html_bundle", |
| 91 | + entrypoint: { type: "html", value: html }, |
| 92 | + cspSandbox: "scripts", |
| 93 | + }, |
| 94 | + { stateDir }, |
| 95 | + ); |
| 96 | + const css = "body { color: red; }"; |
| 97 | + await writeFile( |
| 98 | + path.join(resolveCanvasDocumentsDir(stateDir), "widget-1", "style.css"), |
| 99 | + css, |
| 100 | + "utf8", |
| 101 | + ); |
| 102 | + const cssUrl = document.entryUrl.replace(/index\.html$/, "style.css"); |
| 103 | + |
| 104 | + const getHtml = await capture(document.entryUrl); |
| 105 | + const headHtml = await capture(document.entryUrl, "HEAD"); |
| 106 | + expect(headHtml.statusCode).toBe(200); |
| 107 | + expect(headHtml.headers["content-length"]).toBe(String(getHtml.body.byteLength)); |
| 108 | + expect(headHtml.body.byteLength).toBe(0); |
| 109 | + expect(headHtml.headers["content-type"]).toBe("text/html; charset=utf-8"); |
| 110 | + expect(headHtml.headers["content-security-policy"]).toBe("sandbox allow-scripts"); |
| 111 | + |
| 112 | + const getCss = await capture(cssUrl); |
| 113 | + const headCss = await capture(cssUrl, "HEAD"); |
| 114 | + expect(getCss.statusCode).toBe(200); |
| 115 | + expect(headCss.statusCode).toBe(200); |
| 116 | + expect(headCss.headers["content-length"]).toBe(String(getCss.body.byteLength)); |
| 117 | + expect(headCss.body.byteLength).toBe(0); |
| 118 | + |
| 119 | + // Invalid UTF-8 in a copied HTML file expands to U+FFFD when served, so the |
| 120 | + // header must be measured from the decoded representation, not raw bytes. |
| 121 | + const brokenBytes = Buffer.from([ |
| 122 | + 0x3c, 0x68, 0x31, 0x3e, 0xff, 0xfe, 0x3c, 0x2f, 0x68, 0x31, 0x3e, |
| 123 | + ]); |
| 124 | + await writeFile( |
| 125 | + path.join(resolveCanvasDocumentsDir(stateDir), "widget-1", "broken.html"), |
| 126 | + brokenBytes, |
| 127 | + ); |
| 128 | + const brokenUrl = document.entryUrl.replace(/index\.html$/, "broken.html"); |
| 129 | + const getBroken = await capture(brokenUrl); |
| 130 | + const headBroken = await capture(brokenUrl, "HEAD"); |
| 131 | + expect(getBroken.statusCode).toBe(200); |
| 132 | + expect(getBroken.body.byteLength).toBeGreaterThan(brokenBytes.byteLength); |
| 133 | + expect(headBroken.headers["content-length"]).toBe(String(getBroken.body.byteLength)); |
| 134 | + expect(headBroken.body.byteLength).toBe(0); |
| 135 | + }); |
| 136 | + |
84 | 137 | it("rejects unsupported methods and traversal paths", async () => { |
85 | 138 | await createStateDir(); |
86 | 139 | const methodResponse = await capture( |
|
0 commit comments