Skip to content
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

Removes https get accept header when using Ruby version < 3.0 #286

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 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,11 @@ module NetworkLoader

def load(url)
uri = uri_for_url(url)
Net::HTTP.get(uri, { 'Accept' => 'text/css' }) if uri
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")
Net::HTTP.get(uri, { 'Accept' => 'text/css' }) if uri
else
Net::HTTP.get(uri) if uri
end
end

def uri_for_url(url)
Expand Down
173 changes: 125 additions & 48 deletions spec/integration/css_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,78 @@ def expect_file(path, content='file content')
let(:response) { 'content of base.css' }
let(:uri) { URI('http://example.com/assets/base.css') }

it "falls back to Net::HTTP" do
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(TypeError)

allow(Net::HTTP).to \
receive(:get)
.with(uri, { 'Accept' => 'text/css' })
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
context "with Ruby version < 3.0" do
it "falls back to Net::HTTP" do
stub_const('RUBY_VERSION', '2.7.8')
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(TypeError)

allow(Net::HTTP).to \
receive(:get)
.with(uri)
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
end
end

context "with Ruby version >= 3.0" do
it "falls back to Net::HTTP" do
stub_const('RUBY_VERSION', '3.0.0')
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(TypeError)

allow(Net::HTTP).to \
receive(:get)
.with(uri, { 'Accept' => 'text/css' })
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
end
end

end

context "when find_sources raises Errno::ENOENT" do
let(:response) { 'content of base.css' }
let(:uri) { URI('http://example.com/assets/base.css') }

it "falls back to Net::HTTP" do
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(Errno::ENOENT)

allow(Net::HTTP).to \
receive(:get)
.with(uri, { 'Accept' => 'text/css' })
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
context "with Ruby version < 3.0" do
it "falls back to Net::HTTP" do
stub_const('RUBY_VERSION', '2.7.8')
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(Errno::ENOENT)

allow(Net::HTTP).to \
receive(:get)
.with(uri)
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
end
end

context "with Ruby version >= 3.0" do
it "falls back to Net::HTTP" do
stub_const('RUBY_VERSION', '3.0.0')
expect(Rails.application.assets_manifest).to \
receive(:find_sources)
.with('base.css')
.and_raise(Errno::ENOENT)

allow(Net::HTTP).to \
receive(:get)
.with(uri, { 'Accept' => 'text/css' })
.and_return(response)
expect(css_for_url('http://example.com/assets/base.css')).to \
eq(response)
end
end
end

Expand Down Expand Up @@ -179,37 +220,78 @@ def expect_file(path, content='file content')
let(:url) { "http://assets.example.com#{path}" }
let(:asset_host) { 'http://assets.example.com' }

before do
allow(Rails.application.assets_manifest).to \
receive(:find_sources).and_return([])
context "with Ruby version < 3.0" do
before do
stub_const('RUBY_VERSION', '2.7.8')

config = double(asset_host: asset_host)
allow(Rails.configuration).to \
receive(:action_controller).and_return(config)
allow(Rails.application.assets_manifest).to \
receive(:find_sources).and_return([])

allow(Net::HTTP).to \
receive(:get)
.with(URI(url), { 'Accept' => 'text/css' })
.and_return(response)
end
config = double(asset_host: asset_host)
allow(Rails.configuration).to \
receive(:action_controller).and_return(config)

it 'requests the file' do
expect(css_for_url(url)).to eq('content of base.css')
allow(Net::HTTP).to \
receive(:get)
.with(URI(url))
.and_return(response)
end

it 'requests the file' do
expect(css_for_url(url)).to eq('content of base.css')
end

context 'when file url does not include the host' do
it 'requests the file using the asset host as host' do
expect(css_for_url(path)).to eq('content of base.css')
end

context 'and the asset host uses protocol relative scheme' do
let(:asset_host) { '//assets.example.com' }

it 'requests the file using http as the scheme' do
expect(css_for_url(path)).to eq('content of base.css')
end
end
end
end

context 'when file url does not include the host' do
it 'requests the file using the asset host as host' do
expect(css_for_url(path)).to eq('content of base.css')
context "with Ruby version >= 3.0" do

before do
stub_const('RUBY_VERSION', '3.0.0')
allow(Rails.application.assets_manifest).to \
receive(:find_sources).and_return([])

config = double(asset_host: asset_host)
allow(Rails.configuration).to \
receive(:action_controller).and_return(config)

allow(Net::HTTP).to \
receive(:get)
.with(URI(url), { 'Accept' => 'text/css' })
.and_return(response)
end

context 'and the asset host uses protocol relative scheme' do
let(:asset_host) { '//assets.example.com' }
it 'requests the file' do
expect(css_for_url(url)).to eq('content of base.css')
end

it 'requests the file using http as the scheme' do
context 'when file url does not include the host' do
it 'requests the file using the asset host as host' do
expect(css_for_url(path)).to eq('content of base.css')
end

context 'and the asset host uses protocol relative scheme' do
let(:asset_host) { '//assets.example.com' }

it 'requests the file using http as the scheme' do
expect(css_for_url(path)).to eq('content of base.css')
end
end
end
end

end
end
elsif defined?(::Propshaft)
Expand Down Expand Up @@ -280,11 +362,6 @@ def expect_file(path, content='file content')
config = double(asset_host: asset_host)
allow(Rails.configuration).to \
receive(:action_controller).and_return(config)

allow(Net::HTTP).to \
receive(:get)
.with(URI(url), { 'Accept' => 'text/css' })
.and_return(response)
end

it 'requests the file' do
Expand Down