Allow extensions to the routing system

This commit is contained in:
Bob Aman 2009-05-01 10:29:30 -04:00 committed by Ryan Tomayko
parent 0a6927cad0
commit 452a03045c
2 changed files with 34 additions and 0 deletions

View File

@ -807,6 +807,8 @@ module Sinatra
end
end
[/^#{pattern}$/, keys]
elsif path.respond_to?(:keys) && path.respond_to?(:match)
[path, path.keys]
elsif path.respond_to? :match
[path, keys]
else

View File

@ -5,6 +5,22 @@ def route_def(pattern)
mock_app { get(pattern) { } }
end
class RegexpLookAlike
class MatchData
def captures
["this", "is", "a", "test"]
end
end
def match(string)
::RegexpLookAlike::MatchData.new if string == "/this/is/a/test/"
end
def keys
["one", "two", "three", "four"]
end
end
class RoutingTest < Test::Unit::TestCase
%w[get put post delete].each do |verb|
it "defines #{verb.upcase} request handlers with #{verb}" do
@ -357,6 +373,22 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'right on', body
end
it 'supports regular expression look-alike routes' do
mock_app {
get(RegexpLookAlike.new) do
assert_equal 'this', params[:one]
assert_equal 'is', params[:two]
assert_equal 'a', params[:three]
assert_equal 'test', params[:four]
'right on'
end
}
get '/this/is/a/test/'
assert ok?
assert_equal 'right on', body
end
it 'raises a TypeError when pattern is not a String or Regexp' do
assert_raise(TypeError) {
mock_app { get(42){} }