public_suffix 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class AcceptanceTest < Test::Unit::TestCase
4
+
5
+ ValidCases = {
6
+ "google.com" => [nil, "google", "com"],
7
+ "foo.google.com" => ["foo", "google", "com"],
8
+
9
+ "verybritish.co.uk" => [nil, "verybritish", "co.uk"],
10
+ "foo.verybritish.co.uk" => ["foo", "verybritish", "co.uk"],
11
+
12
+ "parliament.uk" => [nil, "parliament", "uk"],
13
+ "foo.parliament.uk" => ["foo", "parliament", "uk"],
14
+ }
15
+
16
+ def test_valid
17
+ ValidCases.each do |name, results|
18
+ domain = PublicSuffix.parse(name)
19
+ trd, sld, tld = results
20
+ assert_equal tld, domain.tld, "Invalid tld for '#{name}'"
21
+ assert_equal sld, domain.sld, "Invalid sld for '#{name}'"
22
+ assert_equal trd, domain.trd, "Invalid trd for '#{name}'"
23
+ end
24
+ end
25
+
26
+ InvalidCases = {
27
+ "nic.ke" => PublicSuffix::DomainNotAllowed,
28
+ }
29
+
30
+ def test_invalid
31
+ InvalidCases.each do |name, error|
32
+ assert_raise(error) { PublicSuffix.parse(name) }
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+
5
+ $:.unshift File.expand_path('../../lib', __FILE__)
6
+ require 'public_suffix'
@@ -0,0 +1,170 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffix::DomainTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @klass = PublicSuffix::Domain
7
+ end
8
+
9
+ # Tokenizes given input into labels.
10
+ def test_self_domain_to_labels
11
+ assert_equal %w( com live spaces someone ),
12
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
13
+ assert_equal %w( com zoho wiki leontina23samiko ),
14
+ PublicSuffix::Domain.domain_to_labels("leontina23samiko.wiki.zoho.com")
15
+ end
16
+
17
+ # Converts input into String.
18
+ def test_self_domain_to_labels_converts_input_to_string
19
+ assert_equal %w( com live spaces someone ),
20
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
21
+ end
22
+
23
+ # Ignores trailing .
24
+ def test_self_domain_to_labels_ignores_trailing_dot
25
+ assert_equal %w( com live spaces someone ),
26
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
27
+ assert_equal %w( com live spaces someone ),
28
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
29
+ end
30
+
31
+
32
+ def test_initialize_with_tld
33
+ domain = @klass.new("com")
34
+ assert_equal "com", domain.tld
35
+ assert_equal nil, domain.sld
36
+ assert_equal nil, domain.trd
37
+ end
38
+
39
+ def test_initialize_with_tld_and_sld
40
+ domain = @klass.new("com", "google")
41
+ assert_equal "com", domain.tld
42
+ assert_equal "google", domain.sld
43
+ assert_equal nil, domain.trd
44
+ end
45
+
46
+ def test_initialize_with_tld_and_sld_and_trd
47
+ domain = @klass.new("com", "google", "www")
48
+ assert_equal "com", domain.tld
49
+ assert_equal "google", domain.sld
50
+ assert_equal "www", domain.trd
51
+ end
52
+
53
+
54
+ def test_to_s
55
+ assert_equal "com", @klass.new("com").to_s
56
+ assert_equal "google.com", @klass.new("com", "google").to_s
57
+ assert_equal "www.google.com", @klass.new("com", "google", "www").to_s
58
+ end
59
+
60
+ def test_to_a
61
+ assert_equal [nil, nil, "com"], @klass.new("com").to_a
62
+ assert_equal [nil, "google", "com"], @klass.new("com", "google").to_a
63
+ assert_equal ["www", "google", "com"], @klass.new("com", "google", "www").to_a
64
+ end
65
+
66
+
67
+ def test_tld
68
+ assert_equal "com", @klass.new("com", "google", "www").tld
69
+ end
70
+
71
+ def test_sld
72
+ assert_equal "google", @klass.new("com", "google", "www").sld
73
+ end
74
+
75
+ def test_trd
76
+ assert_equal "www", @klass.new("com", "google", "www").trd
77
+ end
78
+
79
+
80
+ def test_name
81
+ assert_equal "com", @klass.new("com").name
82
+ assert_equal "google.com", @klass.new("com", "google").name
83
+ assert_equal "www.google.com", @klass.new("com", "google", "www").name
84
+ end
85
+
86
+ def test_domain
87
+ assert_equal nil, @klass.new("com").domain
88
+ assert_equal nil, @klass.new("zip").domain
89
+ assert_equal "google.com", @klass.new("com", "google").domain
90
+ assert_equal "google.zip", @klass.new("zip", "google").domain
91
+ assert_equal "google.com", @klass.new("com", "google", "www").domain
92
+ assert_equal "google.zip", @klass.new("zip", "google", "www").domain
93
+ end
94
+
95
+ def test_subdomain
96
+ assert_equal nil, @klass.new("com").subdomain
97
+ assert_equal nil, @klass.new("zip").subdomain
98
+ assert_equal nil, @klass.new("com", "google").subdomain
99
+ assert_equal nil, @klass.new("zip", "google").subdomain
100
+ assert_equal "www.google.com", @klass.new("com", "google", "www").subdomain
101
+ assert_equal "www.google.zip", @klass.new("zip", "google", "www").subdomain
102
+ end
103
+
104
+ def test_rule
105
+ assert_equal nil, @klass.new("zip").rule
106
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com").rule
107
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google").rule
108
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google", "www").rule
109
+ end
110
+
111
+
112
+ def test_domain_question
113
+ assert @klass.new("com", "google").domain?
114
+ assert @klass.new("zip", "google").domain?
115
+ assert @klass.new("com", "google", "www").domain?
116
+ assert !@klass.new("com").domain?
117
+ end
118
+
119
+ def test_subdomain_question
120
+ assert @klass.new("com", "google", "www").subdomain?
121
+ assert @klass.new("zip", "google", "www").subdomain?
122
+ assert !@klass.new("com").subdomain?
123
+ assert !@klass.new("com", "google").subdomain?
124
+ end
125
+
126
+ def test_is_a_domain_question
127
+ assert @klass.new("com", "google").is_a_domain?
128
+ assert @klass.new("zip", "google").is_a_domain?
129
+ assert !@klass.new("com", "google", "www").is_a_domain?
130
+ assert !@klass.new("com").is_a_domain?
131
+ end
132
+
133
+ def test_is_a_subdomain_question
134
+ assert @klass.new("com", "google", "www").is_a_subdomain?
135
+ assert @klass.new("zip", "google", "www").is_a_subdomain?
136
+ assert !@klass.new("com").is_a_subdomain?
137
+ assert !@klass.new("com", "google").is_a_subdomain?
138
+ end
139
+
140
+ def test_valid_question
141
+ assert !@klass.new("com").valid?
142
+ assert @klass.new("com", "example").valid?
143
+ assert @klass.new("com", "example", "www").valid?
144
+
145
+ # not-assigned
146
+ assert !@klass.new("zip").valid?
147
+ assert !@klass.new("zip", "example").valid?
148
+ assert !@klass.new("zip", "example", "www").valid?
149
+
150
+ # not-allowed
151
+ assert !@klass.new("ke").valid?
152
+ assert !@klass.new("ke", "example").valid?
153
+ assert @klass.new("ke", "example", "www").valid?
154
+ end
155
+
156
+ def test_valid_domain_question
157
+ assert @klass.new("com", "google").valid_domain?
158
+ assert !@klass.new("zip", "google").valid_domain?
159
+ assert @klass.new("com", "google", "www").valid_domain?
160
+ assert !@klass.new("com").valid_domain?
161
+ end
162
+
163
+ def test_valid_subdomain_question
164
+ assert @klass.new("com", "google", "www").valid_subdomain?
165
+ assert !@klass.new("zip", "google", "www").valid_subdomain?
166
+ assert !@klass.new("com").valid_subdomain?
167
+ assert !@klass.new("com", "google").valid_subdomain?
168
+ end
169
+
170
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class ErrorsTest < Test::Unit::TestCase
4
+
5
+ # Inherits from StandardError
6
+ def test_error_inheritance
7
+ assert_kind_of StandardError,
8
+ PublicSuffix::Error.new
9
+ end
10
+
11
+ # Inherits from PublicSuffix::Error
12
+ def test_domain_invalid_inheritance
13
+ assert_kind_of PublicSuffix::Error,
14
+ PublicSuffix::DomainInvalid.new
15
+ end
16
+
17
+ # Inherits from PublicSuffix::DomainInvalid
18
+ def test_domain_not_allowed_inheritance
19
+ assert_kind_of PublicSuffix::DomainInvalid,
20
+ PublicSuffix::DomainNotAllowed.new
21
+ end
22
+
23
+ end
@@ -0,0 +1,193 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffix::ListTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @list = PublicSuffix::List.new
7
+ end
8
+
9
+ def teardown
10
+ PublicSuffix::List.clear
11
+ end
12
+
13
+
14
+ def test_initialize
15
+ assert_instance_of PublicSuffix::List, @list
16
+ assert_equal 0, @list.length
17
+ end
18
+
19
+ def test_initialize_create_index_when_empty
20
+ assert_equal({}, @list.indexes)
21
+ end
22
+
23
+ def test_indexes
24
+ @list = PublicSuffix::List.parse(<<EOS)
25
+ // com : http://en.wikipedia.org/wiki/.com
26
+ com
27
+
28
+ // uk : http://en.wikipedia.org/wiki/.uk
29
+ *.uk
30
+ *.sch.uk
31
+ !bl.uk
32
+ !british-library.uk
33
+ EOS
34
+
35
+ assert !@list.indexes.empty?
36
+ assert_equal [1,2,3,4], @list.indexes.delete('uk')
37
+ assert_equal [0], @list.indexes.delete('com')
38
+ assert @list.indexes.empty?
39
+ end
40
+
41
+
42
+ def test_equality_with_self
43
+ list = PublicSuffix::List.new
44
+ assert_equal list, list
45
+ end
46
+
47
+ def test_equality_with_internals
48
+ rule = PublicSuffix::Rule.factory("com")
49
+ assert_equal PublicSuffix::List.new.add(rule), PublicSuffix::List.new.add(rule)
50
+ end
51
+
52
+
53
+ def test_add
54
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
55
+ assert_equal @list, @list << PublicSuffix::Rule.factory("")
56
+ assert_equal 2, @list.length
57
+ end
58
+
59
+ def test_add_should_recreate_index
60
+ @list = PublicSuffix::List.parse("com")
61
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
62
+ assert_equal nil, @list.find("google.net")
63
+
64
+ @list << PublicSuffix::Rule.factory("net")
65
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
66
+ assert_equal PublicSuffix::Rule.factory("net"), @list.find("google.net")
67
+ end
68
+
69
+ def test_empty?
70
+ assert @list.empty?
71
+ @list.add(PublicSuffix::Rule.factory(""))
72
+ assert !@list.empty?
73
+ end
74
+
75
+ def test_size
76
+ assert_equal 0, @list.length
77
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
78
+ assert_equal 1, @list.length
79
+ end
80
+
81
+ def test_clear
82
+ assert_equal 0, @list.length
83
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
84
+ assert_equal 1, @list.length
85
+ assert_equal @list, @list.clear
86
+ assert_equal 0, @list.length
87
+ end
88
+
89
+
90
+ def test_find
91
+ @list = PublicSuffix::List.parse(<<EOS)
92
+ // com : http://en.wikipedia.org/wiki/.com
93
+ com
94
+
95
+ // uk : http://en.wikipedia.org/wiki/.uk
96
+ *.uk
97
+ *.sch.uk
98
+ !bl.uk
99
+ !british-library.uk
100
+ EOS
101
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
102
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("foo.google.com")
103
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.uk")
104
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.co.uk")
105
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
106
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
107
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
108
+ end
109
+
110
+ def test_select
111
+ @list = PublicSuffix::List.parse(<<EOS)
112
+ // com : http://en.wikipedia.org/wiki/.com
113
+ com
114
+
115
+ // uk : http://en.wikipedia.org/wiki/.uk
116
+ *.uk
117
+ *.sch.uk
118
+ !bl.uk
119
+ !british-library.uk
120
+ EOS
121
+ assert_equal 2, @list.select("british-library.uk").size
122
+ end
123
+
124
+
125
+ def test_self_default_getter
126
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
127
+ PublicSuffix::List.default
128
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
129
+ end
130
+
131
+ def test_self_default_setter
132
+ PublicSuffix::List.default
133
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
134
+ PublicSuffix::List.default = nil
135
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
136
+ end
137
+
138
+ def test_self_clear
139
+ PublicSuffix::List.default
140
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
141
+ PublicSuffix::List.clear
142
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
143
+ end
144
+
145
+ def test_self_reload
146
+ PublicSuffix::List.default
147
+ PublicSuffix::List.expects(:default_definition).returns("")
148
+
149
+ PublicSuffix::List.reload
150
+ assert_equal PublicSuffix::List.new, PublicSuffix::List.default
151
+ end
152
+
153
+
154
+ def test_self_parse
155
+ list = PublicSuffix::List.parse(<<EOS)
156
+ // ***** BEGIN LICENSE BLOCK *****
157
+ // Version: MPL 1.1/GPL 2.0/LGPL 2.1
158
+ //
159
+ // ***** END LICENSE BLOCK *****
160
+
161
+ // ac : http://en.wikipedia.org/wiki/.ac
162
+ ac
163
+ com.ac
164
+
165
+ // ad : http://en.wikipedia.org/wiki/.ad
166
+ ad
167
+
168
+ // ar : http://en.wikipedia.org/wiki/.ar
169
+ *.ar
170
+ !congresodelalengua3.ar
171
+ EOS
172
+
173
+ assert_instance_of PublicSuffix::List, list
174
+ assert_equal 5, list.length
175
+ assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffix::Rule.factory(name) }, list.to_a
176
+ end
177
+
178
+ def test_self_parse_should_create_cache
179
+ list = PublicSuffix::List.parse(<<EOS)
180
+ // com : http://en.wikipedia.org/wiki/.com
181
+ com
182
+
183
+ // uk : http://en.wikipedia.org/wiki/.uk
184
+ *.uk
185
+ *.sch.uk
186
+ !bl.uk
187
+ !british-library.uk
188
+ EOS
189
+
190
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
191
+ end
192
+
193
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffixTest < Test::Unit::TestCase
4
+
5
+ def test_self_parse_a_domain_with_tld_and_sld
6
+ domain = PublicSuffix.parse("example.com")
7
+ assert_instance_of PublicSuffix::Domain, domain
8
+ assert_equal "com", domain.tld
9
+ assert_equal "example", domain.sld
10
+ assert_equal nil, domain.trd
11
+
12
+ domain = PublicSuffix.parse("example.co.uk")
13
+ assert_instance_of PublicSuffix::Domain, domain
14
+ assert_equal "co.uk", domain.tld
15
+ assert_equal "example", 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 = PublicSuffix.parse("alpha.example.com")
21
+ assert_instance_of PublicSuffix::Domain, domain
22
+ assert_equal "com", domain.tld
23
+ assert_equal "example", domain.sld
24
+ assert_equal "alpha", domain.trd
25
+
26
+ domain = PublicSuffix.parse("alpha.example.co.uk")
27
+ assert_instance_of PublicSuffix::Domain, domain
28
+ assert_equal "co.uk", domain.tld
29
+ assert_equal "example", 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 = PublicSuffix.parse("one.two.example.com")
35
+ assert_instance_of PublicSuffix::Domain, domain
36
+ assert_equal "com", domain.tld
37
+ assert_equal "example", domain.sld
38
+ assert_equal "one.two", domain.trd
39
+
40
+ domain = PublicSuffix.parse("one.two.example.co.uk")
41
+ assert_instance_of PublicSuffix::Domain, domain
42
+ assert_equal "co.uk", domain.tld
43
+ assert_equal "example", domain.sld
44
+ assert_equal "one.two", domain.trd
45
+ end
46
+
47
+ def test_self_parse_a_fully_qualified_domain_name
48
+ domain = PublicSuffix.parse("www.example.com.")
49
+ assert_instance_of PublicSuffix::Domain, domain
50
+ assert_equal "com", domain.tld
51
+ assert_equal "example", domain.sld
52
+ assert_equal "www", domain.trd
53
+ end
54
+
55
+ def test_self_parse_should_raise_with_invalid_domain
56
+ error = assert_raise(PublicSuffix::DomainInvalid) { PublicSuffix.parse("example.zip") }
57
+ assert_match %r{example\.zip}, error.message
58
+ end
59
+
60
+ def test_self_parse_should_raise_with_unallowed_domain
61
+ error = assert_raise(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.ke") }
62
+ assert_match %r{example\.ke}, error.message
63
+ end
64
+
65
+
66
+ def test_self_valid
67
+ assert PublicSuffix.valid?("google.com")
68
+ assert PublicSuffix.valid?("www.google.com")
69
+ assert PublicSuffix.valid?("google.co.uk")
70
+ assert PublicSuffix.valid?("www.google.co.uk")
71
+ end
72
+
73
+ # Returns false when domain has an invalid TLD
74
+ def test_self_valid_with_invalid_tld
75
+ assert !PublicSuffix.valid?("google.zip")
76
+ assert !PublicSuffix.valid?("www.google.zip")
77
+ end
78
+
79
+ def test_self_valid_with_fully_qualified_domain_name
80
+ assert PublicSuffix.valid?("google.com.")
81
+ assert PublicSuffix.valid?("google.co.uk.")
82
+ assert !PublicSuffix.valid?("google.zip.")
83
+ end
84
+
85
+ end