Skip to content

Commit

Permalink
fix domain checking logic in address.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-dyoshikawa committed Feb 3, 2025
1 parent d60b89b commit 36ab1e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ def domain_is_in?(domain_list)
address_domain = address.domain.downcase
return true if domain_list.include?(address_domain)

i = address_domain.index('.')
return false unless i
while i = address_domain.index('.')
address_domain = address_domain[(i + 1)..-1]
return true if domain_list.include?(address_domain)
end

domain_list.include?(address_domain[(i + 1)..-1])
false
end

def mx_server_is_in?(domain_list)
Expand Down
32 changes: 28 additions & 4 deletions spec/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@
end
end

describe "#disposable_domain?" do
let(:disposable_domain) { ValidEmail2.disposable_emails.first }

it "is true if the domain is in the disposable_emails list" do
address = described_class.new("foo@#{disposable_domain}")
expect(address.disposable_domain?).to eq true
end

it "is true if the domain is a subdomain of a disposable domain" do
address = described_class.new("foo@sub.#{disposable_domain}")
expect(address.disposable_domain?).to eq true
end

it "is true if the domain is a deeply nested subdomain of a disposable domain" do
address = described_class.new("[email protected].#{disposable_domain}")
expect(address.disposable_domain?).to eq true
end

it "is false if the domain is not in the disposable_emails list" do
address = described_class.new("[email protected]")
expect(address.disposable_domain?).to eq false
end
end

describe "caching" do
let(:email_address) { "[email protected]" }
let(:email_instance) { described_class.new(email_address) }
Expand Down Expand Up @@ -181,7 +205,7 @@
email_instance.valid_strict_mx?
end

it "calls the the MX servers lookup" do
it "calls the the MX servers lookup" do
email_instance.valid_strict_mx?

expect(Resolv::DNS).to have_received(:open).once
Expand Down Expand Up @@ -308,8 +332,8 @@
stub_const("ValidEmail2::DnsRecordsCache::MAX_CACHE_SIZE", 0)
end

it "prunes the cache" do
expect(dns_records_cache_instance).to receive(:prune_cache).once
it "prunes the cache" do
expect(dns_records_cache_instance).to receive(:prune_cache).once

email_instance.valid_mx?
end
Expand All @@ -331,7 +355,7 @@
end
end
end
end
end
end
end
end

0 comments on commit 36ab1e9

Please sign in to comment.