Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix bug where #valid? and #parse consider URIs as valid domains (closes

  • Loading branch information...
commit 042e77367decfba02b9e019a3943c3ba0fe1e8ee 1 parent aa377e7
@weppos authored
View
2  CHANGELOG.md
@@ -3,6 +3,8 @@
## Master
+- FIXED: #valid? and #parse consider URIs as valid domains (GH-15)
+
- CHANGED: Updated definitions.
- CHANGED: Removed deprecatd PublicSuffixService::RuleList.
View
1  lib/public_suffix.rb
@@ -63,6 +63,7 @@ module PublicSuffix
#
def self.parse(domain)
rule = List.default.find(domain)
+
if rule.nil?
raise DomainInvalid, "`#{domain}' is not a valid domain"
end
View
3  lib/public_suffix/list.rb
@@ -195,6 +195,9 @@ def find(domain)
#
# @return [Array<PublicSuffix::Rule::*>]
def select(domain)
+ # raise DomainInvalid, "`#{domain}' is not expected to contain a scheme"
+ return [] if domain.include?("://")
+
indices = (@indexes[Domain.domain_to_labels(domain).first] || [])
@rules.values_at(*indices).select { |rule| rule.match?(domain) }
end
View
3  test/acceptance_test.rb
@@ -20,16 +20,19 @@ def test_valid
assert_equal tld, domain.tld, "Invalid tld for '#{name}'"
assert_equal sld, domain.sld, "Invalid sld for '#{name}'"
assert_equal trd, domain.trd, "Invalid trd for '#{name}'"
+ assert PublicSuffix.valid?(name)
end
end
InvalidCases = {
"nic.ke" => PublicSuffix::DomainNotAllowed,
+ "http://www.google.com" => PublicSuffix::DomainInvalid,
}
def test_invalid
InvalidCases.each do |name, error|
assert_raise(error) { PublicSuffix.parse(name) }
+ assert !PublicSuffix.valid?(name)
end
end
View
69 test/unit/list_test.rb
@@ -21,21 +21,10 @@ def test_initialize_create_index_when_empty
end
def test_indexes
- @list = PublicSuffix::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
-
- assert !@list.indexes.empty?
- assert_equal [1,2,3,4], @list.indexes.delete('uk')
- assert_equal [0], @list.indexes.delete('com')
- assert @list.indexes.empty?
+ assert !list.indexes.empty?
+ assert_equal [1,2,3,4], list.indexes.delete('uk')
+ assert_equal [0], list.indexes.delete('com')
+ assert list.indexes.empty?
end
@@ -88,37 +77,22 @@ def test_clear
def test_find
- @list = PublicSuffix::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
- assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
- assert_equal PublicSuffix::Rule.factory("com"), @list.find("foo.google.com")
- assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.uk")
- assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.co.uk")
- assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
- assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
- assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("foo.google.com")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("google.uk")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("google.co.uk")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), list.find("foo.google.co.uk")
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("british-library.uk")
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), list.find("foo.british-library.uk")
end
def test_select
- @list = PublicSuffix::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
+ assert_equal 2, list.select("british-library.uk").size
+ end
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
- assert_equal 2, @list.select("british-library.uk").size
+ def test_select_returns_empty_when_domain_has_scheme
+ assert_equal [], list.select("http://google.com")
+ assert_not_equal [], list.select("google.com")
end
@@ -176,7 +150,14 @@ def test_self_parse
end
def test_self_parse_should_create_cache
- list = PublicSuffix::List.parse(<<EOS)
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
+ end
+
+
+private
+
+ def list
+ @_list ||= PublicSuffix::List.parse(<<EOS)
// com : http://en.wikipedia.org/wiki/.com
com
@@ -186,8 +167,6 @@ def test_self_parse_should_create_cache
!bl.uk
!british-library.uk
EOS
-
- assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
end
end
View
9 test/unit/public_suffix_test.rb
@@ -52,16 +52,21 @@ def test_self_parse_a_fully_qualified_domain_name
assert_equal "www", domain.trd
end
- def test_self_parse_should_raise_with_invalid_domain
+ def test_self_parse_raises_with_invalid_domain
error = assert_raise(PublicSuffix::DomainInvalid) { PublicSuffix.parse("example.zip") }
assert_match %r{example\.zip}, error.message
end
- def test_self_parse_should_raise_with_unallowed_domain
+ def test_self_parse_raises_with_unallowed_domain
error = assert_raise(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.ke") }
assert_match %r{example\.ke}, error.message
end
+ def test_self_raises_with_uri
+ error = assert_raise(PublicSuffix::DomainInvalid) { PublicSuffix.parse("http://google.com") }
+ assert_match %r{http://google\.com}, error.message
+ end
+
def test_self_valid
assert PublicSuffix.valid?("google.com")
Please sign in to comment.
Something went wrong with that request. Please try again.