require 'rails_helper'

RSpec.describe Preview::HttpFetcher do
  describe '.fetch' do
    it 'raises FetchFailed when redirect location is invalid' do
      redirect = Net::HTTPFound.new('1.1', '302', 'Found')
      redirect['location'] = 'http://[invalid'

      allow(Preview::UrlSafety).to receive(:validate)
          .with('https://example.com/page')
          .and_return([URI.parse('https://example.com/page'), ['203.0.113.10']])
      allow(described_class).to receive(:request)
          .and_return(redirect)
      allow(Rails.logger).to receive(:warn)

      expect {
        described_class.fetch('https://example.com/page')
      }.to raise_error(Preview::HttpFetcher::FetchFailed)
      expect(Rails.logger).to have_received(:warn)
          .with(/invalid_redirect_location/)
    end
  end
end
