Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 134 lines (107 sloc) 4.061 kb
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 132 133 134
require "spec_helper"

describe Whois::Client do

  describe "#initialize" do
    it "accepts a zero parameters" do
      lambda { klass.new }.should_not raise_error
    end

    it "accepts a settings parameter" do
      lambda { klass.new({ :foo => "bar" }) }.should_not raise_error
    end


    it "accepts a timeout setting with a value in seconds" do
      client = klass.new(:timeout => 100)
      client.timeout.should == 100
    end

    it "accepts a timeout setting with a nil value" do
      client = klass.new(:timeout => nil)
      client.timeout.should be_nil
    end

    it "accepts a block" do
      klass.new do |client|
        client.should be_instance_of(klass)
      end
    end


    it "defaults timeout setting to DEFAULT_TIMEOUT" do
      client = klass.new
      client.timeout.should == klass::DEFAULT_TIMEOUT
    end

    it "sets settings to given argument, except timeout" do
      client = klass.new(:timeout => nil, :foo => "bar")
      client.settings.should == { :foo => "bar" }
    end
  end

  describe "#query" do
    it "converts the argument to string" do
      # I can't use the String in place of instance_of(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 = Whois::Server::Adapters::Base.new(:tld, ".test", "whois.test")
      server.expects(:query).with(instance_of(String))
      Whois::Server.expects(:guess).with(instance_of(String)).returns(server)
      klass.new.query(["example", ".", "test"])
    end

    it "converts the argument to downcase" do
      server = Whois::Server::Adapters::Base.new(:tld, ".test", "whois.test")
      server.expects(:query).with("example.test")
      Whois::Server.expects(:guess).with("example.test").returns(server)
      klass.new.query("Example.TEST")
    end

    it "detects email" do
      lambda do
        klass.new.query("weppos@weppos.net")
      end.should raise_error(Whois::ServerNotSupported)
    end

    it "works with domain with no whois" do
      Whois::Server.define(:tld, ".nowhois", nil, :adapter => Whois::Server::Adapters::None)

      lambda do
        klass.new.query("domain.nowhois")
      end.should raise_error(Whois::NoInterfaceError, /no whois server/)
    end

    it "works with domain with web whois" do
      Whois::Server.define(:tld, ".webwhois", nil, :adapter => Whois::Server::Adapters::Web, :url => "http://www.example.com/")

      lambda do
        klass.new.query("domain.webwhois")
      end.should raise_error(Whois::WebInterfaceError, /www\.example\.com/)
    end

    it "raises if timeout is exceeded" do
      adapter = Class.new(Whois::Server::Adapters::Base) do
        def query(*args)
          sleep(2)
        end
      end
      Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "whois.test"))

      client = klass.new(:timeout => 1)
      lambda { client.query("example.test") }.should raise_error(Timeout::Error)
    end

    it "does not raise if timeout is not exceeded" do
      adapter = Class.new(Whois::Server::Adapters::Base) do
        def query(*args)
          sleep(1)
        end
      end
      Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "whois.test"))

      client = klass.new(:timeout => 5)
      lambda { client.query("example.test") }.should_not raise_error
    end

    it "supports unlimited timeout" do
      adapter = Class.new(Whois::Server::Adapters::Base) do
        def query(*args)
          sleep(1)
        end
      end
      Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "whois.test"))

      client = klass.new.tap { |c| c.timeout = nil }
      lambda { client.query("example.test") }.should_not raise_error
    end

  end

  # FIXME: use RSpec metadata
  need_connectivity do
    describe "#query" do
      it "sends a query for given domain" do
        record = klass.new.query("weppos.it")
        assert record.match?(/Domain:\s+weppos\.it/)
        assert record.match?(/Created:/)
      end
    end
  end

end
Something went wrong with that request. Please try again.