Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

let #uri and #redirect accept other schemas

Conflicts:

	CHANGES
  • Loading branch information...
commit c1c02ee9c6e0eda546f8e375f83f835ddfb86977 1 parent 47dacdd
@rkh rkh authored
Showing with 46 additions and 1 deletion.
  1. +5 −0 CHANGES
  2. +1 −1  lib/sinatra/base.rb
  3. +40 −0 test/helpers_test.rb
View
5 CHANGES
@@ -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
View
2  lib/sinatra/base.rb
@@ -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'
View
40 test/helpers_test.rb
@@ -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
@@ -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 '/'
Please sign in to comment.
Something went wrong with that request. Please try again.