Skip to content

Commit

Permalink
initial example
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsauve committed Mar 27, 2024
1 parent 7f9848d commit f7f2c08
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions example-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
require 'minitest/autorun'
require 'mocha'
require 'mocha/minitest'
require_relative 'example'

class ExampleTest < Minitest::Test

DOMAIN = 'example.com'
TXT_RECORDS = ['hello', 'world']

def test_get_txt
records = TXT_RECORDS.map do |r|
record = mock
record.expects(:data).with.returns(r)
record
end

dns = mock
dns.expects(:getresources).with(DOMAIN, Resolv::DNS::Resource::IN::TXT).returns(records)

Resolv::DNS
.expects(:open)
.yields(dns)
.returns(records) # I should not have to do this, and it allows bad implementations to not actually return the results properly

result = Example.get_txt(DOMAIN)

assert_equal TXT_RECORDS, result
end
end
17 changes: 17 additions & 0 deletions example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
require 'resolv'

class Example
def self.get_txt(domain)
resources =
Resolv::DNS.open do |dns|
dns.getresources(domain, Resolv::DNS::Resource::IN::TXT)
end

resources.map { |r| r.data }
end
end

if __FILE__ == $PROGRAM_NAME
puts Example.get_txt('google.com')
end

0 comments on commit f7f2c08

Please sign in to comment.