|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Helper do |
| 4 | + describe '.url_to_registrable_domain' do |
| 5 | + it 'collapses subdomains to the registrable domain' do |
| 6 | + expect(described_class.url_to_registrable_domain('https://blog.example.com/posts')).to eq('example.com') |
| 7 | + end |
| 8 | + |
| 9 | + it 'keeps multi-part TLDs intact for registrable domain' do |
| 10 | + expect(described_class.url_to_registrable_domain('https://news.bbc.co.uk/world')).to eq('bbc.co.uk') |
| 11 | + end |
| 12 | + |
| 13 | + it 'preserves single-host domains' do |
| 14 | + expect(described_class.url_to_registrable_domain('https://example.com')).to eq('example.com') |
| 15 | + end |
| 16 | + |
| 17 | + it 'returns nil for blank or invalid URLs', :aggregate_failures do |
| 18 | + expect(described_class.url_to_registrable_domain(nil)).to be_nil |
| 19 | + expect(described_class.url_to_registrable_domain('')).to be_nil |
| 20 | + expect(described_class.url_to_registrable_domain('not a url')).to be_nil |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe '.url_to_host_name' do |
| 25 | + it 'returns the full host' do |
| 26 | + expect(described_class.url_to_host_name('https://news.bbc.co.uk/world')).to eq('news.bbc.co.uk') |
| 27 | + end |
| 28 | + |
| 29 | + it 'returns nil for blank or invalid URLs', :aggregate_failures do |
| 30 | + expect(described_class.url_to_host_name(nil)).to be_nil |
| 31 | + expect(described_class.url_to_host_name('')).to be_nil |
| 32 | + expect(described_class.url_to_host_name('not a url')).to be_nil |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe 'legacy naming guardrail' do |
| 37 | + it 'does not expose url_to_directory_name' do |
| 38 | + expect(described_class).not_to respond_to(:url_to_directory_name) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe '.registrable_domain' do |
| 43 | + it 'falls back to host when PublicSuffix returns nil' do |
| 44 | + allow(PublicSuffix).to receive(:domain).with('example.local').and_return(nil) |
| 45 | + |
| 46 | + expect(described_class.send(:registrable_domain, 'example.local')).to eq('example.local') |
| 47 | + end |
| 48 | + |
| 49 | + it 'falls back to host when PublicSuffix raises DomainInvalid' do |
| 50 | + allow(PublicSuffix).to receive(:domain).with('invalid..host') |
| 51 | + .and_raise(PublicSuffix::DomainInvalid) |
| 52 | + |
| 53 | + expect(described_class.send(:registrable_domain, 'invalid..host')).to eq('invalid..host') |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments