Skip to content

Commit

Permalink
let #uri and #redirect accept other schemas
Browse files Browse the repository at this point in the history
Conflicts:

	CHANGES
  • Loading branch information
rkh committed Mar 21, 2011
1 parent 47dacdd commit c1c02ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
= 1.2.2 / Not Yet Released

* 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
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def redirect(uri, *args)
# 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'
Expand Down
40 changes: 40 additions & 0 deletions test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ def test_default
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
Expand Down Expand Up @@ -795,6 +823,18 @@ def send_file_app(opts={})
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 '/'
Expand Down

0 comments on commit c1c02ee

Please sign in to comment.