-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
link_filterer_test.go
108 lines (91 loc) · 1.87 KB
/
link_filterer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func newTestLinkFilterer() linkFilterer {
return newLinkFilterer(nil, nil)
}
func TestLinkFiltererIsLinkExcluded(t *testing.T) {
u, err := url.Parse("http://foo.com")
assert.Nil(t, err)
for _, x := range []struct {
regexps []string
answer bool
}{
{
[]string{"foo\\.com"},
false,
},
{
[]string{"foo"},
false,
},
{
[]string{"bar", "foo"},
false,
},
{
[]string{"bar"},
true,
},
} {
t.Run(fmt.Sprint(x.regexps), func(t *testing.T) {
rs, err := compileRegexps(x.regexps)
assert.Nil(t, err)
assert.Equal(t, x.answer, newLinkFilterer(rs, nil).IsValid(u))
})
}
}
func TestLinkFiltererIsLinkIncluded(t *testing.T) {
u, err := url.Parse("http://foo.com")
assert.Nil(t, err)
for _, x := range []struct {
regexps []string
answer bool
}{
{
[]string{"foo\\.com"},
true,
},
{
[]string{"foo"},
true,
},
{
[]string{"bar", "foo"},
true,
},
{
[]string{"bar"},
false,
},
} {
t.Run(fmt.Sprint(x.regexps), func(t *testing.T) {
rs, err := compileRegexps(x.regexps)
assert.Nil(t, err)
assert.Equal(t, x.answer, newLinkFilterer(nil, rs).IsValid(u))
})
}
}
func TestLinkFiltererExcludeEntireUrl(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
rs, err := compileRegexps([]string{"foo"})
assert.Nil(t, err)
assert.False(t, newLinkFilterer(rs, nil).IsValid(b))
}
func TestLinkFiltererIncludeEntireUrl(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
rs, err := compileRegexps([]string{"foo"})
assert.Nil(t, err)
assert.True(t, newLinkFilterer(nil, rs).IsValid(b))
}
func TestLinkFiltererExcludeInvalidScheme(t *testing.T) {
b, err := url.Parse("mailto:foo@bar.baz")
assert.Nil(t, err)
assert.False(t, newLinkFilterer(nil, nil).IsValid(b))
}