aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/commands/server_test.rb
blob: 25b89ecbd8082f6babf7a3ffffc687ea6802ad04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# frozen_string_literal: true

require "isolation/abstract_unit"
require "env_helpers"
require "rails/command"
require "rails/commands/server/server_command"

class Rails::Command::ServerCommandTest < ActiveSupport::TestCase
  include EnvHelpers

  def test_environment_with_server_option
    args = ["-u", "thin", "-e", "production"]
    options = parse_arguments(args)
    assert_equal "production", options[:environment]
    assert_equal "thin", options[:server]
  end

  def test_environment_without_server_option
    args = ["-e", "production"]
    options = parse_arguments(args)
    assert_equal "production", options[:environment]
    assert_nil options[:server]
  end

  def test_explicit_using_option
    args = ["-u", "thin"]
    options = parse_arguments(args)
    assert_equal "thin", options[:server]
  end

  def test_using_server_mistype
    assert_match(/Could not find server "tin". Maybe you meant "thin"?/, run_command("--using", "tin"))
  end

  def test_using_server_mistype_without_suggestion
    output = run_command("--using", "t")
    assert_match(/Could not find server "t"/, output)
    assert_no_match(/Maybe you meant/, output)
  end

  def test_using_positional_argument_deprecation
    assert_match(/DEPRECATION WARNING/, run_command("tin"))
  end

  def test_using_known_server_that_isnt_in_the_gemfile
    assert_match(/Could not load server "unicorn". Maybe you need to the add it to the Gemfile/, run_command("-u", "unicorn"))
  end

  def test_daemon_with_option
    args = ["-d"]
    options = parse_arguments(args)
    assert_equal true, options[:daemonize]
  end

  def test_daemon_without_option
    args = []
    options = parse_arguments(args)
    assert_equal false, options[:daemonize]
  end

  def test_server_option_without_environment
    args = ["-u", "thin"]
    with_rack_env nil do
      with_rails_env nil do
        options = parse_arguments(args)
        assert_equal "development",  options[:environment]
        assert_equal "thin", options[:server]
      end
    end
  end

  def test_environment_with_rails_env
    with_rack_env nil do
      with_rails_env "production" do
        options = parse_arguments
        assert_equal "production", options[:environment]
      end
    end
  end

  def test_environment_with_rack_env
    with_rails_env nil do
      with_rack_env "production" do
        options = parse_arguments
        assert_equal "production", options[:environment]
      end
    end
  end

  def test_environment_with_port
    switch_env "PORT", "1234" do
      options = parse_arguments
      assert_equal 1234, options[:Port]
    end
  end

  def test_environment_with_host
    switch_env "HOST", "1.2.3.4" do
      assert_deprecated do
        options = parse_arguments
        assert_equal "1.2.3.4", options[:Host]
      end
    end
  end

  def test_environment_with_binding
    switch_env "BINDING", "1.2.3.4" do
      options = parse_arguments
      assert_equal "1.2.3.4", options[:Host]
    end
  end

  def test_caching_without_option
    args = []
    options = parse_arguments(args)
    assert_nil options[:caching]
  end

  def test_caching_with_option
    args = ["--dev-caching"]
    options = parse_arguments(args)
    assert_equal true, options[:caching]

    args = ["--no-dev-caching"]
    options = parse_arguments(args)
    assert_equal false, options[:caching]
  end

  def test_early_hints_with_option
    args = ["--early-hints"]
    options = parse_arguments(args)
    assert_equal true, options[:early_hints]
  end

  def test_early_hints_is_nil_by_default
    args = []
    options = parse_arguments(args)
    assert_nil options[:early_hints]
  end

  def test_log_stdout
    with_rack_env nil do
      with_rails_env nil do
        args    = []
        options = parse_arguments(args)
        assert_equal true, options[:log_stdout]

        args    = ["-e", "development"]
        options = parse_arguments(args)
        assert_equal true, options[:log_stdout]

        args    = ["-e", "development", "-d"]
        options = parse_arguments(args)
        assert_equal false, options[:log_stdout]

        args    = ["-e", "production"]
        options = parse_arguments(args)
        assert_equal false, options[:log_stdout]

        args    = ["-e", "development", "--no-log-to-stdout"]
        options = parse_arguments(args)
        assert_equal false, options[:log_stdout]

        args    = ["-e", "production", "--log-to-stdout"]
        options = parse_arguments(args)
        assert_equal true, options[:log_stdout]

        with_rack_env "development" do
          args    = []
          options = parse_arguments(args)
          assert_equal true, options[:log_stdout]
        end

        with_rack_env "production" do
          args    = []
          options = parse_arguments(args)
          assert_equal false, options[:log_stdout]
        end

        with_rails_env "development" do
          args    = []
          options = parse_arguments(args)
          assert_equal true, options[:log_stdout]
        end

        with_rails_env "production" do
          args    = []
          options = parse_arguments(args)
          assert_equal false, options[:log_stdout]
        end
      end
    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_argument_precedence_over_environment_variable
    switch_env "PORT", "1234" do
      args = ["-p", "5678"]
      options = parse_arguments(args)
      assert_equal 5678, options[:Port]
    end

    switch_env "PORT", "1234" do
      args = ["-p", "3000"]
      options = parse_arguments(args)
      assert_equal 3000, options[:Port]
    end

    switch_env "BINDING", "1.2.3.4" 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]

    server_options = parse_arguments(["--port", "3001"])
    assert_equal [:Port], server_options[:user_supplied_options]

    server_options = parse_arguments(["-p3001", "-C", "--binding", "127.0.0.1"])
    assert_equal [:Port, :Host, :caching], server_options[:user_supplied_options]

    server_options = parse_arguments(["--port=3001"])
    assert_equal [:Port], server_options[:user_supplied_options]

    switch_env "BINDING", "1.2.3.4" do
      server_options = parse_arguments
      assert_equal [:Host], server_options[:user_supplied_options]
    end
  end

  def test_default_options
    server = Rails::Server.new
    old_default_options = server.default_options

    Dir.chdir("..") do
      assert_equal old_default_options, server.default_options
    end
  end

  def test_restart_command_contains_customized_options
    original_args = ARGV.dup
    args = %w(-p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C)
    ARGV.replace args

    expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C --restart"

    assert_equal expected, parse_arguments(args)[:restart_cmd]
  ensure
    ARGV.replace original_args
  end

  def test_served_url
    args = %w(-u webrick -b 127.0.0.1 -p 4567)
    server = Rails::Server.new(parse_arguments(args))
    assert_equal "http://127.0.0.1:4567", server.served_url
  end

  private
    def run_command(*args)
      build_app
      rails "server", *args
    ensure
      teardown_app
    end

    def parse_arguments(args = [])
      Rails::Command::ServerCommand.new([], args).server_options
    end
end