mirror of https://github.com/mastodon/mastodon
Add `DomainResource` class to wrap MX lookup/normalize (#32864)
parent
e8b6607ece
commit
62d65504f6
@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DomainResource
|
||||
attr_reader :domain
|
||||
|
||||
RESOLVE_TIMEOUT = 5
|
||||
|
||||
def initialize(domain)
|
||||
@domain = domain
|
||||
end
|
||||
|
||||
def mx
|
||||
Resolv::DNS.open do |dns|
|
||||
dns.timeouts = RESOLVE_TIMEOUT
|
||||
dns
|
||||
.getresources(domain, Resolv::DNS::Resource::IN::MX)
|
||||
.to_a
|
||||
.map { |mx| mx.exchange.to_s }
|
||||
.compact_blank
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DomainResource do
|
||||
describe '#mx' do
|
||||
subject { described_class.new(domain) }
|
||||
|
||||
let(:domain) { 'example.host' }
|
||||
let(:exchange) { 'mx.host' }
|
||||
|
||||
before { configure_mx(domain: domain, exchange: exchange) }
|
||||
|
||||
it 'returns array of hostnames' do
|
||||
expect(subject.mx)
|
||||
.to eq([exchange])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue