Skip to content

Commit

Permalink
fix: don't return 304 status code for HEAD requests (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 authored Nov 2, 2023
1 parent 1a4ea10 commit 38c2d91
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/handlers/strategies/serveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 38c2d91

Please sign in to comment.