File: acceptance_test.rb

package info (click to toggle)
ruby-public-suffix 4.0.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320 kB
  • sloc: ruby: 1,431; makefile: 10
file content (131 lines) | stat: -rw-r--r-- 3,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true

require "test_helper"

class AcceptanceTest < Minitest::Test

  VALID_CASES = [
    ["example.com",             "example.com", [nil, "example", "com"]],
    ["foo.example.com",         "example.com",        ["foo", "example", "com"]],

    ["verybritish.co.uk",       "verybritish.co.uk",  [nil, "verybritish", "co.uk"]],
    ["foo.verybritish.co.uk",   "verybritish.co.uk",  ["foo", "verybritish", "co.uk"]],

    ["parliament.uk",           "parliament.uk",      [nil, "parliament", "uk"]],
    ["foo.parliament.uk",       "parliament.uk",      ["foo", "parliament", "uk"]],
  ].freeze

  def test_valid
    VALID_CASES.each do |input, domain, results|
      parsed = PublicSuffix.parse(input)
      trd, sld, tld = results
      assert_equal tld, parsed.tld, "Invalid tld for `#{name}`"
      assert_equal sld, parsed.sld, "Invalid sld for `#{name}`"
      if trd.nil?
        assert_nil parsed.trd, "Invalid trd for `#{name}`"
      else
        assert_equal trd, parsed.trd, "Invalid trd for `#{name}`"
      end

      assert_equal domain, PublicSuffix.domain(input)
      assert PublicSuffix.valid?(input)
    end
  end


  INVALID_CASES = [
    ["nic.bd", PublicSuffix::DomainNotAllowed],
    [nil,                       PublicSuffix::DomainInvalid],
    ["",                        PublicSuffix::DomainInvalid],
    ["  ",                      PublicSuffix::DomainInvalid],
  ].freeze

  def test_invalid
    INVALID_CASES.each do |(name, error)|
      assert_raises(error) { PublicSuffix.parse(name) }
      assert !PublicSuffix.valid?(name)
    end
  end


  REJECTED_CASES = [
    ["www. .com", true],
    ["foo.co..uk",          true],
    ["goo,gle.com",         true],
    ["-google.com",         true],
    ["google-.com",         true],

    # This case was covered in GH-15.
    # I decided to cover this case because it's not easily reproducible with URI.parse
    # and can lead to several false positives.
    ["http://google.com",   false],
  ].freeze

  def test_rejected
    REJECTED_CASES.each do |name, expected|
      assert_equal expected, PublicSuffix.valid?(name),
                   "Expected %s to be %s" % [name.inspect, expected.inspect]
      assert !valid_domain?(name),
             "#{name} expected to be invalid"
    end
  end


  CASE_CASES = [
    ["Www.google.com", %w( www google com )],
    ["www.Google.com", %w( www google com )],
    ["www.google.Com", %w( www google com )],
  ].freeze

  def test_ignore_case
    CASE_CASES.each do |name, results|
      domain = PublicSuffix.parse(name)
      trd, sld, tld = results
      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


  INCLUDE_PRIVATE_CASES = [
    ["blogspot.com", true, "blogspot.com"],
    ["blogspot.com", false, nil],
    ["subdomain.blogspot.com", true, "blogspot.com"],
    ["subdomain.blogspot.com", false, "subdomain.blogspot.com"],
  ].freeze

  # rubocop:disable Style/CombinableLoops
  def test_ignore_private
    # test domain and parse
    INCLUDE_PRIVATE_CASES.each do |given, ignore_private, expected|
      if expected.nil?
        assert_nil PublicSuffix.domain(given, ignore_private: ignore_private)
      else
        assert_equal expected, PublicSuffix.domain(given, ignore_private: ignore_private)
      end
    end
    # test valid?
    INCLUDE_PRIVATE_CASES.each do |given, ignore_private, expected|
      assert_equal !expected.nil?, PublicSuffix.valid?(given, ignore_private: ignore_private)
    end
  end
  # rubocop:enable Style/CombinableLoops


  def valid_uri?(name)
    uri = URI.parse(name)
    !uri.host.nil?
  rescue StandardError
    false
  end

  def valid_domain?(name)
    uri = URI.parse(name)
    !uri.host.nil? && uri.scheme.nil?
  rescue StandardError
    false
  end

end