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
2 changes: 1 addition & 1 deletion http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ export async function serveDir(
req: Request,
opts: ServeDirOptions = {},
): Promise<Response> {
if (req.method !== METHOD.Get) {
if (req.method !== METHOD.Get && req.method !== METHOD.Head) {
return createStandardResponse(STATUS_CODE.MethodNotAllowed);
}

Expand Down
24 changes: 24 additions & 0 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,30 @@ Deno.test(async function serveFileHeadRequest() {
assertEquals(res.headers.get("content-length"), "10034");
});

Deno.test(async function serveDirHeadRequest() {
const req = new Request("http://localhost/test_file.txt", {
method: "HEAD",
});
const res = await serveDir(req, serveDirOptions);
assert(!res.body);
assertEquals(res.status, 200);
assertEquals(res.statusText, "OK");
assertEquals(res.headers.get("content-type"), "text/plain; charset=UTF-8");
assertEquals(res.headers.get("content-length"), "10034");
});

Deno.test(async function serveDirHeadRequestForDirectoryWithIndex() {
const req = new Request("http://localhost/subdir-with-index/", {
method: "HEAD",
});
const res = await serveDir(req, serveDirOptions);
assert(!res.body);
assertEquals(res.status, 200);
assertEquals(res.statusText, "OK");
assertEquals(res.headers.get("content-type"), "text/html; charset=UTF-8");
assert(res.headers.has("content-length"));
});

Deno.test("(unstable) serveDir() serves files without the need of html extension when cleanUrls=true", async () => {
const req = new Request("http://localhost/hello");
const res = await unstableServeDir(req, {
Expand Down
Loading