|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { UrlMatch } from "./match"; |
| 2 | +import { isUrlExcluded, isUrlIncluded, UrlMatch } from "./match"; |
3 | 3 | import { v4 as uuidv4 } from "uuid"; |
4 | 4 | import { extractUrlPatterns } from "./url_matcher"; |
5 | 5 |
|
@@ -177,7 +177,7 @@ describe.concurrent("UrlMatch-google", () => { |
177 | 177 | url.addMatch("https://example.org/foo/bar.html", "ok4"); |
178 | 178 | url.addMatch("http://127.0.0.1/*", "ok5"); |
179 | 179 | url.addMatch("*://mail.google.com/*", "ok6"); |
180 | | - url.exclude("https://example-2.org/foo/bar.html", "ok1"); |
| 180 | + url.addExclude("https://example-2.org/foo/bar.html", "ok1"); |
181 | 181 | it.concurrent("match1", () => { |
182 | 182 | expect(url.urlMatch("https://www.google.com/")).toEqual(["ok1"]); |
183 | 183 | expect(url.urlMatch("https://example.org/foo/bar.html")).toEqual(["ok1", "ok2", "ok4"]); |
@@ -386,7 +386,7 @@ describe.concurrent("UrlMatch-exclude", () => { |
386 | 386 | it.concurrent("exclue-port", () => { |
387 | 387 | const url = new UrlMatch<string>(); |
388 | 388 | url.addInclude("*://*/*", "ok3"); |
389 | | - url.exclude("*:5244*", "ok3"); |
| 389 | + url.addExclude("*:5244*", "ok3"); |
390 | 390 | expect(url.urlMatch("http://test.list.ggnb.top:5244/search")).toEqual([]); |
391 | 391 | expect(url.urlMatch("http://test.list.ggnb.top:80/search")).toEqual(["ok3"]); |
392 | 392 | }); |
@@ -704,9 +704,9 @@ describe.concurrent("UrlInclude-1", () => { |
704 | 704 | url.clearRules("ok10"); |
705 | 705 | url.addMatch("*://*.x.com/*", "ok10"); // @match *://*.x.com/* |
706 | 706 | expect(url.urlMatch("https://x.com/trump_chinese")).toEqual(["ok10"]); // 与TM一致 |
707 | | - url.exclude("*://*.x.com/*", "ok10"); // @exclude *://*.x.com/* |
| 707 | + url.addExclude("*://*.x.com/*", "ok10"); // @exclude *://*.x.com/* |
708 | 708 | expect(url.urlMatch("https://x.com/trump_chinese")).toEqual(["ok10"]); // 与TM一致 |
709 | | - url.exclude("*://*x.com/*", "ok10"); // @exclude *://*x.com/* |
| 709 | + url.addExclude("*://*x.com/*", "ok10"); // @exclude *://*x.com/* |
710 | 710 | expect(url.urlMatch("https://x.com/trump_chinese")).toEqual([]); // 与TM一致 |
711 | 711 | }); |
712 | 712 |
|
@@ -832,3 +832,60 @@ describe.concurrent("@include * (all)", () => { |
832 | 832 | expect(url.urlMatch("http://109.70.80.1:40/?#page")).toEqual(["ok1"]); |
833 | 833 | }); |
834 | 834 | }); |
| 835 | + |
| 836 | +describe.concurrent("urlExclude urlMatch 1", () => { |
| 837 | + const url = new UrlMatch<string>(); |
| 838 | + url.addInclude("*://*.example.com/*", "ok1"); |
| 839 | + url.addExclude("*://sub.example.com/*", "ok1"); |
| 840 | + it.concurrent("exclude-subdomain", () => { |
| 841 | + expect(isUrlExcluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 842 | + expect(isUrlExcluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 843 | + |
| 844 | + expect(isUrlIncluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 845 | + expect(isUrlIncluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 846 | + }); |
| 847 | +}); |
| 848 | + |
| 849 | +describe.concurrent("@exclude /REGEX/", () => { |
| 850 | + const url = new UrlMatch<string>(); |
| 851 | + url.addInclude("*://*.dummy1.com/*", "ok1"); |
| 852 | + url.addExclude("*://sub.dummy1.com/*", "ok1"); |
| 853 | + url.addInclude("*://*.dummy2.com/*", "ok2"); |
| 854 | + url.addExclude("*://sub.dummy2.com/*", "ok2"); |
| 855 | + url.addInclude("*://*.example.com/*", "ok1"); |
| 856 | + url.addExclude("*://sub.example.com/*", "ok1"); |
| 857 | + url.addExclude("/h\\d\\.example\\.com/", "ok1"); |
| 858 | + it.concurrent("test R1", () => { |
| 859 | + expect(isUrlExcluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 860 | + expect(isUrlExcluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 861 | + expect(isUrlExcluded("http://h7.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 862 | + expect(isUrlExcluded("http://hl.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 863 | + |
| 864 | + expect(isUrlIncluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 865 | + expect(isUrlIncluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 866 | + expect(isUrlIncluded("http://h7.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 867 | + expect(isUrlIncluded("http://hl.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 868 | + }); |
| 869 | +}); |
| 870 | + |
| 871 | +describe.concurrent("@include /REGEX/", () => { |
| 872 | + const url = new UrlMatch<string>(); |
| 873 | + url.addInclude("*://*.dummy1.com/*", "ok1"); |
| 874 | + url.addExclude("*://sub.dummy1.com/*", "ok1"); |
| 875 | + url.addInclude("*://*.dummy2.com/*", "ok2"); |
| 876 | + url.addExclude("*://sub.dummy2.com/*", "ok2"); |
| 877 | + url.addInclude("*://*.example.com/*", "ok1"); |
| 878 | + url.addExclude("*://sub.example.com/*", "ok1"); |
| 879 | + url.addInclude("/\\.h\\dample\\.com/", "ok1"); |
| 880 | + it.concurrent("test R2", () => { |
| 881 | + expect(isUrlExcluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 882 | + expect(isUrlExcluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 883 | + expect(isUrlExcluded("http://www.h7ample.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 884 | + expect(isUrlExcluded("http://www.hlample.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 885 | + |
| 886 | + expect(isUrlIncluded("http://www.example.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 887 | + expect(isUrlIncluded("http://sub.example.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 888 | + expect(isUrlIncluded("http://www.h7ample.com/", url.rulesMap.get("ok1")!)).toEqual(true); |
| 889 | + expect(isUrlIncluded("http://www.hlample.com/", url.rulesMap.get("ok1")!)).toEqual(false); |
| 890 | + }); |
| 891 | +}); |
0 commit comments