|
| 1 | +package preprocessor |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/internetarchive/Zeno/pkg/models" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMatchRegexExclusion(t *testing.T) { |
| 12 | + exclusionRegex := []string{ |
| 13 | + `(?i)^https?://(www\.)?archive-it\.org.*`, |
| 14 | + `(?i)^https?://(www\.)?x\.com.*`, |
| 15 | + `^https?://127\.0\.`, |
| 16 | + `^https?://192\.168\.`, |
| 17 | + `(?i)https?://[^/]+/wp-admin/`, |
| 18 | + `(?i)^(mailto|sms|tel|data|javascript):`, |
| 19 | + } |
| 20 | + var regexps []*regexp.Regexp |
| 21 | + for _, r := range exclusionRegex { |
| 22 | + re, err := regexp.Compile(r) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to compile regex %q: %v", r, err) |
| 25 | + } |
| 26 | + regexps = append(regexps, re) |
| 27 | + } |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + itemURL string |
| 32 | + expectedMatched bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "Match localhost IP", |
| 36 | + itemURL: "http://127.0.0.1/details/testitem", |
| 37 | + expectedMatched: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Match x.com post with HTTP", |
| 41 | + itemURL: "HTTPS://x.com:/loukoumi07/status/1922747849671934061", |
| 42 | + expectedMatched: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Match foo.com wp-admin", |
| 46 | + itemURL: "https://foo.com/wp-admin/something", |
| 47 | + expectedMatched: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Match mailto: uppercase link", |
| 51 | + itemURL: "MAILTO:someone@foo.com", |
| 52 | + expectedMatched: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Match tel: link", |
| 56 | + itemURL: "tel:0090567854", |
| 57 | + expectedMatched: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "No match", |
| 61 | + itemURL: "https://archive.org/details/testitem", |
| 62 | + expectedMatched: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "No match", |
| 66 | + itemURL: "https://something.org/details/wp-admintestitem", |
| 67 | + expectedMatched: false, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + parsedURL := &models.URL{Raw: tt.itemURL} |
| 74 | + parsedURL.Parse() |
| 75 | + item := models.NewItem(uuid.New().String(), parsedURL, "") |
| 76 | + got := matchRegexExclusion(regexps, item) |
| 77 | + if got != tt.expectedMatched { |
| 78 | + t.Errorf("Expected match: %v, got: %v", tt.expectedMatched, got) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments