Skip to content

Commit

Permalink
Add ORIGINAL_FULLPATH to env
Browse files Browse the repository at this point in the history
This behaves similarly to REQUEST_URI, but
we need to implement it on our own because
REQUEST_URI is not reliable.

Note that since PATH_INFO does not contain
information about trailing question mark,
this is not 100% accurate, for example
`/foo?` will result in `/foo` in ORIGINAL_FULLPATH
  • Loading branch information
drogus committed Dec 20, 2011
1 parent 53c1ae9 commit 482ec2a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
17 changes: 17 additions & 0 deletions railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ def helpers_paths #:nodoc:
config.helpers_paths
end

def call(env)
env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
super(env)
end

protected

alias :build_middleware_stack :app
Expand Down Expand Up @@ -291,5 +296,17 @@ def initialize_console #:nodoc:
require "rails/console/app"
require "rails/console/helpers"
end

def build_original_fullpath(env)
path_info = env["PATH_INFO"]
query_string = env["QUERY_STRING"]
script_name = env["SCRIPT_NAME"]

if query_string.present?
"#{script_name}#{path_info}?#{query_string}"
else
"#{script_name}#{path_info}"
end
end
end
end
27 changes: 27 additions & 0 deletions railties/test/application/build_original_fullpath_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "abstract_unit"

module ApplicationTests
class BuildOriginalPathTest < Test::Unit::TestCase
def test_include_original_PATH_info_in_ORIGINAL_FULLPATH
env = { 'PATH_INFO' => '/foo/' }
assert_equal "/foo/", Rails.application.send(:build_original_fullpath, env)
end

def test_include_SCRIPT_NAME
env = {
'SCRIPT_NAME' => '/foo',
'PATH_INFO' => '/bar'
}

assert_equal "/foo/bar", Rails.application.send(:build_original_fullpath, env)
end

def test_include_QUERY_STRING
env = {
'PATH_INFO' => '/foo',
'QUERY_STRING' => 'bar',
}
assert_equal "/foo?bar", Rails.application.send(:build_original_fullpath, env)
end
end
end
11 changes: 10 additions & 1 deletion railties/test/application/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'isolation/abstract_unit'
require 'stringio'
require 'rack/test'

module ApplicationTests
class MiddlewareTest < Test::Unit::TestCase
Expand Down Expand Up @@ -75,7 +76,7 @@ def app
add_to_config "config.force_ssl = true"
add_to_config "config.ssl_options = { :host => 'example.com' }"
boot!

assert_equal AppTemplate::Application.middleware.first.args, [{:host => 'example.com'}]
end

Expand Down Expand Up @@ -193,6 +194,14 @@ def index
assert_equal nil, last_response.headers["Etag"]
end

test "ORIGINAL_FULLPATH is passed to env" do
boot!
env = ::Rack::MockRequest.env_for("/foo/?something")
Rails.application.call(env)

assert_equal "/foo/?something", env["ORIGINAL_FULLPATH"]
end

private

def boot!
Expand Down

0 comments on commit 482ec2a

Please sign in to comment.