Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

client_test.rb => client_spec.rb

  • Loading branch information...
commit 082fcd4b120cc386a95442f339167f6ebabf2148 1 parent 959e417
@weppos authored
View
13 spec/spec_helper.rb
@@ -11,7 +11,20 @@ def with_definitions(&block)
end
end
+module ConnectivityHelpers
+ def need_connectivity(&block)
+ if connectivity_available?
+ yield
+ end
+ end
+
+ def connectivity_available?
+ ENV["ONLINE"].to_i == 1
+ end
+end
+
RSpec.configure do |config|
config.mock_with :mocha
config.include Helpers
+ config.extend ConnectivityHelpers
end
View
102 spec/whois/client_spec.rb
@@ -0,0 +1,102 @@
+require "spec_helper"
+
+describe Whois::Client do
+
+ before :each do
+ @client = Whois::Client.new
+ end
+
+ context ".new" do
+ it "should initialize" do
+ client = Whois::Client.new
+ client.should be_instance_of(Whois::Client)
+ end
+
+ it "should initialize with timeout" do
+ client = Whois::Client.new(:timeout => 100)
+ client.timeout.should == 100
+ end
+
+ it "should initialize with block" do
+ Whois::Client.new do |client|
+ client.should be_instance_of(Whois::Client)
+ end
+ end
+ end
+
+ context "#query" do
+ it "should coerce qstring to string" do
+ server = Object.new
+ # I can't use the String because Array#to_s behaves differently
+ # on Ruby 1.8.7 and Ruby 1.9.1
+ # http://redmine.ruby-lang.org/issues/show/2617
+ server.expects(:query).with(instance_of(String))
+ Whois::Server.expects(:guess).with(instance_of(String)).returns(server)
+ @client.query(["google", ".", "com"])
+ end
+
+ it "should detect email" do
+ lambda { @client.query("weppos@weppos.net") }.should
+ raise_error(Whois::ServerNotSupported)
+ end
+
+ it "should work with domain with no whois" do
+ Whois::Server.define(:tld, ".nowhois", nil, :adapter => Whois::Server::Adapters::None)
+
+ lambda { @client.query("domain.nowhois") }.should
+ raise_error(Whois::NoInterfaceError, /no whois server/)
+ end
+
+ it "should work with domain with web whois" do
+ Whois::Server.define(:tld, ".webwhois", nil, :adapter => Whois::Server::Adapters::Web, :web => "www.nic.test")
+
+ lambda { @client.query("domain.webwhois") }.should raise_error(Whois::WebInterfaceError) do |error|
+ error.message.should match(/no whois server/)
+ error.message.should match(/www\.nic\.test/)
+ end
+ end
+
+ it "should raise if timeout is exceeded" do
+ server = Class.new do
+ def query(*args)
+ sleep(2)
+ end
+ end
+ Whois::Server.expects(:guess).returns(server.new)
+ @client.timeout = 1
+ lambda { @client.query("foo.com") }.should raise_error(Timeout::Error)
+ end
+
+ it "should not raise if timeout is not exceeded" do
+ server = Class.new do
+ def query(*args)
+ sleep(1)
+ end
+ end
+ Whois::Server.expects(:guess).returns(server.new)
+ @client.timeout = 5
+ lambda { @client.query("foo.com") }.should_not raise_error
+ end
+
+ it "should support unlimited timeout" do
+ server = Class.new do
+ def query(*args)
+ sleep(1)
+ end
+ end
+ Whois::Server.expects(:guess).returns(server.new)
+ @client.timeout = nil
+ lambda { @client.query("foo.com") }.should_not raise_error
+ end
+
+ end
+
+ need_connectivity do
+ specify "#query with domain" do
+ answer = @client.query("weppos.it")
+ assert answer.match?(/Domain:\s+weppos\.it/)
+ assert answer.match?(/Created:/)
+ end
+ end
+
+end
View
100 test/whois/client_test.rb
@@ -1,100 +0,0 @@
-require 'test_helper'
-
-class ClientTest < Test::Unit::TestCase
-
- def setup
- @client = Whois::Client.new
- end
-
-
- def test_initialize
- client = Whois::Client.new
- assert_instance_of Whois::Client, client
- end
-
- def test_initialize_with_timeout
- client = Whois::Client.new(:timeout => 100)
- assert_equal 100, client.timeout
- end
-
- def test_initialize_with_block
- Whois::Client.new do |client|
- assert_instance_of Whois::Client, client
- end
- end
-
-
- def test_query_should_cast_qstring_to_string
- server = Object.new
- # I can't use the String because Array#to_s behaves differently
- # on Ruby 1.8.7 and Ruby 1.9.1
- # http://redmine.ruby-lang.org/issues/show/2617
- server.expects(:query).with(instance_of(String))
- Whois::Server.expects(:guess).with(instance_of(String)).returns(server)
- @client.query(["google", ".", "com"])
- end
-
- def test_query_with_email_address_should_raise
- assert_raise(Whois::ServerNotSupported) { @client.query("weppos@weppos.net") }
- end
-
- def test_query_with_domain_with_no_whois
- Whois::Server.define(:tld, ".nowhois", nil, :adapter => Whois::Server::Adapters::None)
-
- error = assert_raise(Whois::NoInterfaceError) { @client.query("domain.nowhois") }
- assert_match /no whois server/, error.message
- end
-
- def test_query_with_domain_with_web_whois
- Whois::Server.define(:tld, ".webwhois", nil, :adapter => Whois::Server::Adapters::Web, :web => "www.nic.test")
-
- error = assert_raise(Whois::WebInterfaceError) { @client.query("domain.webwhois") }
- assert_match /no whois server/, error.message
- assert_match /www\.nic\.test/, error.message
- end
-
- def test_query_with_timeout
- server = Class.new do
- def query(*args)
- sleep(2)
- end
- end
- Whois::Server.expects(:guess).returns(server.new)
- @client.timeout = 1
- assert_raise(Timeout::Error) { @client.query("foo.com") }
- end
-
- def test_query_with_unlimited_timeout
- server = Class.new do
- def query(*args)
- sleep(1)
- end
- end
- Whois::Server.expects(:guess).returns(server.new)
- @client.timeout = nil
- assert_nothing_raised { @client.query("foo.com") }
- end
-
- def test_query_within_timeout
- server = Class.new do
- def query(*args)
- sleep(1)
- end
- end
- Whois::Server.expects(:guess).returns(server.new)
- @client.timeout = 5
- assert_nothing_raised { @client.query("foo.com") }
- end
-
-
- need_connectivity do
-
- def test_query_with_domain
- answer = @client.query("weppos.it")
- assert answer.match?(/Domain:\s+weppos\.it/)
- assert answer.match?(/Created:/)
- end
-
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.