Permalink
Browse files

Underscore dasherized keys in formatted requests

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3878 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 6c95e9b commit 2429074075b08b219f4dbbe8f6d08598c1d67878 @jamis jamis committed Mar 15, 2006
View
@@ -1,5 +1,7 @@
*SVN*
+* Underscore dasherized keys in formatted requests [Jamis Buck]
+
* Add MimeResponds::Responder#any for managing multiple types with identical responses [Jamis Buck]
* Make the xml_http_request testing method set the HTTP_ACCEPT header [Jamis Buck]
@@ -71,14 +71,26 @@ def self.parse_formatted_request_parameters(mime_type, raw_post_data)
{ node.node_name => node }
end
- params || {}
+ dasherize_keys(params || {})
rescue Object => e
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,
"raw_post_data" => raw_post_data, "format" => mime_type }
end
private
+ def self.dasherize_keys(params)
+ case params.class.to_s
+ when "Hash"
+ params.inject({}) do |h,(k,v)|
+ h[k.tr("-", "_")] = dasherize_keys(v)
+ h
+ end
+ else
+ params
+ end
+ end
+
# Splits the given key into several pieces. Example keys are 'name', 'person[name]',
# 'person[name][first]', and 'people[]'. In each instance, an Array instance is returned.
# 'person[name][first]' produces ['person', 'name', 'first']; 'people[]' produces ['people', '']
@@ -36,7 +36,7 @@ def custom(mime_type, *args, &block)
@responses[mime_type] = block
else
if argument = args.first
- eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding)
+ eval("__mime_responder_arg__ = #{argument.is_a?(String) ? argument.inspect : argument}", @block_binding)
@responses[mime_type] = eval(DEFAULT_BLOCKS[(mime_type.to_sym.to_s + "_arg").to_sym], @block_binding)
else
@responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding)
@@ -19,7 +19,20 @@ class TestController < ActionController::Base
session :off
def assign_parameters
- render :text => (@params.keys - ['controller', 'action']).sort.join(", ")
+ if params[:full]
+ render :text => dump_params_keys
+ else
+ render :text => (params.keys - ['controller', 'action']).sort.join(", ")
+ end
+ end
+
+ def dump_params_keys(hash=params)
+ hash.keys.sort.inject("") do |s, k|
+ value = hash[k]
+ value = Hash === value ? "(#{dump_params_keys(value)})" : ""
+ s << ", " unless s.empty?
+ s << "#{k}#{value}"
+ end
end
def rescue_action(e) raise end
@@ -88,16 +101,28 @@ def test_deprecated_request_methods
assert_equal true, @controller.request.yaml_post?
assert_equal false, @controller.request.xml_post?
end
+
+ def test_dasherized_keys_as_xml
+ ActionController::Base.param_parsers[Mime::XML] = :xml_simple
+ process('POST', 'application/xml', "<first-key>\n<sub-key>...</sub-key>\n</first-key>", true)
+ assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
+ end
+
+ def test_dasherized_keys_as_yaml
+ ActionController::Base.param_parsers[Mime::YAML] = :yaml
+ process('POST', 'application/x-yaml', "---\nfirst-key:\n sub-key: ...\n", true)
+ assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
+ end
private
- def process(verb, content_type = 'application/x-www-form-urlencoded', data = '')
+ def process(verb, content_type = 'application/x-www-form-urlencoded', data = '', full=false)
cgi = MockCGI.new({
'REQUEST_METHOD' => verb,
'CONTENT_TYPE' => content_type,
- 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test",
+ 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test#{"&full=1" if full}",
"REQUEST_URI" => "/",
"HTTP_HOST" => 'testdomain.com',
"CONTENT_LENGTH" => data.size,

0 comments on commit 2429074

Please sign in to comment.