@@ -5,28 +5,103 @@ describe('whitelist-holder', () => {
55
66 beforeEach ( ( ) => processor = new WhitelistProcessor ( ) )
77
8- test ( 'normal' , ( ) => {
9- processor . setWhitelist ( [
10- "www.google.com" ,
11- "github.com" ,
8+ const verify = ( whitelist : string [ ] , cases : Array < [ string , string , boolean ] > ) => {
9+ processor . setWhitelist ( whitelist )
10+ cases . forEach ( ( [ host , url , expected ] ) => expect ( processor . contains ( host , url ) ) . toBe ( expected ) )
11+ }
12+
13+ test ( 'setWhitelist basic' , ( ) => {
14+ processor . setWhitelist ( [ ] )
15+ expect ( processor . contains ( "github.com" , "https://github.com/" ) ) . toBeFalsy ( )
16+
17+ processor . setWhitelist ( [ '' , 'github.com' , '' , null as any , undefined as any ] )
18+ expect ( processor . contains ( "github.com" , "https://github.com/" ) ) . toBeTruthy ( )
19+
20+ processor . setWhitelist ( [ 'google.com' ] )
21+ expect ( processor . contains ( "github.com" , "https://github.com/" ) ) . toBeFalsy ( )
22+ expect ( processor . contains ( "google.com" , "https://google.com/" ) ) . toBeTruthy ( )
23+ } )
24+
25+ test ( 'normal hosts' , ( ) => {
26+ verify ( [ "www.google.com" , "github.com" ] , [
27+ [ "github.com" , "" , true ] ,
28+ [ "github.com" , "https://unrelated.com/" , true ] , // URL ignored
29+ [ "www.google.com" , "http://www.google.com/search" , true ] ,
30+ [ "www.github.com" , "https://www.github.com/" , false ] ,
31+ [ "google.com" , "https://google.com/" , false ] ,
32+ ] )
33+ } )
34+
35+ test ( 'virtual hosts with wildcards' , ( ) => {
36+ verify ( [ "github.com/*" , "*.google.com/**" , "*.example.com/path/*" ] , [
37+ // Single wildcard: matches one level
38+ [ "" , "https://github.com/sheepzh" , true ] ,
39+ [ "" , "https://github.com" , false ] ,
40+ [ "" , "https://github.com/sheepzh/timer" , false ] ,
41+ // Host wildcard
42+ [ "" , "http://map.google.com/search" , true ] ,
43+ [ "" , "http://foo.bar.google.com/path" , true ] ,
44+ [ "" , "https://google.com/" , false ] ,
45+ // Complex pattern
46+ [ "" , "https://sub.example.com/path/to" , true ] ,
47+ [ "" , "https://sub.example.com/path/to/nested" , false ] ,
48+ ] )
49+
50+ verify ( [ "gitlab.com/**" ] , [
51+ // Double wildcard: matches any depth
52+ [ "" , "https://gitlab.com/" , true ] ,
53+ [ "" , "https://gitlab.com/group/project/issues" , true ] ,
54+ ] )
55+ } )
56+
57+ test ( 'exclude patterns' , ( ) => {
58+ verify (
59+ [ "github.com" , "*.google.com/**" , "+github.com/login" , "+www.google.com/**" ] ,
60+ [
61+ [ "github.com" , "https://github.com/" , true ] ,
62+ [ "github.com" , "https://github.com/login" , false ] ,
63+ [ "" , "http://map.google.com/search" , true ] ,
64+ [ "" , "https://www.google.com/search" , false ] ,
65+ ]
66+ )
67+
68+ verify ( [ "example.com/**" , "+example.com/admin/**" , "+example.com/login" ] , [
69+ [ "" , "https://example.com/page" , true ] ,
70+ [ "" , "https://example.com/login" , false ] ,
71+ [ "" , "https://example.com/admin/dashboard" , false ] ,
1272 ] )
13- expect ( processor . contains ( "github.com" , "" ) ) . toBeTruthy ( )
14- expect ( processor . contains ( "www.github.com" , "" ) ) . toBeFalsy ( )
15- expect ( processor . contains ( "www.google.com" , "http://www.google.com/search" ) ) . toBeTruthy ( )
1673 } )
1774
18- test ( 'wildcards' , ( ) => {
19- processor . setWhitelist ( [
20- "www.github.com" ,
21- "*.google.com/**" ,
22- "+www.google.com/**" ,
75+ test ( 'mixed patterns' , ( ) => {
76+ verify ( [ "example.com" , "test.com/**" , "github.com" , "*.example.com/**" ] , [
77+ // Normal host: only checks host param
78+ [ "example.com" , "https://other.com/" , true ] ,
79+ [ "other.com" , "https://example.com/" , false ] ,
80+ // Virtual host: checks URL
81+ [ "" , "https://test.com/path" , true ] ,
82+ [ "" , "https://other.com/path" , false ] ,
83+ // Empty host param: only virtual patterns work
84+ [ "" , "https://github.com/" , false ] ,
85+ [ "" , "https://sub.example.com/path" , true ] ,
2386 ] )
24- expect ( processor . contains ( "google.com" , "https://google.com/" ) ) . toBeFalsy ( )
25- expect ( processor . contains ( "" , "http://map.google.com/search" ) ) . toBeTruthy ( )
87+ } )
88+
89+ test ( 'edge cases' , ( ) => {
90+ // Trailing slashes, special chars, duplicates, long list
91+ verify ( [ "github.com/**" , "example.com/path-with-dash/**" ] , [
92+ [ "" , "https://github.com/" , true ] ,
93+ [ "" , "https://github.com" , true ] ,
94+ [ "" , "https://example.com/path-with-dash/page" , true ] ,
95+ [ "" , "https://example.com/path_with_dash/page" , false ] ,
96+ ] )
97+
98+ processor . setWhitelist ( [ 'github.com' , 'github.com' ] )
99+ expect ( processor . contains ( "github.com" , "" ) ) . toBeTruthy ( )
26100
27- // virtual sites only use url
28- expect ( processor . contains ( "www.google.com" , "https://foo.bar.google.com/" ) ) . toBeTruthy ( )
29- // hit "+www.google.com/**"
30- expect ( processor . contains ( "www.google.com" , "https://www.google.com/" ) ) . toBeFalsy ( )
101+ const longList = Array . from ( { length : 100 } , ( _ , i ) => `site${ i } .com` )
102+ processor . setWhitelist ( longList )
103+ expect ( processor . contains ( "site0.com" , "" ) ) . toBeTruthy ( )
104+ expect ( processor . contains ( "site99.com" , "" ) ) . toBeTruthy ( )
105+ expect ( processor . contains ( "site100.com" , "" ) ) . toBeFalsy ( )
31106 } )
32107} )
0 commit comments