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

Adding a call to logger from params_parser to give detailed debug information when invalid xml or json is posted

[#2481 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Jay Pignata 2009-08-11 00:56:46 -04:00 committed by Jeremy Kemper
parent 911acc10de
commit 679128da58
3 changed files with 47 additions and 6 deletions

View file

@ -47,6 +47,8 @@ module ActionDispatch
false false
end end
rescue Exception => e # YAML, XML or Ruby code block errors rescue Exception => e # YAML, XML or Ruby code block errors
logger.debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
raise raise
{ "body" => request.raw_post, { "body" => request.raw_post,
"content_type" => request.content_type, "content_type" => request.content_type,
@ -67,5 +69,9 @@ module ActionDispatch
nil nil
end end
def logger
defined?(Rails.logger) ? Rails.logger : Logger.new($stderr)
end
end end
end end

View file

@ -30,16 +30,36 @@ class JsonParamsParsingTest < ActionController::IntegrationTest
) )
end end
private test "logs error if parsing unsuccessful" do
def assert_parses(expected, actual, headers = {}) with_test_routing do
with_routing do |set| begin
set.draw do |map| $stderr = StringIO.new
map.connect ':action', :controller => "json_params_parsing_test/test" json = "[\"person]\": {\"name\": \"David\"}}"
post "/parse", json, {'CONTENT_TYPE' => 'application/json'}
assert_response :error
$stderr.rewind && err = $stderr.read
assert err =~ /Error occurred while parsing request parameters/
ensure
$stderr = STDERR
end
end
end end
private
def assert_parses(expected, actual, headers = {})
with_test_routing do
post "/parse", actual, headers post "/parse", actual, headers
assert_response :ok assert_response :ok
assert_equal(expected, TestController.last_request_parameters) assert_equal(expected, TestController.last_request_parameters)
end end
end end
def with_test_routing
with_routing do |set|
set.draw do |map|
map.connect ':action', :controller => "json_params_parsing_test/test"
end
yield
end
end
end end

View file

@ -38,6 +38,21 @@ class XmlParamsParsingTest < ActionController::IntegrationTest
end end
end end
test "logs error if parsing unsuccessful" do
with_test_routing do
begin
$stderr = StringIO.new
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></pineapple>"
post "/parse", xml, default_headers
assert_response :error
$stderr.rewind && err = $stderr.read
assert err =~ /Error occurred while parsing request parameters/
ensure
$stderr = STDERR
end
end
end
test "parses multiple files" do test "parses multiple files" do
xml = <<-end_body xml = <<-end_body
<person> <person>