aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2017-02-27 17:29:16 +0900
committeryuuji.yaginuma <yuuji.yaginuma@gmail.com>2017-02-27 21:32:57 +0900
commit60aeb6a8f9f1fc161a921c2219650c19b6e753e7 (patch)
tree05aadb730d8e1a3d199cd7ad915063ee4f892a2a /railties
parent2d84a6bc74fbe9d9b97bf7d63ec6f27418d9c3a6 (diff)
downloadrails-60aeb6a8f9f1fc161a921c2219650c19b6e753e7.tar.gz
rails-60aeb6a8f9f1fc161a921c2219650c19b6e753e7.tar.bz2
rails-60aeb6a8f9f1fc161a921c2219650c19b6e753e7.zip
Set correct host except development environment
Currently `localhost` is used for the default host in all environments. But up to Rails 5.0, `0.0.0.0` is used except for development. So fixed to use the same value as 5.0. Fixes #28184
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/commands/server/server_command.rb10
-rw-r--r--railties/test/commands/server_test.rb18
2 files changed, 25 insertions, 3 deletions
diff --git a/railties/lib/rails/commands/server/server_command.rb b/railties/lib/rails/commands/server/server_command.rb
index cde8b575b5..1fa27a3155 100644
--- a/railties/lib/rails/commands/server/server_command.rb
+++ b/railties/lib/rails/commands/server/server_command.rb
@@ -99,8 +99,9 @@ module Rails
class_option :port, aliases: "-p", type: :numeric,
desc: "Runs Rails on the specified port.", banner: :port, default: 3000
- class_option :binding, aliases: "-b", type: :string, default: "localhost",
- desc: "Binds Rails to the specified IP.", banner: :IP
+ class_option :binding, aliases: "-b", type: :string,
+ desc: "Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'.",
+ banner: :IP
class_option :config, aliases: "-c", type: :string, default: "config.ru",
desc: "Uses a custom rackup configuration.", banner: :file
class_option :daemon, aliases: "-d", type: :boolean, default: false,
@@ -187,7 +188,10 @@ module Rails
end
def host
- ENV.fetch("HOST", options[:binding])
+ unless (default_host = options[:binding])
+ default_host = environment == "development" ? "localhost" : "0.0.0.0"
+ end
+ ENV.fetch("HOST", default_host)
end
def environment
diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb
index 0f236ff463..d21a80982b 100644
--- a/railties/test/commands/server_test.rb
+++ b/railties/test/commands/server_test.rb
@@ -121,6 +121,24 @@ class Rails::ServerTest < ActiveSupport::TestCase
end
end
+ def test_host
+ with_rails_env "development" do
+ options = parse_arguments([])
+ assert_equal "localhost", options[:Host]
+ end
+
+ with_rails_env "production" do
+ options = parse_arguments([])
+ assert_equal "0.0.0.0", options[:Host]
+ end
+
+ with_rails_env "development" do
+ args = ["-b", "127.0.0.1"]
+ options = parse_arguments(args)
+ assert_equal "127.0.0.1", options[:Host]
+ end
+ end
+
def test_records_user_supplied_options
server_options = parse_arguments(["-p", 3001])
assert_equal [:Port], server_options[:user_supplied_options]