|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe DnsMock::Record::Factory::Srv do |
| 4 | + it { expect(described_class).to be < DnsMock::Record::Factory::Base } |
| 5 | + |
| 6 | + describe '#instance_params' do |
| 7 | + subject(:instance_params) { described_class.new(record_data: record_data).instance_params } |
| 8 | + |
| 9 | + let(:int_params) { (0..3).to_a } |
| 10 | + let(:dns_name) { random_hostname } |
| 11 | + let(:record_data) { int_params + [dns_name] } |
| 12 | + let(:expected_data) do |
| 13 | + [ |
| 14 | + *int_params, |
| 15 | + create_dns_name(dns_name) |
| 16 | + ] |
| 17 | + end |
| 18 | + |
| 19 | + it 'returns prepared target class instance params' do |
| 20 | + expect(instance_params).to eq(expected_data) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe '#create' do |
| 25 | + subject(:create_factory) { described_class.new(record_data: record_data.values).create } |
| 26 | + |
| 27 | + let(:record_data) do |
| 28 | + { |
| 29 | + priority: 0, |
| 30 | + weight: 10, |
| 31 | + port: 5_060, |
| 32 | + target: target |
| 33 | + } |
| 34 | + end |
| 35 | + |
| 36 | + context 'when valid record context' do |
| 37 | + shared_examples 'returns instance of target class' do |
| 38 | + it 'returns instance of target class' do |
| 39 | + expect(DnsMock::Representer::Punycode).to receive(:call).with(target).and_call_original |
| 40 | + expect(create_factory).to be_an_instance_of(described_class.target_class) |
| 41 | + dns_names = record_data.slice(:target).transform_values do |value| |
| 42 | + create_dns_name(ascii_hostname ? value : to_punycode_hostname(value)) |
| 43 | + end |
| 44 | + record_data.merge(dns_names).each { |key, value| expect(create_factory.public_send(key)).to eq(value) } |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context 'when ASCII hostname' do |
| 49 | + let(:ascii_hostname) { true } |
| 50 | + let(:target) { random_hostname } |
| 51 | + |
| 52 | + include_examples 'returns instance of target class' |
| 53 | + end |
| 54 | + |
| 55 | + context 'when non ASCII hostname' do |
| 56 | + let(:ascii_hostname) { false } |
| 57 | + let(:target) { random_non_ascii_hostname } |
| 58 | + |
| 59 | + include_examples 'returns instance of target class' |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context 'when invalid record context' do |
| 64 | + context 'when invalid mname' do |
| 65 | + let(:target) { 42 } |
| 66 | + let(:error_context) { "cannot interpret as DNS name: #{target}. Invalid SRV record context" } |
| 67 | + |
| 68 | + it_behaves_like 'target class exception wrapper' |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments