Skip to content

Commit 298afdc

Browse files
responding to comments - rewriting tests and moving logic
1 parent 2013f18 commit 298afdc

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

lib/mongo/srv/result.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ def normalize_hostname(host)
118118
# @raise [ Mongo::Error::MismatchedDomain ] If the record's domain name doesn't match that of
119119
# the hostname.
120120
def validate_same_origin!(record_host)
121-
srv_is_less_than_three_parts = query_hostname.split('.').length < 3
122-
srv_host_domain = if srv_is_less_than_three_parts
123-
query_hostname.split('.')
124-
else
125-
query_hostname.split('.')[1..-1]
121+
srv_host_domain = query_hostname.split('.')
122+
srv_is_less_than_three_parts = srv_host_domain.length < 3
123+
unless srv_is_less_than_three_parts
124+
srv_host_domain = srv_host_domain[1..-1]
126125
end
127126
record_host_parts = record_host.split('.')
128127

lib/mongo/uri/srv_protocol.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def validate_srv_hostname(hostname)
184184
if parts.any?(&:empty?)
185185
raise_invalid_error!("Hostname cannot have consecutive dots: #{hostname}")
186186
end
187-
if parts.length == 0
187+
if parts.length < 1
188188
raise_invalid_error!("Hostname cannot be empty: #{hostname}")
189189
end
190190
end

spec/mongo/srv/result_spec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,30 @@
4242
let(:host_name) { example_host_names[i] }
4343
let(:mismatched_host_name) { example_host_names_that_do_not_match_parent[i] }
4444
context 'when address does not match parent domain' do
45-
it 'raises MismatchedDomain error' do
46-
record = double('record').tap do |record|
45+
let(:record) do
46+
double('record').tap do |record|
4747
allow(record).to receive(:target).and_return(mismatched_host_name)
4848
allow(record).to receive(:port).and_return(42)
4949
allow(record).to receive(:ttl).and_return(1)
5050
end
51-
51+
end
52+
it 'raises MismatchedDomain error' do
5253
expect {
5354
result = described_class.new(srv_name)
5455
result.add_record(record)
5556
}.to raise_error(Mongo::Error::MismatchedDomain)
5657
end
5758
end
59+
5860
context 'when address matches parent domain' do
59-
it 'adds the record' do
60-
record = double('record').tap do |record|
61+
let(:record) do
62+
double('record').tap do |record|
6163
allow(record).to receive(:target).and_return(host_name)
6264
allow(record).to receive(:port).and_return(42)
6365
allow(record).to receive(:ttl).and_return(1)
6466
end
65-
67+
end
68+
it 'adds the record' do
6669
result = described_class.new(srv_name)
6770
result.add_record(record)
6871

@@ -72,13 +75,14 @@
7275

7376
if i < 2
7477
context 'when the address is less than 3 parts' do
75-
it 'does not accept address if it does not contain an extra domain level' do
76-
record = double('record').tap do |record|
78+
let(:record) do
79+
double('record').tap do |record|
7780
allow(record).to receive(:target).and_return(srv_name)
7881
allow(record).to receive(:port).and_return(42)
7982
allow(record).to receive(:ttl).and_return(1)
8083
end
81-
84+
end
85+
it 'does not accept address if it does not contain an extra domain level' do
8286
expect {
8387
result = described_class.new(srv_name)
8488
result.add_record(record)

0 commit comments

Comments
 (0)