From e87d9decb82fbbde50926911167fecebd4bcc25e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 30 Sep 2009 15:02:59 -0700 Subject: rails: RAILS_RELATIVE_URL_ROOT may be set in Unicorn config Since Unicorn config files are written in Ruby, setting RAILS_RELATIVE_URL_ROOT should be possible (and even encouraged) in the config file if it is done at all. --- bin/unicorn_rails | 8 ++++---- test/rails/app-2.3.3.1/public/x.txt | 1 + test/rails/test_rails.rb | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/rails/app-2.3.3.1/public/x.txt diff --git a/bin/unicorn_rails b/bin/unicorn_rails index ab0cfa3..ab9f1c0 100755 --- a/bin/unicorn_rails +++ b/bin/unicorn_rails @@ -12,7 +12,6 @@ options = { :listeners => listeners } host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT set_listener = false ENV['RAILS_ENV'] ||= "development" -map_path = ENV['RAILS_RELATIVE_URL_ROOT'] opts = OptionParser.new("", 24, ' ') do |opts| opts.banner = "Usage: #{cmd} " \ @@ -81,8 +80,9 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.on("-P", "--path PATH", "DEPRECATED") do |v| warn %q{Use of --path/-P is strongly discouraged} - warn %q{Use the 'map' directive in the rackup config instead} - ENV['RAILS_RELATIVE_URL_ROOT'] = map_path = v + warn %q{Use the RAILS_RELATIVE_URL_ROOT environment variable} + warn %q{ or the 'map' directive in the rackup config instead} + ENV['RAILS_RELATIVE_URL_ROOT'] = v end # I'm avoiding Unicorn-specific config options on the command-line. @@ -156,8 +156,8 @@ app = lambda do || Object.const_get(File.basename(config, '.rb').capitalize) end - map_path ||= '/' Rack::Builder.new do + map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/' if inner_app.class.to_s == "Unicorn::App::OldRails" if map_path != '/' # patches + tests welcome, but I really cbf to deal with this diff --git a/test/rails/app-2.3.3.1/public/x.txt b/test/rails/app-2.3.3.1/public/x.txt new file mode 100644 index 0000000..e427984 --- /dev/null +++ b/test/rails/app-2.3.3.1/public/x.txt @@ -0,0 +1 @@ +HELLO diff --git a/test/rails/test_rails.rb b/test/rails/test_rails.rb index 8ff97f6..9502dcb 100644 --- a/test/rails/test_rails.rb +++ b/test/rails/test_rails.rb @@ -231,6 +231,31 @@ logger Logger.new('#{COMMON_TMP.path}') assert_equal '404 Not Found', res['Status'] end + def test_alt_url_root_config_env + # cbf to actually work on this since I never use this feature (ewong) + return unless ROR_V[0] >= 2 && ROR_V[1] >= 3 + tmp = Tempfile.new(nil) + tmp.syswrite("ENV['RAILS_RELATIVE_URL_ROOT'] = '/poo'\n") + redirect_test_io do + @pid = fork { exec 'unicorn_rails', "-l#@addr:#@port", "-c", tmp.path } + end + wait_master_ready("test_stderr.#$$.log") + res = Net::HTTP.get_response(URI.parse("http://#@addr:#@port/poo/foo")) + assert_equal "200", res.code + assert_equal '200 OK', res['Status'] + assert_equal "FOO\n", res.body + assert_match %r{^text/html\b}, res['Content-Type'] + assert_equal "4", res['Content-Length'] + + res = Net::HTTP.get_response(URI.parse("http://#@addr:#@port/foo")) + assert_equal "404", res.code + assert_equal '404 Not Found', res['Status'] + + res = Net::HTTP.get_response(URI.parse("http://#@addr:#@port/poo/x.txt")) + assert_equal "200", res.code + assert_equal "HELLO\n", res.body + end + def teardown return if @start_pid != $$ -- cgit v1.2.3-24-ge0c7