1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

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.
This commit is contained in:
Jean-Philippe Garcia Ballester 2010-08-30 11:00:30 +02:00 committed by Konstantin Haase
parent 0b032a0c3e
commit 507972ee64
3 changed files with 13 additions and 5 deletions

View file

@ -96,6 +96,14 @@ module Sinatra
# 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 file

@ -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 file

@ -58,7 +58,7 @@ class HelpersTest < Test::Unit::TestCase
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 @@ class HelpersTest < Test::Unit::TestCase
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 @@ class HelpersTest < Test::Unit::TestCase
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