Skip to content

Commit

Permalink
Change default response to 405 for wrong method (#2372)
Browse files Browse the repository at this point in the history
Feature was introduced in #2366 .
Making it default behavior in next major.
  • Loading branch information
RobinTail authored Feb 6, 2025
1 parent 8aa15eb commit b394427
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### v23.0.0

- Publicly exposed interface `CustomHeaderSecurity` is renamed to `HeaderSecurity`.
- Publicly exposed interface `CustomHeaderSecurity` is renamed to `HeaderSecurity`;
- The default value for `wrongMethodBehavior` config option is changed to `405`.

## Version 22

Expand Down
3 changes: 1 addition & 2 deletions src/config-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export interface CommonConfig {
* @desc How to respond to a request that uses a wrong method to an existing endpoint
* @example 404 — Not found
* @example 405 — Method not allowed, incl. the "Allow" header with a list of methods
* @default 404
* @todo consider changing default to 405 in v23
* @default 405
* */
wrongMethodBehavior?: 404 | 405;
/**
Expand Down
25 changes: 25 additions & 0 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {

interface Queries {
headerSecurity: TSESTree.Identifier;
createConfig: TSESTree.ObjectExpression;
}

type Listener = keyof Queries;

const queries: Record<Listener, string> = {
headerSecurity: `${NT.Identifier}[name='CustomHeaderSecurity']`,
createConfig: `${NT.CallExpression}[callee.name='createConfig'] > ${NT.ObjectExpression}`,
};

const listen = <
Expand All @@ -35,6 +37,7 @@ const v23 = ESLintUtils.RuleCreator.withoutDocs({
schema: [],
messages: {
change: "change {{ subject }} from {{ from }} to {{ to }}",
add: `add {{ subject }} to {{ to }}`,
},
},
defaultOptions: [],
Expand All @@ -47,6 +50,28 @@ const v23 = ESLintUtils.RuleCreator.withoutDocs({
data: { subject: "interface", from: node.name, to: "HeaderSecurity" },
fix: (fixer) => fixer.replaceText(node, "HeaderSecurity"),
}),
createConfig: (node) => {
const wmProp = node.properties.find(
(prop) =>
prop.type === NT.Property &&
prop.key.type === NT.Identifier &&
prop.key.name === "wrongMethodBehavior",
);
if (wmProp) return;
ctx.report({
node,
messageId: "add",
data: {
subject: "wrongMethodBehavior property",
to: "configuration",
},
fix: (fixer) =>
fixer.insertTextAfterRange(
[node.range[0], node.range[0] + 1],
"wrongMethodBehavior: 404,",
),
});
},
}),
});

Expand Down
2 changes: 1 addition & 1 deletion src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const initRouting = ({
app[method](path, ...matchingParsers, handler);
};
walkRouting({ routing, onEndpoint, onStatic: app.use.bind(app) });
if (config.wrongMethodBehavior !== 405) return;
if (config.wrongMethodBehavior === 404) return;
for (const [path, allowedMethods] of familiar.entries())
app.all(path, createWrongMethodHandler(allowedMethods));
};
1 change: 0 additions & 1 deletion tests/system/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ describe("App in production mode", async () => {
const config = createConfig({
http: { listen: port },
compression: { threshold: 1 },
wrongMethodBehavior: 405,
beforeRouting: ({ app, getLogger }) => {
depd("express")("Sample deprecation message");
app.use((req, {}, next) => {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/__snapshots__/migration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`Migration > should consist of one rule being the major version of the p
"meta": {
"fixable": "code",
"messages": {
"add": "add {{ subject }} to {{ to }}",
"change": "change {{ subject }} from {{ from }} to {{ to }}",
},
"schema": [],
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe("Migration", () => {
});

tester.run("v23", migration.rules.v23, {
valid: [`import { HeaderSecurity } from "express-zod-api";`],
valid: [
`import { HeaderSecurity } from "express-zod-api";`,
`createConfig({ wrongMethodBehavior: 405 });`,
],
invalid: [
{
code: `const security: CustomHeaderSecurity = {};`,
Expand All @@ -34,6 +37,19 @@ describe("Migration", () => {
},
],
},
{
code: `createConfig({});`,
output: `createConfig({wrongMethodBehavior: 404,});`,
errors: [
{
messageId: "add",
data: {
subject: "wrongMethodBehavior property",
to: "configuration",
},
},
],
},
],
});
});

0 comments on commit b394427

Please sign in to comment.