let #uri and #redirect accept other schemas

This commit is contained in:
Konstantin Haase 2011-03-21 10:32:24 +01:00
parent 670a100973
commit 5177d86e76
3 changed files with 44 additions and 1 deletions

View File

@ -6,6 +6,9 @@
* Important: 1.8.6 support has been dropped. (Konstantin Haase)
* URIs passed to the `url` helper or `redirect` may now use any schema to be
identified as absolute URIs, not only `http` or `https`. (Konstantin Haase)
= 1.2.1 / 2011-03-17
* Use a generated session secret when using `enable :sessions`. (Konstantin

View File

@ -106,7 +106,7 @@ module Sinatra
# Generates the absolute URI for a given path in the app.
# Takes Rack routers and reverse proxies into account.
def uri(addr = nil, absolute = true, add_script_name = true)
return addr if addr =~ /^https?:\/\//
return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
uri = [host = ""]
if absolute
host << 'http'

View File

@ -123,6 +123,34 @@ class HelpersTest < Test::Unit::TestCase
response = request.get('/', 'HTTP_X_FORWARDED_HOST' => 'example.com', 'SERVER_PORT' => '8080')
assert_equal 'http://example.com/foo', response['Location']
end
it 'accepts absolute URIs' do
mock_app do
get '/' do
redirect 'http://google.com'
fail 'redirect should halt'
end
end
get '/'
assert_equal 302, status
assert_equal '', body
assert_equal 'http://google.com', response['Location']
end
it 'accepts absolute URIs with a different schema' do
mock_app do
get '/' do
redirect 'mailto:jsmith@example.com'
fail 'redirect should halt'
end
end
get '/'
assert_equal 302, status
assert_equal '', body
assert_equal 'mailto:jsmith@example.com', response['Location']
end
end
describe 'error' do
@ -808,6 +836,18 @@ class HelpersTest < Test::Unit::TestCase
assert_equal 'http://example.org/foo/bar', body
end
it 'handles absolute URIs' do
mock_app { get('/') { uri 'http://google.com' }}
get '/'
assert_equal 'http://google.com', body
end
it 'handles different protocols' do
mock_app { get('/') { uri 'mailto:jsmith@example.com' }}
get '/'
assert_equal 'mailto:jsmith@example.com', body
end
it 'is aliased to #url' do
mock_app { get('/') { url }}
get '/'