From 52658061d1205753a8afd2801845a910a6c01ffd Mon Sep 17 00:00:00 2001 From: Simon Rozet Date: Sun, 18 Jan 2009 14:23:00 +0100 Subject: [PATCH] Support for nested params in Sinatra::Test Sinatra::Test#param_string was slurped from merb: --- lib/sinatra/test.rb | 25 +++++++++++++++++-------- test/test_test.rb | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 test/test_test.rb diff --git a/lib/sinatra/test.rb b/lib/sinatra/test.rb index f990604c..fbe8253d 100644 --- a/lib/sinatra/test.rb +++ b/lib/sinatra/test.rb @@ -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 diff --git a/test/test_test.rb b/test/test_test.rb new file mode 100644 index 00000000..95e74b42 --- /dev/null +++ b/test/test_test.rb @@ -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