Class: MailResolver::Testing::LocalNameserver

Inherits:
Object
  • Object
show all
Defined in:
lib/mailresolver/testing.rb

Overview

A loopback nameserver answering every query with one rcode, and a Resolver pointed at it. Replies are encoded with Resolv, so the decoder under test is a different implementation than the encoder serving it.

LocalNameserver.answering(:servfail) do |resolver|
assert_raises(MailResolver::ServerFailure) { resolver.txt("example.com") }
end

Records answer a :noerror query; a [ name, record ] pair answers under a name of its own, which is how an alias points somewhere else. An rcode of nil never replies at all, which is what a timeout looks like from here.

Defined Under Namespace

Classes: LocalResolver

Constant Summary collapse

RCODES =
{ noerror: 0, servfail: 2, nxdomain: 3, refused: 5 }.freeze
TIMEOUTS =
[ 0.5 ].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rcode, records: []) ⇒ LocalNameserver

Returns a new instance of LocalNameserver.



117
118
119
120
121
122
# File 'lib/mailresolver/testing.rb', line 117

def initialize(rcode, records: [])
  @rcode, @records = rcode, records
  @socket = UDPSocket.new
  @socket.bind("127.0.0.1", 0)
  @thread = Thread.new { serve }
end

Class Method Details

.answering(rcode, records: [], timeouts: TIMEOUTS) ⇒ Object



110
111
112
113
114
115
# File 'lib/mailresolver/testing.rb', line 110

def self.answering(rcode, records: [], timeouts: TIMEOUTS)
  nameserver = new(rcode, records: records)
  yield nameserver.resolver(timeouts: timeouts)
ensure
  nameserver&.close
end

Instance Method Details

#closeObject



132
133
134
135
# File 'lib/mailresolver/testing.rb', line 132

def close
  @thread.kill
  @socket.close
end

#portObject



124
125
126
# File 'lib/mailresolver/testing.rb', line 124

def port
  @socket.addr[1]
end

#resolver(timeouts: TIMEOUTS) ⇒ Object



128
129
130
# File 'lib/mailresolver/testing.rb', line 128

def resolver(timeouts: TIMEOUTS)
  LocalResolver.new(port, timeouts: timeouts)
end