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.
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class ErrorsTest < Minitest::Unit::TestCase
4
4
 
@@ -20,4 +20,4 @@ class ErrorsTest < Minitest::Unit::TestCase
20
20
  PublicSuffix::DomainNotAllowed.new
21
21
  end
22
22
 
23
- end
23
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
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.length
16
+ assert_equal 0, @list.size
17
17
  end
18
18
 
19
- def test_initialize_create_index_when_empty
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.length
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 nil, @list.find("google.net")
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 @list.empty?
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.length
64
+ assert_equal 0, @list.size
72
65
  assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
73
- assert_equal 1, @list.length
66
+ assert_equal 1, @list.size
74
67
  end
75
68
 
76
69
  def test_clear
77
- assert_equal 0, @list.length
70
+ assert_equal 0, @list.size
78
71
  assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
79
- assert_equal 1, @list.length
72
+ assert_equal 1, @list.size
80
73
  assert_equal @list, @list.clear
81
- assert_equal 0, @list.length
74
+ assert_equal 0, @list.size
82
75
  end
83
76
 
84
77
 
85
78
  def test_find
86
- assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
87
- assert_equal PublicSuffix::Rule.factory("com"), list.find("foo.google.com")
88
- assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("google.uk")
89
- assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("google.co.uk")
90
- assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("foo.google.co.uk")
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 test_select_returns_empty_when_domain_is_nil
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 test_select_returns_empty_when_domain_has_scheme
109
- assert_equal [], list.select("http://google.com")
110
- assert_not_equal [], list.select("google.com")
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 nil, PublicSuffix::List.class_eval { @default }
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 nil, PublicSuffix::List.class_eval { @default }
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 nil, PublicSuffix::List.class_eval { @default }
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
- // ***** BEGIN LICENSE BLOCK *****
145
- // Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
- // ac : http://en.wikipedia.org/wiki/.ac
150
- ac
151
- com.ac
186
+ // ===BEGIN ICANN DOMAINS===
152
187
 
153
- // ad : http://en.wikipedia.org/wiki/.ad
154
- ad
188
+ // com
189
+ com
155
190
 
156
- // ar : http://en.wikipedia.org/wiki/.ar
157
- *.ar
158
- !congresodelalengua3.ar
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 5, list.length
163
- assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffix::Rule.factory(name) }, list.to_a
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 test_self_parse_should_create_cache
167
- assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
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 'test_helper'
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 test_self_parse_a_fully_qualified_domain_name
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 test_private_domains_are_enabled_by_default
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 test_self_parse_raises_with_invalid_domain
81
- error = assert_raises(PublicSuffix::DomainInvalid) { PublicSuffix.parse("example.qqq") }
82
- assert_match %r{example\.qqq}, error.message
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 test_self_parse_raises_with_unallowed_domain
89
+ def test_self_parse_with_unallowed_domain
86
90
  error = assert_raises(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.ke") }
87
- assert_match %r{example\.ke}, error.message
91
+ assert_match(/example\.ke/, error.message)
88
92
  end
89
93
 
90
- def test_self_raises_with_uri
94
+ def test_self_parse_with_uri
91
95
  error = assert_raises(PublicSuffix::DomainInvalid) { PublicSuffix.parse("http://google.com") }
92
- assert_match %r{http://google\.com}, error.message
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
- # Returns false when domain has an invalid TLD
104
- def test_self_valid_with_invalid_tld
105
- assert !PublicSuffix.valid?("google.qqq")
106
- assert !PublicSuffix.valid?("www.google.qqq")
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 test_self_valid_with_fully_qualified_domain_name
110
- assert PublicSuffix.valid?("google.com.")
111
- assert PublicSuffix.valid?("google.co.uk.")
112
- assert !PublicSuffix.valid?("google.qqq.")
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