diff --git a/src/handlers/strategies/serveFile.ts b/src/handlers/strategies/serveFile.ts index 32c0d48..7aa9060 100644 --- a/src/handlers/strategies/serveFile.ts +++ b/src/handlers/strategies/serveFile.ts @@ -14,6 +14,11 @@ import responses from '../../commonResponses'; * @returns Http status code */ function getStatusCode(request: Request, objectHasBody: boolean): number { + // Don't return 304 for HEAD requests + if (request.method === 'HEAD') { + return 200; + } + if (objectHasBody) { if (request.headers.has('range')) { // Range header was sent, this is @@ -33,7 +38,7 @@ function getStatusCode(request: Request, objectHasBody: boolean): number { return 412; } - // We weren't given a body + // We weren't given a body and preconditions succeeded return 304; } diff --git a/tests/e2e/directory.test.ts b/tests/e2e/directory.test.ts index d5eb614..ee3d0fb 100644 --- a/tests/e2e/directory.test.ts +++ b/tests/e2e/directory.test.ts @@ -96,6 +96,16 @@ describe('Directory Tests (Restricted Directory Listing)', () => { assert.strictEqual(res.status, 200); }); + it('allows HEAD `/dist` and returns no body', async () => { + const res = await mf.dispatchFetch(`${url}dist/`, { + method: 'HEAD', + }); + assert.strictEqual(res.status, 200); + + const body = await res.text(); + assert.strictEqual(body.length, 0); + }); + it('redirects `/download` to `/download/`', async () => { const originalRes = await mf.dispatchFetch(`${url}download`, { redirect: 'manual', diff --git a/tests/e2e/file.test.ts b/tests/e2e/file.test.ts index d7ab690..98e1888 100644 --- a/tests/e2e/file.test.ts +++ b/tests/e2e/file.test.ts @@ -37,6 +37,21 @@ describe('File Tests', () => { assert.strictEqual(body, `{ hello: 'world' }`); }); + it('HEAD `/dist/index.json` returns no body and status code 200', async () => { + const res = await mf.dispatchFetch(`${url}dist/index.json`, { + method: 'HEAD', + }); + assert.strictEqual(res.status, 200); + assert.strictEqual(res.headers.get('content-type'), 'application/json'); + assert.strictEqual(res.headers.get('cache-control'), cacheControl); + assert.strictEqual(res.headers.has('etag'), true); + assert.strictEqual(res.headers.has('last-modified'), true); + assert.strictEqual(res.headers.has('content-type'), true); + + const body = await res.text(); + assert.strictEqual(body.length, 0); + }); + it('returns 404 for unknown file', async () => { const res = await mf.dispatchFetch(`${url}dist/asd123.json`); assert.strictEqual(res.status, 404);