class DNSAdapter::MockClient

A mock client for use in tests.

Constants

RECORD_TYPE_TO_ATTR_NAME_MAP
TIMEOUT

Public Class Methods

new(zone_data) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 6
def initialize(zone_data)
  @zone_data = {}
  zone_data.each do |k, v|
    @zone_data[k.downcase] = v.dup
  end
end

Public Instance Methods

fetch_a_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 13
def fetch_a_records(domain)
  fetch_records(domain, 'A')
end
fetch_aaaa_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 17
def fetch_aaaa_records(domain)
  fetch_records(domain, 'AAAA')
end
fetch_cname_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 33
def fetch_cname_records(domain)
  fetch_records(domain, 'CNAME')
end
fetch_mx_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 21
def fetch_mx_records(domain)
  fetch_records(domain, 'MX')
end
fetch_ns_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 29
def fetch_ns_records(domain)
  fetch_records(domain, 'NS')
end
fetch_ptr_records(arpa_address) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 25
def fetch_ptr_records(arpa_address)
  fetch_records(arpa_address, 'PTR')
end
fetch_records(domain, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 49
def fetch_records(domain, type)
  records = raw_records(domain, type)
  if records.empty?
    check_for_timeout(domain)
  else
    formatted_records(records, type)
  end
end
fetch_spf_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 41
def fetch_spf_records(domain)
  fetch_records(domain, 'SPF')
end
fetch_txt_records(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 37
def fetch_txt_records(domain)
  fetch_records(domain, 'TXT')
end
follow_cname(record_set, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 64
def follow_cname(record_set, type)
  return nil if type == 'CNAME' # Never follow CNAME for a CNAME query
  cname_record = formatted_records(records_for_type(record_set, 'CNAME'), 'CNAME').first
  cname_target = cname_record.try(:[], :name)
  cname_target.present? ? raw_records(cname_target, type) : nil
end
raw_records(domain, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 58
def raw_records(domain, type)
  record_set = find_records_for_domain(domain)
  return [] if record_set.empty?
  follow_cname(record_set, type) || records_for_type(record_set, type)
end
timeouts=(timeouts) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 45
def timeouts=(timeouts)
  # Deliberate NOOP
end

Private Instance Methods

check_for_timeout(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 91
def check_for_timeout(domain)
  record_set = find_records_for_domain(domain)
  return [] if record_set.select { |r| r == TIMEOUT }.empty?
  raise DNSAdapter::TimeoutError
end
find_records_for_domain(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 79
def find_records_for_domain(domain)
  return [] if domain.blank?
  @zone_data[normalize_domain(domain)] || []
end
formatted_records(records, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 108
def formatted_records(records, type)
  records.map do |r|
    val = r[type]
    raise DNSAdapter::TimeoutError if val == TIMEOUT
    value_to_hash(val, type).merge(type: type)
  end
end
mx_hash(value) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 126
def mx_hash(value)
  if value.size > 1
    { preference: value.first, exchange: value.last }
  else
    { exchange: value.last }
  end
end
normalize_domain(domain) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 73
def normalize_domain(domain)
  return if domain.blank?
  domain = domain[0...-1] if domain[domain.length - 1] == '.'
  domain.downcase
end
records_for_type(record_set, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 84
def records_for_type(record_set, type)
  record_set.select do |r|
    r.is_a?(Hash) && r[type] && r[type] != 'NONE'
  end
end
value_to_hash(value, type) click to toggle source
# File lib/dns_adapter/mock_client.rb, line 116
def value_to_hash(value, type)
  if type == 'MX' && value.is_a?(Array)
    mx_hash(value)
  elsif (type == 'TXT' || type == 'SPF') && value.is_a?(Array)
    { text: value.join('') }
  else
    { RECORD_TYPE_TO_ATTR_NAME_MAP[type] => value }
  end
end