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

src: send a 405 for anything other than GET or HEAD #170

Merged
merged 1 commit into from
Nov 9, 2024
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: 7 additions & 0 deletions src/middleware/methodNotAllowedMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Middleware } from './middleware';

export class MethodNotAllowedMiddleware implements Middleware {
handle(): Promise<Response> {
return Promise.resolve(new Response(undefined, { status: 405 }));
}
}
3 changes: 3 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import latestVersions from '../constants/latestVersions.json' assert { type: 'json' };
import { cached } from '../middleware/cacheMiddleware';
import { MethodNotAllowedMiddleware } from '../middleware/methodNotAllowedMiddleware';
import { NotFoundMiddleware } from '../middleware/notFoundMiddleware';
import { OptionsMiddleware } from '../middleware/optionsMiddleware';
import { OriginMiddleware } from '../middleware/originMiddleware';
Expand Down Expand Up @@ -52,6 +53,8 @@ export function registerRoutes(router: Router): void {
]);

router.get('*', [new NotFoundMiddleware()]);

router.all('*', [new MethodNotAllowedMiddleware()]);
}

export * from './router';
8 changes: 8 additions & 0 deletions src/routes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export class Router {
return this.itty.fetch(request, ctx);
}

all(endpoint: string, middlewares: Middleware[]): void {
const middlewareChain = buildMiddlewareChain(middlewares);

this.itty.all(endpoint, (req, ctx) => {
return callMiddlewareChain(middlewareChain, req, ctx);
});
}

options(endpoint: string, middlewares: Middleware[]): void {
const middlewareChain = buildMiddlewareChain(middlewares);

Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ describe('File Tests', () => {
assert.strictEqual(body, '{ hello:');
});

it('sends a 405 for anything other than GET or HEAD', async () => {
// Doesn't need to be all-inclusive
for (const method of ['POST', 'PATCH', 'DELETE', 'PROPFIND']) {
const res = await mf.dispatchFetch(`${url}`, {
method: method,
});
assert.strictEqual(res.status, 405, method);
}
});

// Cleanup Miniflare
after(async () => mf.dispose());
});
Loading