Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't return 304 status code for HEAD requests #54

Merged
merged 1 commit into from
Nov 2, 2023
Merged
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
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
Loading