提交 24290740 编写于 作者: J Jamis Buck

Underscore dasherized keys in formatted requests


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3878 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 6c95e9b1
*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,7 +71,7 @@ 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 }
......@@ -79,6 +79,18 @@ def self.parse_formatted_request_parameters(mime_type, raw_post_data)
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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册