let #uri and #redirect accept other schemas
This commit is contained in:
parent
670a100973
commit
5177d86e76
3
CHANGES
3
CHANGES
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
* Important: 1.8.6 support has been dropped. (Konstantin Haase)
|
* 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
|
= 1.2.1 / 2011-03-17
|
||||||
|
|
||||||
* Use a generated session secret when using `enable :sessions`. (Konstantin
|
* Use a generated session secret when using `enable :sessions`. (Konstantin
|
||||||
|
|
|
@ -106,7 +106,7 @@ module Sinatra
|
||||||
# Generates the absolute URI for a given path in the app.
|
# Generates the absolute URI for a given path in the app.
|
||||||
# Takes Rack routers and reverse proxies into account.
|
# Takes Rack routers and reverse proxies into account.
|
||||||
def uri(addr = nil, absolute = true, add_script_name = true)
|
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 = ""]
|
uri = [host = ""]
|
||||||
if absolute
|
if absolute
|
||||||
host << 'http'
|
host << 'http'
|
||||||
|
|
|
@ -123,6 +123,34 @@ class HelpersTest < Test::Unit::TestCase
|
||||||
response = request.get('/', 'HTTP_X_FORWARDED_HOST' => 'example.com', 'SERVER_PORT' => '8080')
|
response = request.get('/', 'HTTP_X_FORWARDED_HOST' => 'example.com', 'SERVER_PORT' => '8080')
|
||||||
assert_equal 'http://example.com/foo', response['Location']
|
assert_equal 'http://example.com/foo', response['Location']
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe 'error' do
|
describe 'error' do
|
||||||
|
@ -808,6 +836,18 @@ class HelpersTest < Test::Unit::TestCase
|
||||||
assert_equal 'http://example.org/foo/bar', body
|
assert_equal 'http://example.org/foo/bar', body
|
||||||
end
|
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
|
it 'is aliased to #url' do
|
||||||
mock_app { get('/') { url }}
|
mock_app { get('/') { url }}
|
||||||
get '/'
|
get '/'
|
||||||
|
|
Loading…
Reference in New Issue