public_suffix 1.5.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -0
- data/.rubocop_defaults.yml +127 -0
- data/.travis.yml +10 -5
- data/2.0-Upgrade.md +35 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +7 -1
- data/LICENSE.txt +1 -1
- data/README.md +89 -62
- data/Rakefile +23 -20
- data/data/{definitions.txt → list.txt} +472 -286
- data/lib/public_suffix.rb +96 -52
- data/lib/public_suffix/domain.rb +26 -156
- data/lib/public_suffix/errors.rb +5 -17
- data/lib/public_suffix/list.rb +107 -122
- data/lib/public_suffix/rule.rb +169 -190
- data/lib/public_suffix/version.rb +3 -13
- data/public_suffix.gemspec +4 -4
- data/test/acceptance_test.rb +57 -34
- data/test/benchmark_helper.rb +4 -0
- data/test/execution_profiler.rb +14 -0
- data/test/initialization_profiler.rb +11 -0
- data/test/performance_benchmark.rb +38 -0
- data/test/psl_test.rb +49 -0
- data/test/test_helper.rb +12 -5
- data/test/tests.txt +98 -0
- data/test/unit/domain_test.rb +18 -84
- data/test/unit/errors_test.rb +2 -2
- data/test/unit/list_test.rb +131 -59
- data/test/unit/public_suffix_test.rb +105 -34
- data/test/unit/rule_test.rb +52 -135
- metadata +20 -6
- data/.gemtest +0 -0
data/test/unit/errors_test.rb
CHANGED
data/test/unit/list_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class PublicSuffix::ListTest < Minitest::Unit::TestCase
|
4
4
|
|
@@ -13,20 +13,13 @@ class PublicSuffix::ListTest < Minitest::Unit::TestCase
|
|
13
13
|
|
14
14
|
def test_initialize
|
15
15
|
assert_instance_of PublicSuffix::List, @list
|
16
|
-
assert_equal 0, @list.
|
16
|
+
assert_equal 0, @list.size
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def test_initialize_indexes
|
20
20
|
assert_equal({}, @list.indexes)
|
21
21
|
end
|
22
22
|
|
23
|
-
def test_indexes
|
24
|
-
assert !list.indexes.empty?
|
25
|
-
assert_equal [1,2,3,4], list.indexes.delete('uk')
|
26
|
-
assert_equal [0], list.indexes.delete('com')
|
27
|
-
assert list.indexes.empty?
|
28
|
-
end
|
29
|
-
|
30
23
|
|
31
24
|
def test_equality_with_self
|
32
25
|
list = PublicSuffix::List.new
|
@@ -41,13 +34,13 @@ class PublicSuffix::ListTest < Minitest::Unit::TestCase
|
|
41
34
|
def test_add
|
42
35
|
assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
|
43
36
|
assert_equal @list, @list << PublicSuffix::Rule.factory("")
|
44
|
-
assert_equal 2, @list.
|
37
|
+
assert_equal 2, @list.size
|
45
38
|
end
|
46
39
|
|
47
40
|
def test_add_should_recreate_index
|
48
41
|
@list = PublicSuffix::List.parse("com")
|
49
42
|
assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
|
50
|
-
assert_equal
|
43
|
+
assert_equal @list.default_rule, @list.find("google.net")
|
51
44
|
|
52
45
|
@list << PublicSuffix::Rule.factory("net")
|
53
46
|
assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
|
@@ -62,57 +55,110 @@ class PublicSuffix::ListTest < Minitest::Unit::TestCase
|
|
62
55
|
end
|
63
56
|
|
64
57
|
def test_empty?
|
65
|
-
assert
|
58
|
+
assert @list.empty?
|
66
59
|
@list.add(PublicSuffix::Rule.factory(""))
|
67
60
|
assert !@list.empty?
|
68
61
|
end
|
69
62
|
|
70
63
|
def test_size
|
71
|
-
assert_equal 0, @list.
|
64
|
+
assert_equal 0, @list.size
|
72
65
|
assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
|
73
|
-
assert_equal 1, @list.
|
66
|
+
assert_equal 1, @list.size
|
74
67
|
end
|
75
68
|
|
76
69
|
def test_clear
|
77
|
-
assert_equal 0, @list.
|
70
|
+
assert_equal 0, @list.size
|
78
71
|
assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
|
79
|
-
assert_equal 1, @list.
|
72
|
+
assert_equal 1, @list.size
|
80
73
|
assert_equal @list, @list.clear
|
81
|
-
assert_equal 0, @list.
|
74
|
+
assert_equal 0, @list.size
|
82
75
|
end
|
83
76
|
|
84
77
|
|
85
78
|
def test_find
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
79
|
+
list = PublicSuffix::List.parse(<<EOS)
|
80
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
81
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
82
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
83
|
+
|
84
|
+
// ===BEGIN ICANN DOMAINS===
|
85
|
+
|
86
|
+
// com
|
87
|
+
com
|
88
|
+
|
89
|
+
// uk
|
90
|
+
*.uk
|
91
|
+
*.sch.uk
|
92
|
+
!bl.uk
|
93
|
+
!british-library.uk
|
94
|
+
|
95
|
+
// io
|
96
|
+
io
|
97
|
+
|
98
|
+
// ===END ICANN DOMAINS===
|
99
|
+
// ===BEGIN PRIVATE DOMAINS===
|
100
|
+
|
101
|
+
// Google, Inc.
|
102
|
+
blogspot.com
|
103
|
+
|
104
|
+
// ===END PRIVATE DOMAINS===
|
105
|
+
EOS
|
106
|
+
|
107
|
+
# match IANA
|
108
|
+
assert_equal PublicSuffix::Rule.factory("com"), list.find("example.com")
|
109
|
+
assert_equal PublicSuffix::Rule.factory("com"), list.find("foo.example.com")
|
110
|
+
|
111
|
+
# match wildcard
|
112
|
+
assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("example.uk")
|
113
|
+
assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("example.co.uk")
|
114
|
+
assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("foo.example.co.uk")
|
115
|
+
|
116
|
+
# match exception
|
91
117
|
assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("british-library.uk")
|
92
118
|
assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("foo.british-library.uk")
|
119
|
+
|
120
|
+
# match default rule
|
121
|
+
assert_equal PublicSuffix::Rule.factory("*"), list.find("test")
|
122
|
+
assert_equal PublicSuffix::Rule.factory("*"), list.find("example.test")
|
123
|
+
assert_equal PublicSuffix::Rule.factory("*"), list.find("foo.example.test")
|
124
|
+
|
125
|
+
# match private
|
126
|
+
assert_equal PublicSuffix::Rule.factory("blogspot.com", private: true), list.find("blogspot.com")
|
127
|
+
assert_equal PublicSuffix::Rule.factory("blogspot.com", private: true), list.find("foo.blogspot.com")
|
93
128
|
end
|
94
129
|
|
130
|
+
|
95
131
|
def test_select
|
96
132
|
assert_equal 2, list.select("british-library.uk").size
|
97
133
|
end
|
98
134
|
|
99
|
-
def
|
135
|
+
def test_select_name_blank
|
100
136
|
assert_equal [], list.select(nil)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_select_returns_empty_when_domain_is_blank
|
104
137
|
assert_equal [], list.select("")
|
105
|
-
assert_equal [], list.select("
|
138
|
+
assert_equal [], list.select(" ")
|
106
139
|
end
|
107
140
|
|
108
|
-
def
|
109
|
-
|
110
|
-
|
141
|
+
def test_select_ignore_private
|
142
|
+
list = PublicSuffix::List.new
|
143
|
+
list.add r1 = PublicSuffix::Rule.factory("io")
|
144
|
+
list.add r2 = PublicSuffix::Rule.factory("example.io", private: true)
|
145
|
+
|
146
|
+
assert_equal list.select("foo.io"), [r1]
|
147
|
+
assert_equal list.select("example.io"), [r1, r2]
|
148
|
+
assert_equal list.select("foo.example.io"), [r1, r2]
|
149
|
+
|
150
|
+
assert_equal list.select("foo.io", ignore_private: false), [r1]
|
151
|
+
assert_equal list.select("example.io", ignore_private: false), [r1, r2]
|
152
|
+
assert_equal list.select("foo.example.io", ignore_private: false), [r1, r2]
|
153
|
+
|
154
|
+
assert_equal list.select("foo.io", ignore_private: true), [r1]
|
155
|
+
assert_equal list.select("example.io", ignore_private: true), [r1]
|
156
|
+
assert_equal list.select("foo.example.io", ignore_private: true), [r1]
|
111
157
|
end
|
112
158
|
|
113
159
|
|
114
160
|
def test_self_default_getter
|
115
|
-
assert_equal
|
161
|
+
assert_equal nil, PublicSuffix::List.class_eval { @default }
|
116
162
|
PublicSuffix::List.default
|
117
163
|
assert_not_equal nil, PublicSuffix::List.class_eval { @default }
|
118
164
|
end
|
@@ -121,54 +167,80 @@ class PublicSuffix::ListTest < Minitest::Unit::TestCase
|
|
121
167
|
PublicSuffix::List.default
|
122
168
|
assert_not_equal nil, PublicSuffix::List.class_eval { @default }
|
123
169
|
PublicSuffix::List.default = nil
|
124
|
-
assert_equal
|
170
|
+
assert_equal nil, PublicSuffix::List.class_eval { @default }
|
125
171
|
end
|
126
172
|
|
127
173
|
def test_self_clear
|
128
174
|
PublicSuffix::List.default
|
129
175
|
assert_not_equal nil, PublicSuffix::List.class_eval { @default }
|
130
176
|
PublicSuffix::List.clear
|
131
|
-
assert_equal
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_self_reload
|
135
|
-
PublicSuffix::List.default
|
136
|
-
PublicSuffix::List.expects(:default_definition).returns("")
|
137
|
-
|
138
|
-
PublicSuffix::List.reload
|
139
|
-
assert_equal PublicSuffix::List.new, PublicSuffix::List.default
|
177
|
+
assert_equal nil, PublicSuffix::List.class_eval { @default }
|
140
178
|
end
|
141
179
|
|
142
180
|
def test_self_parse
|
143
181
|
list = PublicSuffix::List.parse(<<EOS)
|
144
|
-
//
|
145
|
-
//
|
146
|
-
//
|
147
|
-
// ***** END LICENSE BLOCK *****
|
182
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
183
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
184
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
148
185
|
|
149
|
-
//
|
150
|
-
ac
|
151
|
-
com.ac
|
186
|
+
// ===BEGIN ICANN DOMAINS===
|
152
187
|
|
153
|
-
//
|
154
|
-
|
188
|
+
// com
|
189
|
+
com
|
155
190
|
|
156
|
-
//
|
157
|
-
*.
|
158
|
-
!
|
191
|
+
// uk
|
192
|
+
*.uk
|
193
|
+
!british-library.uk
|
194
|
+
|
195
|
+
// ===END ICANN DOMAINS===
|
196
|
+
// ===BEGIN PRIVATE DOMAINS===
|
197
|
+
|
198
|
+
// Google, Inc.
|
199
|
+
blogspot.com
|
200
|
+
|
201
|
+
// ===END PRIVATE DOMAINS===
|
159
202
|
EOS
|
160
203
|
|
161
204
|
assert_instance_of PublicSuffix::List, list
|
162
|
-
assert_equal
|
163
|
-
|
205
|
+
assert_equal 4, list.size
|
206
|
+
|
207
|
+
rules = %w( com *.uk !british-library.uk blogspot.com ).map { |name| PublicSuffix::Rule.factory(name) }
|
208
|
+
assert_equal rules, list.to_a
|
209
|
+
|
210
|
+
# private domains
|
211
|
+
assert_equal false, list.find("com").private
|
212
|
+
assert_equal true, list.find("blogspot.com").private
|
164
213
|
end
|
165
214
|
|
166
|
-
def
|
167
|
-
|
215
|
+
def test_self_parse_indexes
|
216
|
+
list = PublicSuffix::List.parse(<<EOS)
|
217
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
218
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
219
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
220
|
+
|
221
|
+
// ===BEGIN ICANN DOMAINS===
|
222
|
+
|
223
|
+
// com
|
224
|
+
com
|
225
|
+
|
226
|
+
// uk
|
227
|
+
*.uk
|
228
|
+
!british-library.uk
|
229
|
+
|
230
|
+
// ===END ICANN DOMAINS===
|
231
|
+
// ===BEGIN PRIVATE DOMAINS===
|
232
|
+
|
233
|
+
// Google, Inc.
|
234
|
+
blogspot.com
|
235
|
+
|
236
|
+
// ===END PRIVATE DOMAINS===
|
237
|
+
EOS
|
238
|
+
|
239
|
+
assert_equal({ "com" => [0, 3], "uk" => [1, 2] }, list.indexes)
|
168
240
|
end
|
169
241
|
|
170
242
|
|
171
|
-
private
|
243
|
+
private
|
172
244
|
|
173
245
|
def list
|
174
246
|
@_list ||= PublicSuffix::List.parse(<<EOS)
|
@@ -1,7 +1,22 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class PublicSuffixTest < Minitest::Unit::TestCase
|
4
4
|
|
5
|
+
def test_private_domains_enabled_by_default
|
6
|
+
domain = PublicSuffix.parse("www.example.blogspot.com")
|
7
|
+
assert_equal "blogspot.com", domain.tld
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_private_domains_disable
|
11
|
+
data = File.read(PublicSuffix::List::DEFAULT_LIST_PATH)
|
12
|
+
PublicSuffix::List.default = PublicSuffix::List.parse(data, private_domains: false)
|
13
|
+
domain = PublicSuffix.parse("www.example.blogspot.com")
|
14
|
+
assert_equal "com", domain.tld
|
15
|
+
ensure
|
16
|
+
PublicSuffix::List.clear
|
17
|
+
end
|
18
|
+
|
19
|
+
|
5
20
|
def test_self_parse_a_domain_with_tld_and_sld
|
6
21
|
domain = PublicSuffix.parse("example.com")
|
7
22
|
assert_instance_of PublicSuffix::Domain, domain
|
@@ -44,7 +59,7 @@ class PublicSuffixTest < Minitest::Unit::TestCase
|
|
44
59
|
assert_equal "one.two", domain.trd
|
45
60
|
end
|
46
61
|
|
47
|
-
def
|
62
|
+
def test_self_parse_name_fqdn
|
48
63
|
domain = PublicSuffix.parse("www.example.com.")
|
49
64
|
assert_instance_of PublicSuffix::Domain, domain
|
50
65
|
assert_equal "com", domain.tld
|
@@ -52,44 +67,33 @@ class PublicSuffixTest < Minitest::Unit::TestCase
|
|
52
67
|
assert_equal "www", domain.trd
|
53
68
|
end
|
54
69
|
|
55
|
-
def
|
56
|
-
domain = PublicSuffix.parse("www.example.blogspot.com")
|
57
|
-
assert_equal "blogspot.com", domain.tld
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_disable_support_for_private_domains
|
61
|
-
begin
|
62
|
-
PublicSuffix::List.private_domains = false
|
63
|
-
domain = PublicSuffix.parse("www.example.blogspot.com")
|
64
|
-
assert_equal "com", domain.tld
|
65
|
-
ensure
|
66
|
-
PublicSuffix::List.private_domains = true
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_self_parse_a_domain_with_custom_list
|
70
|
+
def test_self_parse_with_custom_list
|
71
71
|
list = PublicSuffix::List.new
|
72
72
|
list << PublicSuffix::Rule.factory("test")
|
73
73
|
|
74
|
-
domain = PublicSuffix.parse("www.example.test", list)
|
74
|
+
domain = PublicSuffix.parse("www.example.test", list: list)
|
75
|
+
assert_instance_of PublicSuffix::Domain, domain
|
75
76
|
assert_equal "test", domain.tld
|
76
77
|
assert_equal "example", domain.sld
|
77
78
|
assert_equal "www", domain.trd
|
78
79
|
end
|
79
80
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
81
|
+
def test_self_parse_with_notlisted_name
|
82
|
+
domain = PublicSuffix.parse("example.tldnotlisted")
|
83
|
+
assert_instance_of PublicSuffix::Domain, domain
|
84
|
+
assert_equal "tldnotlisted", domain.tld
|
85
|
+
assert_equal "example", domain.sld
|
86
|
+
assert_equal nil, domain.trd
|
83
87
|
end
|
84
88
|
|
85
|
-
def
|
89
|
+
def test_self_parse_with_unallowed_domain
|
86
90
|
error = assert_raises(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.ke") }
|
87
|
-
assert_match
|
91
|
+
assert_match(/example\.ke/, error.message)
|
88
92
|
end
|
89
93
|
|
90
|
-
def
|
94
|
+
def test_self_parse_with_uri
|
91
95
|
error = assert_raises(PublicSuffix::DomainInvalid) { PublicSuffix.parse("http://google.com") }
|
92
|
-
assert_match
|
96
|
+
assert_match(%r{http://google\.com}, error.message)
|
93
97
|
end
|
94
98
|
|
95
99
|
|
@@ -100,16 +104,83 @@ class PublicSuffixTest < Minitest::Unit::TestCase
|
|
100
104
|
assert PublicSuffix.valid?("www.google.co.uk")
|
101
105
|
end
|
102
106
|
|
103
|
-
|
104
|
-
|
105
|
-
assert
|
106
|
-
|
107
|
+
def test_self_valid_with_notlisted_name
|
108
|
+
assert PublicSuffix.valid?("google.tldnotlisted")
|
109
|
+
assert PublicSuffix.valid?("www.google.tldnotlisted")
|
110
|
+
end
|
111
|
+
|
112
|
+
# def test_self_valid_with_fully_qualified_domain_name
|
113
|
+
# assert PublicSuffix.valid?("google.com.")
|
114
|
+
# assert PublicSuffix.valid?("google.co.uk.")
|
115
|
+
# assert !PublicSuffix.valid?("google.tldnotlisted.")
|
116
|
+
# end
|
117
|
+
|
118
|
+
|
119
|
+
def test_self_domain
|
120
|
+
assert_equal "google.com", PublicSuffix.domain("google.com")
|
121
|
+
assert_equal "google.com", PublicSuffix.domain("www.google.com")
|
122
|
+
assert_equal "google.co.uk", PublicSuffix.domain("google.co.uk")
|
123
|
+
assert_equal "google.co.uk", PublicSuffix.domain("www.google.co.uk")
|
107
124
|
end
|
108
125
|
|
109
|
-
def
|
110
|
-
|
111
|
-
|
112
|
-
|
126
|
+
def test_self_domain_with_notlisted_name
|
127
|
+
assert_equal "example.tldnotlisted", PublicSuffix.domain("example.tldnotlisted")
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_self_domain_with_unallowed_name
|
131
|
+
assert_nil PublicSuffix.domain("example.ke")
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_self_domain_with_blank_sld
|
135
|
+
assert_nil PublicSuffix.domain("com")
|
136
|
+
assert_nil PublicSuffix.domain(".com")
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
def test_self_normalize
|
141
|
+
[
|
142
|
+
["com", "com"],
|
143
|
+
["example.com", "example.com"],
|
144
|
+
["www.example.com", "www.example.com"],
|
145
|
+
|
146
|
+
["example.com.", "example.com"], # strip FQDN
|
147
|
+
[" example.com ", "example.com"], # strip spaces
|
148
|
+
["Example.COM", "example.com"], # downcase
|
149
|
+
].each do |input, output|
|
150
|
+
assert_equal output, PublicSuffix.normalize(input)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_normalize_blank
|
155
|
+
[
|
156
|
+
nil,
|
157
|
+
"",
|
158
|
+
" ",
|
159
|
+
].each do |input, _|
|
160
|
+
error = PublicSuffix.normalize(input)
|
161
|
+
assert_instance_of PublicSuffix::DomainInvalid, error
|
162
|
+
assert_equal "Name is blank", error.message
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_normalize_scheme
|
167
|
+
[
|
168
|
+
"https://google.com",
|
169
|
+
].each do |input, _|
|
170
|
+
error = PublicSuffix.normalize(input)
|
171
|
+
assert_instance_of PublicSuffix::DomainInvalid, error
|
172
|
+
assert_match /scheme/, error.message
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_normalize_leading_dot
|
177
|
+
[
|
178
|
+
".google.com",
|
179
|
+
].each do |input, _|
|
180
|
+
error = PublicSuffix.normalize(input)
|
181
|
+
assert_instance_of PublicSuffix::DomainInvalid, error
|
182
|
+
assert_match "Name starts with a dot", error.message
|
183
|
+
end
|
113
184
|
end
|
114
185
|
|
115
186
|
end
|