Skip to content

Commit

Permalink
Improve tests around various policies
Browse files Browse the repository at this point in the history
This (1) only does one `any` cast (2) tries to make the tests a little
more realistic; things someone might do by accident.
  • Loading branch information
EvanHahn committed Jun 25, 2022
1 parent 94a7468 commit 9b88c00
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
16 changes: 8 additions & 8 deletions test/cross-origin-embedder-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ describe("Cross-Origin-Embedder-Policy middleware", () => {
it("throws when setting the policy to an invalid value", () => {
const invalidValues = [
"",
"NONE",
"by-ftp-filename",
123 as any,
null as any,
new String("none") as any,
"foo",
"CREDENTIALLESS",
123,
null,
new String("credentialless"),
];
for (const policy of invalidValues) {
expect(() => crossOriginEmbedderPolicy({ policy })).toThrow(
/^Cross-Origin-Embedder-Policy does not support /
);
expect(() =>
crossOriginEmbedderPolicy({ policy: policy as any })
).toThrow(/^Cross-Origin-Embedder-Policy does not support /);
}
});
});
12 changes: 6 additions & 6 deletions test/cross-origin-opener-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe("Cross-Origin-Opener-Policy middleware", () => {
it("throws when setting the policy to an invalid value", () => {
const invalidValues = [
"",
"NONE",
"by-ftp-filename",
123 as any,
null as any,
new String("none") as any,
"foo",
"SAME-ORIGIN",
123,
null,
new String("same-origin"),
];
for (const policy of invalidValues) {
expect(() => crossOriginOpenerPolicy({ policy })).toThrow(
expect(() => crossOriginOpenerPolicy({ policy: policy as any })).toThrow(
/^Cross-Origin-Opener-Policy does not support /
);
}
Expand Down
16 changes: 8 additions & 8 deletions test/cross-origin-resource-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ describe("Cross-Origin-Resource-Policy middleware", () => {
it("throws when setting the policy to an invalid value", () => {
const invalidValues = [
"",
"NONE",
"by-ftp-filename",
123 as any,
null as any,
new String("none") as any,
"foo",
"CROSS-ORIGIN",
123,
null,
new String("none"),
];
for (const policy of invalidValues) {
expect(() => crossOriginResourcePolicy({ policy })).toThrow(
/^Cross-Origin-Resource-Policy does not support /
);
expect(() =>
crossOriginResourcePolicy({ policy: policy as any })
).toThrow(/^Cross-Origin-Resource-Policy does not support /);
}
});
});
10 changes: 4 additions & 6 deletions test/referrer-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ describe("Referrer-Policy middleware", () => {
});

it("fails with a bad policy", () => {
expect(referrerPolicy.bind(null, { policy: "garbage" })).toThrow();
expect(referrerPolicy.bind(null, { policy: "sameorigin" })).toThrow();
expect(referrerPolicy.bind(null, { policy: 123 as any })).toThrow();
expect(referrerPolicy.bind(null, { policy: false as any })).toThrow();
expect(referrerPolicy.bind(null, { policy: null as any })).toThrow();
expect(referrerPolicy.bind(null, { policy: {} as any })).toThrow();
const invalidValues = ["foo", "sameorigin", "ORIGIN", 123, false, null, {}];
for (const policy of invalidValues) {
expect(referrerPolicy.bind(null, { policy: policy as any })).toThrow();
}
});

it("fails with an empty array", () => {
Expand Down
11 changes: 9 additions & 2 deletions test/x-frame-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ describe("X-Frame-Options middleware", () => {
);
}

for (const action of ["garbage", "", 123 as any, null as any]) {
expect(() => xFrameOptions({ action })).toThrow(
for (const action of [
"",
"foo",
" deny",
123,
null,
new String("SAMEORIGIN"),
]) {
expect(() => xFrameOptions({ action: action as any })).toThrow(
/^X-Frame-Options received an invalid action /
);
}
Expand Down
10 changes: 6 additions & 4 deletions test/x-permitted-cross-domain-policies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ describe("X-Permitted-Cross-Domain-Policies middleware", () => {
"",
"NONE",
"by-ftp-filename",
123 as any,
null as any,
new String("none") as any,
123,
null,
new String("none"),
];
for (const permittedPolicies of invalidValues) {
expect(() =>
xPermittedCrossDomainPolicies({ permittedPolicies })
xPermittedCrossDomainPolicies({
permittedPolicies: permittedPolicies as any,
})
).toThrow(/^X-Permitted-Cross-Domain-Policies does not support /);
}
});
Expand Down

0 comments on commit 9b88c00

Please sign in to comment.