mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Merge pull request #1333 from dometto/fix1332
Add pattern matches to values for Mustermann::Concat. Fixes #1332
This commit is contained in:
commit
a96f144c3f
2 changed files with 25 additions and 1 deletions
|
@ -1024,7 +1024,8 @@ module Sinatra
|
|||
params.delete("ignore") # TODO: better params handling, maybe turn it into "smart" object or detect changes
|
||||
original, @params = @params, @params.merge(params) if params.any?
|
||||
|
||||
if pattern.is_a? Mustermann::Regular
|
||||
regexp_exists = pattern.is_a?(Mustermann::Regular) || (pattern.respond_to?(:patterns) && pattern.patterns.any? {|subpattern| subpattern.is_a?(Mustermann::Regular)} )
|
||||
if regexp_exists
|
||||
captures = pattern.match(route).captures
|
||||
values += captures
|
||||
@params[:captures] = captures
|
||||
|
|
|
@ -629,6 +629,29 @@ class RoutingTest < Minitest::Test
|
|||
assert_equal 'right on', body
|
||||
end
|
||||
|
||||
it 'makes regular expression captures available in params[:captures] for concatenated routes' do
|
||||
with_regexp = Mustermann.new('/prefix') + Mustermann.new("/fo(.*)/ba(.*)", type: :regexp)
|
||||
without_regexp = Mustermann.new('/prefix', type: :identity) + Mustermann.new('/baz')
|
||||
mock_app {
|
||||
get(with_regexp) do
|
||||
assert_equal ['orooomma', 'f'], params[:captures]
|
||||
'right on'
|
||||
end
|
||||
get(without_regexp) do
|
||||
assert !params.keys.include?(:captures)
|
||||
'no captures here'
|
||||
end
|
||||
}
|
||||
|
||||
get '/prefix/foorooomma/baf'
|
||||
assert ok?
|
||||
assert_equal 'right on', body
|
||||
|
||||
get '/prefix/baz'
|
||||
assert ok?
|
||||
assert_equal 'no captures here', body
|
||||
end
|
||||
|
||||
it 'supports regular expression look-alike routes' do
|
||||
mock_app {
|
||||
get(PatternLookAlike.new) do
|
||||
|
|
Loading…
Reference in a new issue