1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Revert "Quick fix for not escaping []s (not ideal)"

According to http://www.ietf.org/rfc/rfc2616.txt and
http://tools.ietf.org/rfc/rfc3986.txt [ and ] are reserved characters
and should be escaped using "%" HEX HEX

This reverts commit 856d2fd874 and
1ee9b40b18a0bed5bb10a0785f7e2730bac983f6..
This commit is contained in:
Santiago Pastorino 2010-11-16 19:57:09 -02:00
parent 07a74f196d
commit 52b71c01fd
4 changed files with 10 additions and 12 deletions

View file

@ -1444,7 +1444,7 @@ class RouteSetTest < ActiveSupport::TestCase
end end
def test_expand_array_build_query_string def test_expand_array_build_query_string
assert_uri_equal '/foo?x[]=1&x[]=2', default_route_set.generate({:controller => 'foo', :x => [1, 2]}) assert_uri_equal '/foo?x%5B%5D=1&x%5B%5D=2', default_route_set.generate({:controller => 'foo', :x => [1, 2]})
end end
def test_escape_spaces_build_query_string_selected_keys def test_escape_spaces_build_query_string_selected_keys
@ -1744,9 +1744,9 @@ class RackMountIntegrationTests < ActiveSupport::TestCase
assert_equal '/posts', @routes.generate({:controller => 'posts'}, {:controller => 'posts', :action => 'index'}) assert_equal '/posts', @routes.generate({:controller => 'posts'}, {:controller => 'posts', :action => 'index'})
assert_equal '/posts/create', @routes.generate({:action => 'create'}, {:controller => 'posts'}) assert_equal '/posts/create', @routes.generate({:action => 'create'}, {:controller => 'posts'})
assert_equal '/posts?foo=bar', @routes.generate(:controller => 'posts', :foo => 'bar') assert_equal '/posts?foo=bar', @routes.generate(:controller => 'posts', :foo => 'bar')
assert_equal '/posts?foo[]=bar&foo[]=baz', @routes.generate(:controller => 'posts', :foo => ['bar', 'baz']) assert_equal '/posts?foo%5B%5D=bar&foo%5B%5D=baz', @routes.generate(:controller => 'posts', :foo => ['bar', 'baz'])
assert_equal '/posts?page=2', @routes.generate(:controller => 'posts', :page => 2) assert_equal '/posts?page=2', @routes.generate(:controller => 'posts', :page => 2)
assert_equal '/posts?q[foo][a]=b', @routes.generate(:controller => 'posts', :q => { :foo => { :a => 'b'}}) assert_equal '/posts?q%5Bfoo%5D%5Ba%5D=b', @routes.generate(:controller => 'posts', :q => { :foo => { :a => 'b'}})
assert_equal '/news.rss', @routes.generate(:controller => 'news', :action => 'index', :format => 'rss') assert_equal '/news.rss', @routes.generate(:controller => 'news', :action => 'index', :format => 'rss')

View file

@ -1,5 +1,3 @@
class Object class Object
# Alias of <tt>to_s</tt>. # Alias of <tt>to_s</tt>.
def to_param def to_param
@ -41,7 +39,7 @@ class Hash
# ==== Examples # ==== Examples
# { :name => 'David', :nationality => 'Danish' }.to_param # => "name=David&nationality=Danish" # { :name => 'David', :nationality => 'Danish' }.to_param # => "name=David&nationality=Danish"
# #
# { :name => 'David', :nationality => 'Danish' }.to_param('user') # => "user[name]=David&user[nationality]=Danish" # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
def to_param(namespace = nil) def to_param(namespace = nil)
collect do |key, value| collect do |key, value|
value.to_query(namespace ? "#{namespace}[#{key}]" : key) value.to_query(namespace ? "#{namespace}[#{key}]" : key)

View file

@ -7,7 +7,7 @@ class Object
# Note: This method is defined as a default implementation for all Objects for Hash#to_query to work. # Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
def to_query(key) def to_query(key)
require 'cgi' unless defined?(CGI) && defined?(CGI::escape) require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
"#{CGI.escape(key.to_s).gsub(/%(5B|5D)/n) { [$1].pack('H*') }}=#{CGI.escape(to_param.to_s)}" "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
end end
end end
@ -15,7 +15,7 @@ class Array
# Converts an array into a string suitable for use as a URL query string, # Converts an array into a string suitable for use as a URL query string,
# using the given +key+ as the param name. # using the given +key+ as the param name.
# #
# ['Rails', 'coding'].to_query('hobbies') # => "hobbies[]=Rails&hobbies[]=coding" # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
def to_query(key) def to_query(key)
prefix = "#{key}[]" prefix = "#{key}[]"
collect { |value| value.to_query(prefix) }.join '&' collect { |value| value.to_query(prefix) }.join '&'

View file

@ -18,22 +18,22 @@ class ToQueryTest < Test::Unit::TestCase
end end
def test_nested_conversion def test_nested_conversion
assert_query_equal 'person[login]=seckar&person[name]=Nicholas', assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas',
:person => ActiveSupport::OrderedHash[:login, 'seckar', :name, 'Nicholas'] :person => ActiveSupport::OrderedHash[:login, 'seckar', :name, 'Nicholas']
end end
def test_multiple_nested def test_multiple_nested
assert_query_equal 'account[person][id]=20&person[id]=10', assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10',
ActiveSupport::OrderedHash[:account, {:person => {:id => 20}}, :person, {:id => 10}] ActiveSupport::OrderedHash[:account, {:person => {:id => 20}}, :person, {:id => 10}]
end end
def test_array_values def test_array_values
assert_query_equal 'person[id][]=10&person[id][]=20', assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20',
:person => {:id => [10, 20]} :person => {:id => [10, 20]}
end end
def test_array_values_are_not_sorted def test_array_values_are_not_sorted
assert_query_equal 'person[id][]=20&person[id][]=10', assert_query_equal 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10',
:person => {:id => [20, 10]} :person => {:id => [20, 10]}
end end