Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty SCRIPT_NAME with partial match route bug #43

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: ruby
rvm:
- 1.9.3-p551
- 2.3.0
before_install: gem install bundler
2 changes: 1 addition & 1 deletion http_router.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'url_mount', '~> 0.2.1'
s.add_development_dependency 'minitest', '~> 2.0.0'
s.add_development_dependency 'code_stats'
s.add_development_dependency 'rake', '~> 0.8.7'
s.add_development_dependency 'rake', '~> 10.5.0'
s.add_development_dependency 'rbench'
s.add_development_dependency 'json'
s.add_development_dependency 'phocus'
Expand Down
10 changes: 8 additions & 2 deletions lib/http_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ def clone(klass = self.class)
end

def rewrite_partial_path_info(env, request)
env['PATH_INFO'] = "/#{request.path.join('/')}"
env['SCRIPT_NAME'] += request.rack_request.path_info[0, request.rack_request.path_info.size - env['PATH_INFO'].size]
path_info_before = request.rack_request.path_info.dup
if request.path.empty?
env['PATH_INFO'] = "/"
env['SCRIPT_NAME'] += path_info_before
else
env['PATH_INFO'] = "/#{URI.encode(request.path.join('/'))}"
env['SCRIPT_NAME'] += path_info_before[0, path_info_before.size - env['PATH_INFO'].size]
end
end

def rewrite_path_info(env, request)
Expand Down
54 changes: 54 additions & 0 deletions test/rack/test_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,58 @@ def test_custom_status
def test_raise_error_on_invalid_status
assert_raises(ArgumentError) { router.get("/index.html").redirect("/", 200) }
end

def test_path_info_from_partial_match
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq/queues"))
assert_equal('/queues', request_env['PATH_INFO'])
end

def test_script_name_from_partial_match
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq/queues"))
assert_equal('/sidekiq', request_env['SCRIPT_NAME'])
end

def test_path_info_from_partial_match_of_single
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq"))
assert_equal('/', request_env['PATH_INFO'])
end

def test_script_name_from_partial_match_of_single
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq"))
assert_equal('/sidekiq', request_env['SCRIPT_NAME'])
end

def test_path_info_with_encoded_request_path
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq/queues/some%20path"))
assert_equal('/queues/some%20path', request_env['PATH_INFO'])
end

def test_script_name_with_encoded_request_path
request_env = nil
router do
add("/sidekiq*").to { |env| request_env = env; [200, {}, []] }
end
router.call(Rack::MockRequest.env_for("/sidekiq/queues/some%20path"))
assert_equal('/sidekiq', request_env['SCRIPT_NAME'])
end
end