Skip to content

Commit

Permalink
added @match header validator & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oaphi committed Aug 9, 2021
1 parent 746b133 commit 0839cda
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const validateMatchHeaders = (matches: string[]) => {
const validationRegex =
/^((?:https?|file|ftp|\*)(?=:\/\/)|(?:urn(?=:))):(?:\/\/)?(?:((?:\*||.+?)(?=\/|$)))?(\/\*|(?:.+?\*?)+)?|<all_urls>/;
return matches.every((match) => validationRegex.test(match));
};
30 changes: 30 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";
import { validateMatchHeaders } from "../src/utils/validators";

describe("validators", () => {
describe("@match headers validator", () => {
const sampleMatches: string[] = [
"http://*/foo*",
"https://*.google.com/foo*bar",
"http://example.org/foo/bar.html",
"file:///foo*",
"http://127.0.0.1/*",
"*://mail.google.com/*",
"urn:*",
"<all_urls>",
];

it("should correctly validate valid headers", () => {
const valid = validateMatchHeaders(sampleMatches);
expect(valid).to.be.true;
});

it("should correctly validate invalid headers", () => {
const valid = validateMatchHeaders([
...sampleMatches,
"http//typo.ed/*",
]);
expect(valid).to.be.false;
});
});
});

0 comments on commit 0839cda

Please # to comment.