Skip to content

Commit

Permalink
In some rare circumstances the server guessing fails to return the ri…
Browse files Browse the repository at this point in the history
…ght server but returns an other server instead (closes #260).
  • Loading branch information
weppos committed Sep 18, 2009
1 parent 1fd11c3 commit 6009391
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
= Changelog


== master
== release-0.5

FIXED: self.valid_ipv6?(addr) references valid_v4? instead of valid_ipv4? (closes #300)

FIXED: In some rare circumstances the server guessing fails to return the right server but returns an other server instead (closes #260).


== Release 0.5.2

Expand Down
2 changes: 1 addition & 1 deletion lib/whois/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def self.find_for_email(qstring)

def self.find_for_tld(qstring)
definitions(:tld).each do |definition|
return factory(:tld, *definition) if /#{definition.first}$/ =~ qstring
return factory(:tld, *definition) if /#{Regexp.escape(definition.first)}$/ =~ qstring
end
nil
end
Expand Down
78 changes: 44 additions & 34 deletions test/server_test.rb
Original file line number Diff line number Diff line change
@@ -1,89 +1,99 @@
require 'test_helper'

class ServerTest < Test::Unit::TestCase
include Whois

def setup
Whois::Server.class_eval { class_variable_set("@@definitions", { :tld => [], :ipv4 =>[], :ipv6 => [] }) }
end


def test_guess_should_recognize_email
Server.expects(:find_for_email).with("email@example.org").returns(true)
assert Server.guess("email@example.org")
Whois::Server.expects(:find_for_email).with("email@example.org").returns(true)
assert Whois::Server.guess("email@example.org")
end

def test_guess_should_recognize_tld
Server.expects(:find_for_tld).with("google.com").returns(true)
assert Server.guess("google.com")
Whois::Server.expects(:find_for_tld).with("google.com").returns(true)
assert Whois::Server.guess("google.com")
end

def test_guess_should_recognize_ipv4
Server.expects(:find_for_ipv4).with("192.168.1.1").returns(true)
assert Server.guess("192.168.1.1")
Whois::Server.expects(:find_for_ipv4).with("192.168.1.1").returns(true)
assert Whois::Server.guess("192.168.1.1")
end

def test_guess_should_recognize_ipv6
Server.expects(:find_for_ipv6).with("2001:0db8:85a3:0000:0000:8a2e:0370:7334").returns(true)
assert Server.guess("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
Whois::Server.expects(:find_for_ipv6).with("2001:0db8:85a3:0000:0000:8a2e:0370:7334").returns(true)
assert Whois::Server.guess("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
end

def test_guess_should_recognize_ipv6_with_zeros_group
Server.expects(:find_for_ipv6).with("2002::1").returns(true)
assert Server.guess("2002::1")
Whois::Server.expects(:find_for_ipv6).with("2002::1").returns(true)
assert Whois::Server.guess("2002::1")
end

def test_guess_should_raise_servernotfound_with_unrecognized_query
assert_raise(ServerNotFound){ Server.guess("xyz") }
assert_raise(Whois::ServerNotFound){ Whois::Server.guess("xyz") }
end




def test_find_for_tld_should_not_consider_dot_as_regexp_instruction
Whois::Server.define(:tld, ".no.com", "whois.no.com")
Whois::Server.define(:tld, ".com", "whois.com")
assert_equal "whois.com", Whois::Server.guess("antoniocangiano.com").host
end

def test_find_for_ipv6_should_factory_ipv6_with_ipv4_compatibility
Server.define(:ipv6, "::192.168.1.1", "whois.foo")
Server.expects(:factory).with(:ipv6, any_parameters).returns(true)
assert Server.guess("::192.168.1.1")
Whois::Server.define(:ipv6, "::192.168.1.1", "whois.foo")
Whois::Server.expects(:factory).with(:ipv6, any_parameters).returns(true)
assert Whois::Server.guess("::192.168.1.1")
end


def test_definitions
assert_instance_of Hash, Server.definitions
assert_instance_of Hash, Whois::Server.definitions
end

def test_definitions_with_key
assert_equal nil, Server.definitions(:foo)
Server.define(:foo, ".foo", "whois.foo")
assert_equal [[".foo", "whois.foo", {}]], Server.definitions(:foo)
assert_equal nil, Whois::Server.definitions(:foo)
Whois::Server.define(:foo, ".foo", "whois.foo")
assert_equal [[".foo", "whois.foo", {}]], Whois::Server.definitions(:foo)
end


def test_define_tld
Server.define(:tld, ".foo", "whois.foo")
assert_equal [".foo", "whois.foo", {}], Server.definitions[:tld].last
Whois::Server.define(:tld, ".foo", "whois.foo")
assert_equal [".foo", "whois.foo", {}], Whois::Server.definitions[:tld].last
end

def test_define_tld_with_options
Server.define(:tld, ".foo", "whois.foo", :foo => "bar")
assert_equal [".foo", "whois.foo", { :foo => "bar" }], Server.definitions[:tld].last
Whois::Server.define(:tld, ".foo", "whois.foo", :foo => "bar")
assert_equal [".foo", "whois.foo", { :foo => "bar" }], Whois::Server.definitions[:tld].last
end

def test_define_ipv4
Server.define(:ipv4, ".foo", "whois.foo")
assert_equal [".foo", "whois.foo", {}], Server.definitions[:ipv4].last
Whois::Server.define(:ipv4, ".foo", "whois.foo")
assert_equal [".foo", "whois.foo", {}], Whois::Server.definitions[:ipv4].last
end

def test_define_ipv4_with_options
Server.define(:ipv4, ".foo", "whois.foo", :foo => "bar")
assert_equal [".foo", "whois.foo", { :foo => "bar" }], Server.definitions[:ipv4].last
Whois::Server.define(:ipv4, ".foo", "whois.foo", :foo => "bar")
assert_equal [".foo", "whois.foo", { :foo => "bar" }], Whois::Server.definitions[:ipv4].last
end


def test_factory
server = Server.factory(:tld, ".foo", "whois.foo")
assert_instance_of Server::Adapters::Standard, server
server = Whois::Server.factory(:tld, ".foo", "whois.foo")
assert_instance_of Whois::Server::Adapters::Standard, server
end

def test_factory_with_adapter
server = Server.factory(:tld, ".foo", "whois.foo", :adapter => Server::Adapters::None)
assert_instance_of Server::Adapters::None, server
server = Whois::Server.factory(:tld, ".foo", "whois.foo", :adapter => Whois::Server::Adapters::None)
assert_instance_of Whois::Server::Adapters::None, server
end

def test_factory_with_adapter_should_delete_adapter_option
server = Server.factory(:tld, ".foo", "whois.foo", :adapter => Server::Adapters::None, :foo => "bar")
server = Whois::Server.factory(:tld, ".foo", "whois.foo", :adapter => Whois::Server::Adapters::None, :foo => "bar")
assert_equal server.options, { :foo => "bar" }
end

Expand Down

0 comments on commit 6009391

Please sign in to comment.