-
Notifications
You must be signed in to change notification settings - Fork 17
/
matchers_test.go
99 lines (81 loc) · 2.23 KB
/
matchers_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
package ant
import (
"net/url"
"regexp"
"testing"
"github.com/stretchr/testify/require"
)
func TestMatchers(t *testing.T) {
t.Run("hostname", func(t *testing.T) {
var cases = []struct {
rawurl string
pattern string
match bool
}{
{"https://foo.example.com", `example.com`, false},
{"https://example.com", `example.com`, true},
}
for _, c := range cases {
t.Run(c.rawurl, func(t *testing.T) {
var assert = require.New(t)
var match = MatchHostname(c.pattern)
u, err := url.Parse(c.rawurl)
assert.NoError(err)
assert.Equal(c.match, match.Match(u))
})
}
})
t.Run("pattern", func(t *testing.T) {
var cases = []struct {
rawurl string
pattern string
match bool
}{
{"http://example.com", `example.com`, true},
{"https://example.com", `example.com`, true},
{"https://foo.example.com", `*example.com`, true},
{"https://example.com/foo/baz", `example.com/foo/*`, true},
{"https://example.com", `example.com/foo/*`, false},
}
for _, c := range cases {
t.Run(c.rawurl, func(t *testing.T) {
var assert = require.New(t)
var match = MatchPattern(c.pattern)
u, err := url.Parse(c.rawurl)
assert.NoError(err)
assert.Equal(c.match, match.Match(u))
})
}
})
t.Run("regexp", func(t *testing.T) {
var cases = []struct {
rawurl string
pattern string
match bool
}{
{"http://example.com", regexp.QuoteMeta(`example.com`), true},
{"https://example.com", regexp.QuoteMeta(`example.com`), true},
{"https://example.com/foo/baz", regexp.QuoteMeta(`example.com`), true},
{"https://example.com/foo?query", regexp.QuoteMeta(`example.com/foo`), true},
{"https://google.com/search/car", regexp.QuoteMeta(`google.com/search/car`), true},
}
for _, c := range cases {
t.Run(c.rawurl, func(t *testing.T) {
var assert = require.New(t)
var match = MatchRegexp(c.pattern)
u, err := url.Parse(c.rawurl)
assert.NoError(err)
assert.Equal(c.match, match.Match(u), u)
})
}
})
t.Run("regexp error", func(t *testing.T) {
var assert = require.New(t)
defer func() {
err, ok := recover().(string)
assert.True(ok, "expected a panic")
assert.Contains(err, `ant: regexp "[" - error parsing`)
}()
MatchRegexp(`[`)
})
}