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
Fix malformed asset_url in ActionController::Renderer #28250
Fix malformed asset_url in ActionController::Renderer #28250
Conversation
renderer = ApplicationController.renderer | ||
content = renderer.render template: "test/render_file_with_asset" | ||
|
||
assert_equal "http://example.org/asset.jpg", content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also test the scenario where a renderer is initialized with url_scheme: "https"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Erol Done!
What's the need to switch to |
@pixeltrix Take a look at issue #28151 . The problem is described inside the conversation. I don't know if something has changed in Rack but the |
@tzabaman that's a good first step - you've found out the how, but we need to know when and why too. So the first step is to write a standalone test case that we can use to test against multiple versions: begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails", "5.0.2"
end
require "rack/test"
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_token = "secret_token"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
end
end
class ApplicationController < ActionController::Base; end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_default_renderer_with_asset_url
template = "<%= asset_url('avatar.jpg') %>"
renderer = ApplicationController.renderer
rendered = renderer.render inline: template
assert_equal "http://example.org/avatar.jpg", rendered
end
end So starting with Rails 5.0.2 what do we get?
Okay, so what about Rails 5.0.0? Change the gemspec to the following: gemfile(true) do
source "https://rubygems.org"
gem "rails", "5.0.0"
end Re-run the test and what do we get?
Nope, still broken. Looking at the history for gemfile(true) do
source "https://rubygems.org"
gem "rails", github: "rails/rails", ref: "801e399" # commit where feature was introduced
gem "arel", github: "rails/arel", ref: "56040f2" # arel master HEAD at the time
end So surely this will work, right?
Nope, seems like this bug has aways been around. 😞 Okay, we now know when - it's always been broken. The why that obviously follows on is that no-one wrote a test for it. So what about fixing it? Well, since "rack.url_scheme" => https? ? "https" : "http" Can we use a similar technique to fix this problem? Turns out if we change the def normalize_keys(env)
new_env = {}
env.each_pair { |k,v| new_env[rack_key_for(k)] = rack_value_for(k, v) }
new_env['rack.url_scheme'] = new_env['HTTPS'] == 'on' ? 'https' : 'http'
new_env
end and re-run our test then we get:
🎉 - the bug is fixed without anyone having to change their code. So @tzabaman can you update your PR with this fix and remove the deprecation? Thanks 👍 |
@tzabaman I think something went wrong with your rebase - it's showing 86 commits. |
@pixeltrix Maybe that happened because I rebased my branch with rails master... Do you propose to open a new PR and drop this one? |
@tzabaman try squashing all your commits into a single commit and then rebase that one against master |
c0bc1c5
to
32046de
Compare
@pixeltrix Thank you very much for your precious help! The methodology with which you approached and finally solved the issue will be very useful for future similar situations! 👍 |
@tzabaman thanks! 👍 |
Backported to 5-0-stable in f69edbe |
@tzabaman for future reference we normally add a CHANGELOG entry, thanks again. |
@pixeltrix Thank you, I will have it in mind next time! |
Summary
#28151
Fix malformed asset_url when rendering a template with ActionController::Renderer