Allow extensions to the routing system
This commit is contained in:
parent
0a6927cad0
commit
452a03045c
|
@ -807,6 +807,8 @@ module Sinatra
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
[/^#{pattern}$/, keys]
|
[/^#{pattern}$/, keys]
|
||||||
|
elsif path.respond_to?(:keys) && path.respond_to?(:match)
|
||||||
|
[path, path.keys]
|
||||||
elsif path.respond_to? :match
|
elsif path.respond_to? :match
|
||||||
[path, keys]
|
[path, keys]
|
||||||
else
|
else
|
||||||
|
|
|
@ -5,6 +5,22 @@ def route_def(pattern)
|
||||||
mock_app { get(pattern) { } }
|
mock_app { get(pattern) { } }
|
||||||
end
|
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
|
class RoutingTest < Test::Unit::TestCase
|
||||||
%w[get put post delete].each do |verb|
|
%w[get put post delete].each do |verb|
|
||||||
it "defines #{verb.upcase} request handlers with #{verb}" do
|
it "defines #{verb.upcase} request handlers with #{verb}" do
|
||||||
|
@ -357,6 +373,22 @@ class RoutingTest < Test::Unit::TestCase
|
||||||
assert_equal 'right on', body
|
assert_equal 'right on', body
|
||||||
end
|
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
|
it 'raises a TypeError when pattern is not a String or Regexp' do
|
||||||
assert_raise(TypeError) {
|
assert_raise(TypeError) {
|
||||||
mock_app { get(42){} }
|
mock_app { get(42){} }
|
||||||
|
|
Loading…
Reference in New Issue