public_suffix_service 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffixService::RuleListTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @list = PublicSuffixService::RuleList.new
7
+ end
8
+
9
+ def teardown
10
+ PublicSuffixService::RuleList.clear
11
+ end
12
+
13
+
14
+ def test_initialize
15
+ assert_instance_of PublicSuffixService::RuleList, @list
16
+ assert_equal 0, @list.length
17
+ end
18
+
19
+
20
+ def test_equality_with_self
21
+ list = PublicSuffixService::RuleList.new
22
+ assert_equal list, list
23
+ end
24
+
25
+ def test_equality_with_internals
26
+ rule = PublicSuffixService::Rule.factory("com")
27
+ assert_equal PublicSuffixService::RuleList.new.add(rule), PublicSuffixService::RuleList.new.add(rule)
28
+ end
29
+
30
+
31
+ def test_add
32
+ assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
33
+ assert_equal @list, @list << PublicSuffixService::Rule.factory("")
34
+ assert_equal 2, @list.length
35
+ end
36
+
37
+ def test_empty?
38
+ assert @list.empty?
39
+ @list.add(PublicSuffixService::Rule.factory(""))
40
+ assert !@list.empty?
41
+ end
42
+
43
+ def test_size
44
+ assert_equal 0, @list.length
45
+ assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
46
+ assert_equal 1, @list.length
47
+ end
48
+
49
+ def test_clear
50
+ assert_equal 0, @list.length
51
+ assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
52
+ assert_equal 1, @list.length
53
+ assert_equal @list, @list.clear
54
+ assert_equal 0, @list.length
55
+ end
56
+
57
+
58
+ def test_find
59
+ @list = PublicSuffixService::RuleList.parse(<<EOS)
60
+ // com : http://en.wikipedia.org/wiki/.com
61
+ com
62
+
63
+ // uk : http://en.wikipedia.org/wiki/.uk
64
+ *.uk
65
+ *.sch.uk
66
+ !bl.uk
67
+ !british-library.uk
68
+ EOS
69
+ assert_equal PublicSuffixService::Rule.factory("com"), @list.find("google.com")
70
+ assert_equal PublicSuffixService::Rule.factory("com"), @list.find("foo.google.com")
71
+ assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("google.uk")
72
+ assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("google.co.uk")
73
+ assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
74
+ assert_equal PublicSuffixService::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
75
+ assert_equal PublicSuffixService::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
76
+ end
77
+
78
+ def test_select
79
+ @list = PublicSuffixService::RuleList.parse(<<EOS)
80
+ // com : http://en.wikipedia.org/wiki/.com
81
+ com
82
+
83
+ // uk : http://en.wikipedia.org/wiki/.uk
84
+ *.uk
85
+ *.sch.uk
86
+ !bl.uk
87
+ !british-library.uk
88
+ EOS
89
+ assert_equal 2, @list.select("british-library.uk").size
90
+ end
91
+
92
+
93
+ def test_self_default_getter
94
+ assert_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
95
+ PublicSuffixService::RuleList.default
96
+ assert_not_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
97
+ end
98
+
99
+ def test_self_default_setter
100
+ PublicSuffixService::RuleList.default
101
+ assert_not_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
102
+ PublicSuffixService::RuleList.default = nil
103
+ assert_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
104
+ end
105
+
106
+ def test_self_clear
107
+ PublicSuffixService::RuleList.default
108
+ assert_not_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
109
+ PublicSuffixService::RuleList.clear
110
+ assert_equal nil, PublicSuffixService::RuleList.send(:class_variable_get, :"@@default")
111
+ end
112
+
113
+ def test_self_reload
114
+ PublicSuffixService::RuleList.default
115
+ PublicSuffixService::RuleList.expects(:default_definition).returns("")
116
+ PublicSuffixService::RuleList.reload
117
+ assert_equal PublicSuffixService::RuleList.new, PublicSuffixService::RuleList.default
118
+ end
119
+
120
+ def test_self_parse
121
+ input = <<EOS
122
+ // ***** BEGIN LICENSE BLOCK *****
123
+ // Version: MPL 1.1/GPL 2.0/LGPL 2.1
124
+ //
125
+ // The contents of this file are subject to the Mozilla Public License Version
126
+ // 1.1 (the "License"); you may not use this file except in compliance with
127
+ // the License. You may obtain a copy of the License at
128
+ // http://www.mozilla.org/MPL/
129
+ //
130
+ // Software distributed under the License is distributed on an "AS IS" basis,
131
+ // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
132
+ // for the specific language governing rights and limitations under the
133
+ // License.
134
+ //
135
+ // The Original Code is the Public Suffix List.
136
+ //
137
+ // The Initial Developer of the Original Code is
138
+ // Jo Hermans <jo.hermans@gmail.com>.
139
+ // Portions created by the Initial Developer are Copyright (C) 2007
140
+ // the Initial Developer. All Rights Reserved.
141
+ //
142
+ // Contributor(s):
143
+ // Ruben Arakelyan <ruben@wackomenace.co.uk>
144
+ // Gervase Markham <gerv@gerv.net>
145
+ // Pamela Greene <pamg.bugs@gmail.com>
146
+ // David Triendl <david@triendl.name>
147
+ // The kind representatives of many TLD registries
148
+ //
149
+ // Alternatively, the contents of this file may be used under the terms of
150
+ // either the GNU General Public License Version 2 or later (the "GPL"), or
151
+ // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
152
+ // in which case the provisions of the GPL or the LGPL are applicable instead
153
+ // of those above. If you wish to allow use of your version of this file only
154
+ // under the terms of either the GPL or the LGPL, and not to allow others to
155
+ // use your version of this file under the terms of the MPL, indicate your
156
+ // decision by deleting the provisions above and replace them with the notice
157
+ // and other provisions required by the GPL or the LGPL. If you do not delete
158
+ // the provisions above, a recipient may use your version of this file under
159
+ // the terms of any one of the MPL, the GPL or the LGPL.
160
+ //
161
+ // ***** END LICENSE BLOCK *****
162
+
163
+ // ac : http://en.wikipedia.org/wiki/.ac
164
+ ac
165
+ com.ac
166
+
167
+ // ad : http://en.wikipedia.org/wiki/.ad
168
+ ad
169
+
170
+ // ar : http://en.wikipedia.org/wiki/.ar
171
+ *.ar
172
+ !congresodelalengua3.ar
173
+ EOS
174
+ expected = []
175
+ list = PublicSuffixService::RuleList.parse(input)
176
+
177
+ assert_instance_of PublicSuffixService::RuleList, list
178
+ assert_equal 5, list.length
179
+ assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffixService::Rule.factory(name) }, list.to_a
180
+ end
181
+
182
+ end
@@ -0,0 +1,215 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffixService::RuleTest < Test::Unit::TestCase
4
+
5
+ def test_factory_should_return_rule_normal
6
+ rule = PublicSuffixService::Rule.factory("verona.it")
7
+ assert_instance_of PublicSuffixService::Rule::Normal, rule
8
+ end
9
+
10
+ def test_factory_should_return_rule_exception
11
+ rule = PublicSuffixService::Rule.factory("!british-library.uk")
12
+ assert_instance_of PublicSuffixService::Rule::Exception, rule
13
+ end
14
+
15
+ def test_factory_should_return_rule_wildcard
16
+ rule = PublicSuffixService::Rule.factory("*.aichi.jp")
17
+ assert_instance_of PublicSuffixService::Rule::Wildcard, rule
18
+ end
19
+
20
+ end
21
+
22
+
23
+ class PublicSuffixService::RuleBaseTest < Test::Unit::TestCase
24
+
25
+ class ::PublicSuffixService::Rule::Test < ::PublicSuffixService::Rule::Base
26
+ end
27
+
28
+ def setup
29
+ @klass = PublicSuffixService::Rule::Base
30
+ end
31
+
32
+
33
+ def test_initialize
34
+ rule = @klass.new("verona.it")
35
+ assert_instance_of @klass, rule
36
+
37
+ assert_equal :base, rule.type
38
+ assert_equal "verona.it", rule.name
39
+ assert_equal "verona.it", rule.value
40
+ assert_equal %w(verona it).reverse, rule.labels
41
+ end
42
+
43
+ def test_equality_with_self
44
+ rule = PublicSuffixService::Rule::Base.new("foo")
45
+ assert_equal rule, rule
46
+ end
47
+
48
+ def test_equality_with_internals
49
+ assert_equal @klass.new("foo"), @klass.new("foo")
50
+ assert_not_equal @klass.new("foo"), @klass.new("bar")
51
+ assert_not_equal @klass.new("foo"), PublicSuffixService::Rule::Test.new("bar")
52
+ assert_not_equal @klass.new("foo"), Class.new { def name; foo; end }.new
53
+ end
54
+
55
+
56
+ def test_match
57
+ assert @klass.new("uk").match?("google.uk")
58
+ assert !@klass.new("gk").match?("google.uk")
59
+ assert !@klass.new("google").match?("google.uk")
60
+ assert @klass.new("uk").match?("google.co.uk")
61
+ assert !@klass.new("gk").match?("google.co.uk")
62
+ assert !@klass.new("co").match?("google.co.uk")
63
+ assert @klass.new("co.uk").match?("google.co.uk")
64
+ assert !@klass.new("uk.co").match?("google.co.uk")
65
+ assert !@klass.new("go.uk").match?("google.co.uk")
66
+ end
67
+
68
+ def test_length
69
+ assert_raise(NotImplementedError) { @klass.new("com").length }
70
+ end
71
+
72
+ def test_parts
73
+ assert_raise(NotImplementedError) { @klass.new("com").parts }
74
+ end
75
+
76
+ def test_decompose
77
+ assert_raise(NotImplementedError) { @klass.new("com").decompose("google.com") }
78
+ end
79
+
80
+ end
81
+
82
+
83
+ class PublicSuffixService::RuleNormalTest < Test::Unit::TestCase
84
+
85
+ def setup
86
+ @klass = PublicSuffixService::Rule::Normal
87
+ end
88
+
89
+
90
+ def test_initialize
91
+ rule = @klass.new("verona.it")
92
+ assert_instance_of @klass, rule
93
+ assert_equal :normal, rule.type
94
+ assert_equal "verona.it", rule.name
95
+ assert_equal "verona.it", rule.value
96
+ assert_equal %w(verona it).reverse, rule.labels
97
+ end
98
+
99
+
100
+ def test_match
101
+ assert @klass.new("uk").match?("google.uk")
102
+ assert !@klass.new("gk").match?("google.uk")
103
+ assert !@klass.new("google").match?("google.uk")
104
+ assert @klass.new("uk").match?("google.co.uk")
105
+ assert !@klass.new("gk").match?("google.co.uk")
106
+ assert !@klass.new("co").match?("google.co.uk")
107
+ assert @klass.new("co.uk").match?("google.co.uk")
108
+ assert !@klass.new("uk.co").match?("google.co.uk")
109
+ assert !@klass.new("go.uk").match?("google.co.uk")
110
+ end
111
+
112
+ def test_length
113
+ assert_equal 1, @klass.new("com").length
114
+ assert_equal 2, @klass.new("co.com").length
115
+ assert_equal 3, @klass.new("mx.co.com").length
116
+ end
117
+
118
+ def test_parts
119
+ assert_equal %w(com), @klass.new("com").parts
120
+ assert_equal %w(co com), @klass.new("co.com").parts
121
+ assert_equal %w(mx co com), @klass.new("mx.co.com").parts
122
+ end
123
+
124
+ def test_decompose
125
+ assert_equal %w(google com), @klass.new("com").decompose("google.com")
126
+ assert_equal %w(foo.google com), @klass.new("com").decompose("foo.google.com")
127
+ end
128
+
129
+ end
130
+
131
+
132
+ class PublicSuffixService::RuleExceptionTest < Test::Unit::TestCase
133
+
134
+ def setup
135
+ @klass = PublicSuffixService::Rule::Exception
136
+ end
137
+
138
+
139
+ def test_initialize
140
+ rule = @klass.new("!british-library.uk")
141
+ assert_instance_of @klass, rule
142
+ assert_equal :exception, rule.type
143
+ assert_equal "!british-library.uk", rule.name
144
+ assert_equal "british-library.uk", rule.value
145
+ assert_equal %w(british-library uk).reverse, rule.labels
146
+ end
147
+
148
+
149
+ def test_match
150
+ assert @klass.new("!uk").match?("google.co.uk")
151
+ assert !@klass.new("!gk").match?("google.co.uk")
152
+ assert @klass.new("!co.uk").match?("google.co.uk")
153
+ assert !@klass.new("!go.uk").match?("google.co.uk")
154
+ assert @klass.new("!british-library.uk").match?("british-library.uk")
155
+ assert !@klass.new("!british-library.uk").match?("google.co.uk")
156
+ end
157
+
158
+ def test_length
159
+ assert_equal 1, @klass.new("!british-library.uk").length
160
+ assert_equal 2, @klass.new("!foo.british-library.uk").length
161
+ end
162
+
163
+ def test_parts
164
+ assert_equal %w(uk), @klass.new("!british-library.uk").parts
165
+ assert_equal %w(tokyo jp), @klass.new("!metro.tokyo.jp").parts
166
+ end
167
+
168
+ def test_decompose
169
+ assert_equal %w(british-library uk), @klass.new("!british-library.uk").decompose("british-library.uk")
170
+ assert_equal %w(foo.british-library uk), @klass.new("!british-library.uk").decompose("foo.british-library.uk")
171
+ end
172
+
173
+ end
174
+
175
+
176
+ class PublicSuffixService::RuleWildcardTest < Test::Unit::TestCase
177
+
178
+ def setup
179
+ @klass = PublicSuffixService::Rule::Wildcard
180
+ end
181
+
182
+
183
+ def test_initialize
184
+ rule = @klass.new("*.aichi.jp")
185
+ assert_instance_of @klass, rule
186
+ assert_equal :wildcard, rule.type
187
+ assert_equal "*.aichi.jp", rule.name
188
+ assert_equal "aichi.jp", rule.value
189
+ assert_equal %w(aichi jp).reverse, rule.labels
190
+ end
191
+
192
+
193
+ def test_match
194
+ assert @klass.new("*.uk").match?("google.uk")
195
+ assert @klass.new("*.uk").match?("google.co.uk")
196
+ assert @klass.new("*.co.uk").match?("google.co.uk")
197
+ assert !@klass.new("*.go.uk").match?("google.co.uk")
198
+ end
199
+
200
+ def test_length
201
+ assert_equal 2, @klass.new("*.uk").length
202
+ assert_equal 3, @klass.new("*.co.uk").length
203
+ end
204
+
205
+ def test_parts
206
+ assert_equal %w(uk), @klass.new("*.uk").parts
207
+ assert_equal %w(co uk), @klass.new("*.co.uk").parts
208
+ end
209
+
210
+ def test_decompose
211
+ assert_equal %w(google co.uk), @klass.new("*.uk").decompose("google.co.uk")
212
+ assert_equal %w(foo.google co.uk), @klass.new("*.uk").decompose("foo.google.co.uk")
213
+ end
214
+
215
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffixServiceTest < Test::Unit::TestCase
4
+
5
+ def test_self_parse_a_domain_with_tld_and_sld
6
+ domain = PublicSuffixService.parse("google.com")
7
+ assert_instance_of PublicSuffixService::Domain, domain
8
+ assert_equal "com", domain.tld
9
+ assert_equal "google", domain.sld
10
+ assert_equal nil, domain.trd
11
+
12
+ domain = PublicSuffixService.parse("google.co.uk")
13
+ assert_instance_of PublicSuffixService::Domain, domain
14
+ assert_equal "co.uk", domain.tld
15
+ assert_equal "google", domain.sld
16
+ assert_equal nil, domain.trd
17
+ end
18
+
19
+ def test_self_parse_a_domain_with_tld_and_sld_and_trd
20
+ domain = PublicSuffixService.parse("alpha.google.com")
21
+ assert_instance_of PublicSuffixService::Domain, domain
22
+ assert_equal "com", domain.tld
23
+ assert_equal "google", domain.sld
24
+ assert_equal "alpha", domain.trd
25
+
26
+ domain = PublicSuffixService.parse("alpha.google.co.uk")
27
+ assert_instance_of PublicSuffixService::Domain, domain
28
+ assert_equal "co.uk", domain.tld
29
+ assert_equal "google", domain.sld
30
+ assert_equal "alpha", domain.trd
31
+ end
32
+
33
+ def test_self_parse_a_domain_with_tld_and_sld_and_4rd
34
+ domain = PublicSuffixService.parse("one.two.google.com")
35
+ assert_instance_of PublicSuffixService::Domain, domain
36
+ assert_equal "com", domain.tld
37
+ assert_equal "google", domain.sld
38
+ assert_equal "one.two", domain.trd
39
+
40
+ domain = PublicSuffixService.parse("one.two.google.co.uk")
41
+ assert_instance_of PublicSuffixService::Domain, domain
42
+ assert_equal "co.uk", domain.tld
43
+ assert_equal "google", domain.sld
44
+ assert_equal "one.two", domain.trd
45
+ end
46
+
47
+ def test_self_parse_should_raise_with_invalid_domain
48
+ error = assert_raise(PublicSuffixService::InvalidDomain) { PublicSuffixService.parse("google.zip") }
49
+ assert_match %r{google\.zip}, error.message
50
+ end
51
+
52
+
53
+ def test_self_valid_question
54
+ assert PublicSuffixService.valid?("google.com")
55
+ assert PublicSuffixService.valid?("www.google.com")
56
+ assert PublicSuffixService.valid?("google.co.uk")
57
+ assert PublicSuffixService.valid?("www.google.co.uk")
58
+ assert !PublicSuffixService.valid?("google.zip")
59
+ assert !PublicSuffixService.valid?("www.google.zip")
60
+ end
61
+
62
+ end