Skip to content

Commit f3042c5

Browse files
authored
Make matchRegexExclusion testable and add unit tests (#282)
1 parent 2a1713b commit f3042c5

3 files changed

Lines changed: 87 additions & 4 deletions

File tree

internal/pkg/preprocessor/exclusion.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package preprocessor
22

33
import (
4-
"github.com/internetarchive/Zeno/internal/pkg/config"
4+
"regexp"
5+
56
"github.com/internetarchive/Zeno/pkg/models"
67
)
78

8-
func matchRegexExclusion(item *models.Item) bool {
9-
for _, exclusion := range config.Get().ExclusionRegexes {
9+
func matchRegexExclusion(ExclusionRegexes []*regexp.Regexp, item *models.Item) bool {
10+
for _, exclusion := range ExclusionRegexes {
1011
if exclusion.MatchString(item.GetURL().String()) {
1112
return true
1213
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

internal/pkg/preprocessor/preprocessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func preprocess(workerID string, seed *models.Item) {
200200
// Apply exclusion filters even if it passed inclusion
201201
if utils.StringContainsSliceElements(items[i].GetURL().GetParsed().Host, config.Get().ExcludeHosts) ||
202202
utils.StringContainsSliceElements(items[i].GetURL().String(), config.Get().ExcludeString) ||
203-
matchRegexExclusion(items[i]) {
203+
matchRegexExclusion(config.Get().ExclusionRegexes, items[i]) {
204204

205205
logger.Debug("URL excluded (matches exclusion filters)",
206206
"item_id", items[i].GetShortID(),

0 commit comments

Comments
 (0)