rails icon indicating copy to clipboard operation
rails copied to clipboard

Unexpected routes in console using app.

Open woto opened this issue 3 years ago • 8 comments

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!

woto avatar Dec 25 '22 12:12 woto

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.

ghiculescu avatar Dec 26 '22 18:12 ghiculescu

Maybe we should log a warning here if the host starts with "http://" or "https://" ?

ghiculescu avatar Dec 26 '22 19:12 ghiculescu

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

woto avatar Dec 27 '22 08:12 woto

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.

rails-bot[bot] avatar Mar 27 '23 08:03 rails-bot[bot]

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"

zzak avatar Apr 07 '23 02:04 zzak

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.

rails-bot[bot] avatar Jul 06 '23 02:07 rails-bot[bot]

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.

rails-bot[bot] avatar Oct 11 '23 23:10 rails-bot[bot]

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.

rails-bot[bot] avatar Jan 15 '24 17:01 rails-bot[bot]