require 'celluloid/dns'
require 'yaml'

module Celluloid::DNS::TransactionSpec
	SERVER_PORTS = [[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]]
	IN = Resolv::DNS::Resource::IN
	
	describe Celluloid::DNS::Transaction do
		let(:server) { Celluloid::DNS::Server.new }
		let(:query) { Celluloid::DNS::Message.new(0) }
		let(:question) { Resolv::DNS::Name.create("www.google.com.") }
		let(:response) { Celluloid::DNS::Message.new(0) }
		let(:resolver) { Celluloid::DNS::Resolver.new([[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]])}
		
		it "should append an address" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::A, response)
			
			transaction.respond!("1.2.3.4")
			
			expect(transaction.response.answer[0][0]).to be == question
			expect(transaction.response.answer[0][2].address.to_s).to be == "1.2.3.4"
		end
		
		it "should passthrough the request" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::A, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.size).to be > 0
		end
		
		it "should return a response on passthrough" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::A, response)
			
			expect(transaction.response.answer.size).to be 0
			
			response = transaction.passthrough(resolver)
			
			expect(response.answer.length).to be > 0
		end
		
		it "should call the block with the response when invoking passthrough!" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::A, response)
			
			expect(transaction.response.answer.size).to be 0
			
			passthrough_response = nil
			
			transaction.passthrough!(resolver) do |response|
				passthrough_response = response
			end
			
			expect(passthrough_response.answer.length).to be > 0
		end
		
		it "should fail the request" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::A, response)
			
			transaction.fail! :NXDomain
			
			expect(transaction.response.rcode).to be Resolv::DNS::RCode::NXDomain
		end
		
		it "should return AAAA record" do
			transaction = Celluloid::DNS::Transaction.new(server, query, question, IN::AAAA, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.first[2]).to be_kind_of IN::AAAA
		end
		
		it "should return MX record" do
			transaction = Celluloid::DNS::Transaction.new(server,query,"google.com",IN::MX, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.first[2]).to be_kind_of IN::MX
		end
		
		it "should return NS record" do
			transaction = Celluloid::DNS::Transaction.new(server, query, "google.com", IN::NS, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.first[2]).to be_kind_of IN::NS
		end
		
		it "should return PTR record" do
			transaction = Celluloid::DNS::Transaction.new(server, query, "8.8.8.8.in-addr.arpa", IN::PTR, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.first[2]).to be_kind_of IN::PTR
		end
		
		it "should return SOA record" do
			transaction = Celluloid::DNS::Transaction.new(server, query, "google.com", IN::SOA, response)
			
			expect(transaction.response.answer.size).to be 0
			
			transaction.passthrough!(resolver)
			
			expect(transaction.response.answer.first[2]).to be_kind_of IN::SOA
		end
	end
end
