# frozen_string_literal: true

require "spec_helper"

describe Whois do
  let(:response) { "Domain:             example.it\nStatus:             AVAILABLE\n" }

  describe "Basic WHOIS querying and parsing" do
    it "queries and returns a record" do
      with_definitions do
        Whois::Server.define(:tld, "it", "whois.nic.it")
        expect(Whois::Server::Adapters::Standard.query_handler).to receive(:call)
          .with("example.it", "whois.nic.it", 43)
          .and_return(response)

        record = described_class.lookup("example.it")

        expect(record).to be_a(Whois::Record)
        # expect(record.available?).to be_truthy
        # expect(record.registered?).to be_falsey

        # expect(record.parser).to be_a(Whois::Record::Parser)
        # expect(record.parser.parsers.first).to be_a(Whois::Record::Parser::WhoisNicIt)
      end
    end
  end

  describe "Passing :bind_host and :bind_port options" do
    it "binds the WHOIS query to given host and port" do
      with_definitions do
        Whois::Server.define(:tld, "it", "whois.nic.it")
        expect(Whois::Server::Adapters::Standard.query_handler).to receive(:call)
          .with("example.it", "whois.nic.it", 43, "192.168.1.1", 3000)
          .and_return(response)

        client = Whois::Client.new(bind_host: "192.168.1.1", bind_port: 3000)
        client.lookup("example.it")
      end
    end
  end

  describe "Passing :bind_port options" do
    it "binds the WHOIS query to given port and defaults host" do
      with_definitions do
        Whois::Server.define(:tld, "it", "whois.nic.it")
        expect(Whois::Server::Adapters::Standard.query_handler).to receive(:call)
          .with("example.it", "whois.nic.it", 43, Whois::Server::Adapters::Base::DEFAULT_BIND_HOST, 3000)
          .and_return(response)

        client = Whois::Client.new(bind_port: 3000)
        client.lookup("example.it")
      end
    end
  end

  describe "Passing :host options" do
    it "forces the WHOIS query to given host" do
      with_definitions do
        Whois::Server.define(:tld, "it", "whois.nic.it")
        expect(Whois::Server::Adapters::Standard.query_handler).to receive(:call)
          .with("example.it", "whois.example.com", 43)
          .and_return(response)

        client = Whois::Client.new(host: "whois.example.com")
        record = client.lookup("example.it")

        expect(record.parts.size).to eq(1)
        expect(record.parts.first.body).to eq(response)
        expect(record.parts.first.host).to eq("whois.example.com")
      end
    end
  end
end
