Unexpected routes in console using app.
Steps to reproduce
irb(main):013:0> host = "https://host.ru:443"
=> "https://host.ru:443"
irb(main):014:0> Rails.application.routes.url_helpers.tests_url(host: host)
=> "https://host.ru/tests"
irb(main):015:0> app.tests_url(host: host)
=> "http://host.ru:443/tests"
irb(main):016:0>
irb(main):017:0>
irb(main):018:0> host = "https://host.ru:443/"
=> "https://host.ru:443/"
irb(main):019:0> Rails.application.routes.url_helpers.tests_url(host: host)
=> "https://host.ru/tests"
irb(main):020:0> app.tests_url(host: host)
=> "http://host.ru/tests"
Expected behavior
In all cases I expect that output will be https://host.ru/tests
Actual behavior
Unexpected results built with app. are http://host.ru:443/tests and http://host.ru/tests
System configuration
Rails version: 7.0.4
Ruby version: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
Cheers!
This is the cause:
3.1.3 :002 > app.url_options
=> {:host=>"www.example.com", :protocol=>"http"}
3.1.3 :003 > Rails.application.routes.url_helpers.url_options
=> {}
These get merged into what you pass to the route. So the fact you're passing in a https host is overridden by the http protocol.
If you do app.https! at the start of your console session, you get the expected behaviour, because of the HTTPS check here.
Alternatively you could do app.tests_url(host: host, protocol: "https").
Better yet, if you did Rails.application.default_url_options = { host: host, protocol: "https" } in an initializer, this issue should go away permanently.
Maybe we should log a warning here if the host starts with "http://" or "https://" ?
Yeah. I know how to avoid the problem. The most annoying part of this that I thought that this methods must give identical results. Like if agree
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
I agree this is surprising, can reproduce it simply with a newly generated application like this:
# bin/rails c
# Loading development environment (Rails 7.1.0.alpha)
>> app.url_options
=> {:host=>"www.example.com", :protocol=>"http"}
>> app.rails_health_check_url
=> "http://www.example.com/up"
>> app.rails_health_check_url host: "https://github.com"
=> "http://github.com/up"
>> app.rails_health_check_url host: "github.com", protocol: "https"
=> "https://github.com/up"
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.