Class: MailResolver::Testing::LocalNameserver
- Inherits:
-
Object
- Object
- MailResolver::Testing::LocalNameserver
- Defined in:
- lib/mailresolver/testing.rb
Overview
A loopback nameserver for one rcode (records for :noerror, [name, record] for an alias, nil for a timeout), with replies encoded via Resolv.
LocalNameserver.answering(:servfail) do |resolver|
assert_raises(MailResolver::ServerFailure) { resolver.txt("example.com") }
end
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
- #close ⇒ Object
-
#initialize(rcode, records: []) ⇒ LocalNameserver
constructor
A new instance of LocalNameserver.
- #port ⇒ Object
- #resolver(timeouts: TIMEOUTS) ⇒ Object
Constructor Details
#initialize(rcode, records: []) ⇒ LocalNameserver
Returns a new instance of LocalNameserver.
103 104 105 106 107 108 |
# File 'lib/mailresolver/testing.rb', line 103 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
96 97 98 99 100 101 |
# File 'lib/mailresolver/testing.rb', line 96 def self.answering(rcode, records: [], timeouts: TIMEOUTS) nameserver = new(rcode, records: records) yield nameserver.resolver(timeouts: timeouts) ensure nameserver&.close end |
Instance Method Details
#close ⇒ Object
118 119 120 121 |
# File 'lib/mailresolver/testing.rb', line 118 def close @thread.kill @socket.close end |
#port ⇒ Object
110 111 112 |
# File 'lib/mailresolver/testing.rb', line 110 def port @socket.addr[1] end |
#resolver(timeouts: TIMEOUTS) ⇒ Object
114 115 116 |
# File 'lib/mailresolver/testing.rb', line 114 def resolver(timeouts: TIMEOUTS) LocalResolver.new(port, timeouts: timeouts) end |