Skip to content

Commit

Permalink
feat: prevent runtime problems
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed Apr 16, 2023
1 parent cf7ac14 commit 118546a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/body-parser.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export function getIsEnabledBodyAs(enableTypes: BodyType[]) {
export function getMimeTypes(
extendTypes: NonNullable<BodyParserOptions['extendTypes']>,
) {
for (const extendedType of Object.keys(extendTypes) as BodyType[]) {
for (const extendedTypeKey of Object.keys(extendTypes) as BodyType[]) {
const extendedType = extendTypes[extendedTypeKey];

if (
!supportedBodyTypes.includes(extendedType) ||
!Array.isArray(extendTypes[extendedType])
!supportedBodyTypes.includes(extendedTypeKey) ||
!Array.isArray(extendedType)
) {
throw new UnsupportedBodyTypeError(extendedType);
throw new UnsupportedBodyTypeError(extendedTypeKey);
}
}

Expand Down
15 changes: 14 additions & 1 deletion test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe("test/body-parser.test.ts", () => {
}
});

it("should throw when pass supported type but with string value instead of array", () => {
it("should throw when pass supported types with string value instead of array", () => {
try {
createApp({
extendTypes: {
Expand All @@ -394,6 +394,18 @@ describe("test/body-parser.test.ts", () => {
expect(error instanceof UnsupportedBodyTypeError).toBe(true);
}
});

it("should throw when pass supported types with array contain falsy values", () => {
try {
createApp({
extendTypes: {
json: ["", 0, false, null, undefined],
} as any,
});
} catch (error) {
expect(error instanceof UnsupportedBodyTypeError).toBe(true);
}
});
});

describe("enableTypes", () => {
Expand Down Expand Up @@ -492,6 +504,7 @@ describe("test/body-parser.test.ts", () => {
.send({ foo: "bar" })
.expect(rawParsedBody, done);
});

it("shouldn't override koa request with raw request body if not exist and enableRawChecking is truthy", (done) => {
const rawParsedBody = undefined;
const app = createApp({ rawParsedBody, enableRawChecking: true });
Expand Down

0 comments on commit 118546a

Please sign in to comment.