1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Support for nested params in Sinatra::Test

Sinatra::Test#param_string was slurped from merb:

<http://github.com/wycats/merb/blob/84f61f39/merb-core/lib/merb-core/dispatch/request_parsers.rb#L156>
This commit is contained in:
Simon Rozet 2009-01-18 14:23:00 +01:00 committed by Ryan Tomayko
parent 9d329651b4
commit 52658061d1
2 changed files with 38 additions and 8 deletions

View file

@ -16,14 +16,14 @@ module Sinatra
when 2 # input, env
input, env = args
if input.kind_of?(Hash) # params, env
[env, build_query(input)]
[env, param_string(input)]
else
[env, input]
end
when 1 # params
if (data = args.first).kind_of?(Hash)
env = (data.delete(:env) || {})
[env, build_query(data)]
[env, param_string(data)]
else
[{}, data]
end
@ -82,6 +82,21 @@ module Sinatra
end
end
def param_string(value, prefix = nil)
case value
when Array
value.map { |v|
param_string(v, "#{prefix}[]")
} * "&"
when Hash
value.map { |k, v|
param_string(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
} * "&"
else
"#{prefix}=#{escape(value)}"
end
end
if defined? Sinatra::Compat
# Deprecated. Use: "get" instead of "get_it".
%w(get head post put delete).each do |verb|
@ -92,12 +107,6 @@ module Sinatra
end
RUBY
end
# Deprecated. Use: build_query instead.
def param_string(hash)
sinatra_warn "The param_string method is deprecated; use build_query instead."
build_query(hash)
end
end
end

21
test/test_test.rb Normal file
View file

@ -0,0 +1,21 @@
require File.dirname(__FILE__) + '/helper'
describe "Sinatra::Test" do
it "support nested parameters" do
mock_app {
get '/' do
params[:post][:title]
end
post '/' do
params[:post][:content]
end
}
get '/', :post => { :title => 'My Post Title' }
assert_equal 'My Post Title', body
post '/', :post => { :content => 'Post Content' }
assert_equal 'Post Content', body
end
end