Skip to content

Fail on non successful network requests #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/premailer/rails/css_loaders/network_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module NetworkLoader

def load(url)
uri = uri_for_url(url)
Net::HTTP.get(uri, { 'Accept' => 'text/css' }) if uri
if uri
response = Net::HTTP.get_response(uri, { 'Accept' => 'text/css' })
response.body if response.is_a?(Net::HTTPSuccess)
end
end

def uri_for_url(url)
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/css_loaders/network_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@
end
end
end

describe '#load' do
subject { described_class.load(url) }
let(:url) { 'https://example.com/style.css' }

context 'when resource exists' do
it 'returns the body' do
response = Net::HTTPSuccess.new('1.1', 200, nil)
allow(response).to receive(:body).and_return('BODY')
allow(Net::HTTP).to receive(:get_response).and_return(response)

expect(subject).to eq('BODY')
end
end

context 'when resource is not found' do
it 'returns the body' do
response = Net::HTTPNotFound.new('1.1', 404, 'Not Found')
allow(Net::HTTP).to receive(:get_response).and_return(response)

expect(subject).to be_nil
end
end
end
end