aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-02-11 06:29:04 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-02-11 06:29:04 +0000
commit002c0aeef3302d6cceecfd039a325e0eef59c17a (patch)
tree5481e87590fb537d15c66c76da67d43e800ae135 /activesupport
parent39ae7025e84e35785e11d9fed059e74e70745ee3 (diff)
downloadrails-002c0aeef3302d6cceecfd039a325e0eef59c17a.tar.gz
rails-002c0aeef3302d6cceecfd039a325e0eef59c17a.tar.bz2
rails-002c0aeef3302d6cceecfd039a325e0eef59c17a.zip
Hash#to_query CGI-escapes its keys.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6148 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb2
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb30
3 files changed, 25 insertions, 9 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 114ce90c1e..d3ae372a47 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Hash#to_query CGI-escapes its keys. [Jeremy Kemper]
+
* Deprecation: warn on stderr if RAILS_DEFAULT_LOGGER isn't set yet. [Jeremy
Kemper]
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index a77fb55747..d3ab3b3590 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -9,7 +9,7 @@ class Object
end
def to_query(key) #:nodoc:
- "#{key}=#{CGI.escape(to_param || "")}"
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param || "")}"
end
end
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 2946e849e0..8a120fdfee 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -530,22 +530,36 @@ end
class QueryTest < Test::Unit::TestCase
def test_simple_conversion
- assert_equal 'a=10', {:a => 10}.to_query
+ assert_query_equal 'a=10', :a => 10
+ end
+
+ def test_cgi_escaping
+ assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d'
+ end
+
+ def test_nil_parameter_value
+ empty = Object.new
+ def empty.to_param; nil end
+ assert_query_equal 'a=', 'a' => empty
end
def test_nested_conversion
- expected = %w(person[name]=Nicholas person[login]=seckar)
- actual = {:person => {:name => 'Nicholas', :login => 'seckar'}}.to_query
- assert_equal expected.sort, actual.split('&').sort
+ assert_query_equal 'person%5Bname%5D=Nicholas&person%5Blogin%5D=seckar',
+ :person => {:name => 'Nicholas', :login => 'seckar'}
end
def test_multiple_nested
- assert_equal 'account[person][id]=20&person[id]=10',
- {:person => {:id => 10}, :account => {:person => {:id => 20}}}.to_query
+ assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10',
+ :person => {:id => 10}, :account => {:person => {:id => 20}}
end
def test_array_values
- assert_equal 'person[id][]=10&person[id][]=20',
- {:person => {:id => [10, 20]}}.to_query
+ assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20',
+ :person => {:id => [10, 20]}
end
+
+ private
+ def assert_query_equal(expected, actual, message = nil)
+ assert_equal expected.split('&').sort, actual.to_query.split('&').sort
+ end
end