-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from lipsumar/add-wildcard-support-for-passth…
…rough Add wildcard support for passthrough
- Loading branch information
Showing
6 changed files
with
222 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function shouldPassThrough( | ||
hostname: string | undefined, | ||
// Only one of these two should have values (validated above): | ||
passThroughPatterns: URLPattern[], | ||
interceptOnlyPatterns: URLPattern[] | undefined | ||
): boolean { | ||
if (!hostname) return false; | ||
|
||
if (interceptOnlyPatterns) { | ||
return !interceptOnlyPatterns.some((pattern) => | ||
pattern.test(`https://${hostname}`) | ||
); | ||
} | ||
|
||
return passThroughPatterns.some((pattern) => | ||
pattern.test(`https://${hostname}`) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { URLPattern } from "urlpattern-polyfill"; | ||
import { expect } from "./test-utils"; | ||
import { shouldPassThrough } from "../src/util/server-utils"; | ||
|
||
describe("shouldPassThrough", () => { | ||
it("should return false when passThroughHostnames is empty and interceptOnlyHostnames is undefined", async () => { | ||
const should = shouldPassThrough("example.org", [], undefined); | ||
expect(should).to.be.false; | ||
}); | ||
|
||
it("should return true when both lists empty", async () => { | ||
const should = shouldPassThrough("example.org", [], []); | ||
expect(should).to.be.true; | ||
}); | ||
|
||
it("should return false when hostname is falsy", () => { | ||
const should = shouldPassThrough("", [], []); | ||
expect(should).to.be.false; | ||
}); | ||
|
||
describe("passThroughHostnames", () => { | ||
it("should return true when hostname is in passThroughHostnames", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[new URLPattern("https://example.org")], | ||
undefined | ||
); | ||
expect(should).to.be.true; | ||
}); | ||
|
||
it("should return false when hostname is not in passThroughHostnames", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[new URLPattern("https://example.com")], | ||
undefined | ||
); | ||
expect(should).to.be.false; | ||
}); | ||
|
||
it("should return true when hostname match a wildcard", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[new URLPattern("https://*.org")], | ||
undefined | ||
); | ||
expect(should).to.be.true; | ||
}); | ||
}); | ||
describe("interceptOnlyHostnames", () => { | ||
it("should return false when hostname is in interceptOnlyHostnames", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[], | ||
[new URLPattern("https://example.org")] | ||
); | ||
expect(should).to.be.false; | ||
}); | ||
|
||
it("should return true when hostname is not in interceptOnlyHostnames", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[], | ||
[new URLPattern("https://example.com")] | ||
); | ||
expect(should).to.be.true; | ||
}); | ||
|
||
it("should return false when hostname match a wildcard", () => { | ||
const should = shouldPassThrough( | ||
"example.org", | ||
[], | ||
[new URLPattern("https://*.org")] | ||
); | ||
expect(should).to.be.false; | ||
}); | ||
}); | ||
}); |