Permalink
Browse files

Fix redirect method

	According to RFC 2616 section 14.30, for the “Location” header, “the field value consists of a single absolute URI”
	If a non absolute URI is given, the URI is reconstructed using the request.
	Tests are modified accordingly.
  • Loading branch information...
1 parent 0b032a0 commit 507972ee64772ffc79a6e54cf84c613f4fe49be6 @giga giga committed with Aug 30, 2010
Showing with 13 additions and 5 deletions.
  1. +8 −0 lib/sinatra/base.rb
  2. +2 −2 test/filter_test.rb
  3. +3 −3 test/helpers_test.rb
View
8 lib/sinatra/base.rb
@@ -96,6 +96,14 @@ def block.each ; yield call ; end
# Halt processing and redirect to the URI provided.
def redirect(uri, *args)
+ if not uri =~ /^https?:\/\//
+ # According to RFC 2616 section 14.30, “the field value consists of a single absolute URI”
+ abs_uri = request.scheme + "://"
+ abs_uri << request.host
+ abs_uri << ":#{port}" if request.scheme == "https" and request.port != 443 or request.scheme == "http" and request.port != 80
+ abs_uri << uri
+ uri = abs_uri
+ end
status 302
response['Location'] = uri
halt(*args)
View
4 test/filter_test.rb
@@ -55,7 +55,7 @@ class BeforeFilterTest < Test::Unit::TestCase
get '/foo'
assert redirect?
- assert_equal '/bar', response['Location']
+ assert_equal 'http://example.org/bar', response['Location']
assert_equal '', body
end
@@ -189,7 +189,7 @@ class AfterFilterTest < Test::Unit::TestCase
get '/foo'
assert redirect?
- assert_equal '/bar', response['Location']
+ assert_equal 'http://example.org/bar', response['Location']
assert_equal '', body
end
View
6 test/helpers_test.rb
@@ -58,7 +58,7 @@ def test_default
get '/'
assert_equal 302, status
assert_equal '', body
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
it 'uses the code given when specified' do
@@ -72,7 +72,7 @@ def test_default
get '/'
assert_equal 301, status
assert_equal '', body
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
it 'redirects back to request.referer when passed back' do
@@ -85,7 +85,7 @@ def test_default
request = Rack::MockRequest.new(@app)
response = request.get('/try_redirect', 'HTTP_REFERER' => '/foo')
assert_equal 302, response.status
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
end

0 comments on commit 507972e

Please sign in to comment.