/
set_test.go
219 lines (179 loc) · 4.13 KB
/
set_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package suffix_test
import (
"bytes"
"fmt"
"log"
"net/http"
"strings"
"testing"
"github.com/tg/suffix"
)
func TestSet_Match(t *testing.T) {
var set suffix.Set
// add suffixes; multiple times to make sure this is safe
for n := 0; n < 3; n++ {
set.Add("redtube.com")
set.Add("bbc.co.uk")
set.Add("d.")
set.Add("a.b.c.d")
set.Add(".c.d")
set.Add("c.d.")
set.Add(".b.c.d")
set.Add(".subs")
set.Add(".subs.com")
set.Add("fixed.")
set.Add("fixed.com.")
set.Add(".both.com.") // equivalent to no edge dots
}
if size := set.Len(); size != 11 {
t.Error("invalid set size: ", size)
}
cases := []struct {
name string
suffix string
}{
{"redtube.com", "redtube.com"},
{"gang.redtube.com", "redtube.com"},
{"bang.gang.redtube.com", "redtube.com"},
{"d", "d"},
{"c.d", "c.d"},
{"x.d", ""},
{"c.d", "c.d"},
{"b.c.d", "c.d"},
{"a.b.c.d", "a.b.c.d"},
{"x.b.c.d", "b.c.d"},
{"x.x.c.d", "c.d"},
{"x.x.x.d", ""},
{"yellow.subs", "subs"},
{"yellow.subs.com", "subs.com"},
{"green.and.yellow.subs.com", "subs.com"},
{"fixed", "fixed"},
{"fixed.com", "fixed.com"},
{"both.com", "both.com"},
{"yes.both.com", "both.com"},
// Don't match these...
{"pinktube.com", ""},
{"edtube.com", ""},
{"com", ""},
{"bbc", ""},
{"co.uk", ""},
{"bbc.co", ""},
{"subs", ""},
{"subs.com", ""},
{"some.fixed", ""},
{"some.fixed.com", ""},
}
for _, c := range cases {
suffix := set.Match(c.name)
if suffix != c.suffix {
t.Errorf("%s expected suffix %q, got %q", c.name, c.suffix, suffix)
}
belongs := (suffix != "")
if b := set.Matches(c.name); b != belongs {
t.Error(c.name, b, " != ", belongs)
}
}
}
func TestSet_ReadFrom(t *testing.T) {
var set suffix.Set
data := `// This is a file full of suffixes
two.girls
# comment
one.cup `
expected := []string{"two.girls", "one.cup"}
set.ReadFrom(strings.NewReader(data))
if set.Len() != len(expected) {
t.Fatalf("Expected %d suffixes, got %d", len(expected), set.Len())
}
}
func TestSet_WriteTo(t *testing.T) {
var set suffix.Set
set.Add("com")
set.Add("google.com")
set.Add(".youtube.com")
set.Add("blog.golang.org.")
buf := &bytes.Buffer{}
set.WriteTo(buf)
s := buf.String()
expected := `.youtube.com
blog.golang.org.
com
google.com
`
if s != expected {
t.Errorf("Expected %q, got %q", expected, s)
}
}
func ExampleSet_ReadFrom_iana() {
r, err := http.Get("http://data.iana.org/TLD/tlds-alpha-by-domain.txt")
if err != nil {
log.Fatal(err)
}
var tlds suffix.Set
_, err = tlds.ReadFrom(r.Body)
if err != nil {
log.Fatal(err)
}
r.Body.Close()
fmt.Println(tlds.Match("MERRY.CHRISTMAS"))
// Output:
// CHRISTMAS
}
func ExampleSet_Split() {
var set suffix.Set
set.Add("com")
set.Add("blogspot.com")
fmt.Println(set.Split("bob.blogspot.com"))
fmt.Println(set.Split("blogspot.com"))
// Output:
// bob blogspot.com
// blogspot.com
}
func Example_plusOne() {
var set suffix.Set
set.Add("com")
set.Add("blogspot.com")
fmt.Println(suffix.PlusOne(&set, "bob.dylan.blogspot.com"))
fmt.Println(suffix.PlusOne(&set, "blogspot.com"))
// Output:
// dylan.blogspot.com
//
}
// Example_map shows how to create a mapping for suffixes added to Set.
func Example_map() {
var set suffix.Set
// tags will keep a list of tags for each possible match
tags := make(map[suffix.Match][]string)
// set up rules and add to set
rules := []struct {
Name string
Tag string
}{
// will match google.com and all subdomains
{"google.com", "google"},
// will match google.com exactly
{"google.com.", "toplevel"},
// will match only subdomains *.google.com
{".google.com", "subdomain"},
// will match api.google.com exactly
{"www.google.com.", "websearch"},
}
for _, rule := range rules {
for _, match := range set.Add(rule.Name) {
// create mapping between each match;
// matches are not unique between names, so we need to append.
tags[match] = append(tags[match], rule.Tag)
}
}
// get all tags for www.google.com
set.MatchAll("www.google.com", func(m suffix.Match) bool {
for _, tag := range tags[m] {
fmt.Println(tag)
}
return true
})
// Output:
// websearch
// google
// subdomain
}