2009-01-09 16:43:32 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class JsonParamsParsingTest < ActionDispatch::IntegrationTest
|
2009-01-09 16:43:32 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_request_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_request_parameters = request.request_parameters
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
TestController.last_request_parameters = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses json params for application json" do
|
|
|
|
assert_parses(
|
|
|
|
{"person" => {"name" => "David"}},
|
|
|
|
"{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses json params for application jsonrequest" do
|
|
|
|
assert_parses(
|
|
|
|
{"person" => {"name" => "David"}},
|
|
|
|
"{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/jsonrequest' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-01-04 15:02:22 -05:00
|
|
|
test "nils are stripped from collections" do
|
|
|
|
assert_parses(
|
|
|
|
{"person" => nil},
|
|
|
|
"{\"person\":[null]}", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
assert_parses(
|
|
|
|
{"person" => ['foo']},
|
|
|
|
"{\"person\":[\"foo\",null]}", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
assert_parses(
|
|
|
|
{"person" => nil},
|
|
|
|
"{\"person\":[null, null]}", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2009-08-11 00:56:46 -04:00
|
|
|
test "logs error if parsing unsuccessful" do
|
|
|
|
with_test_routing do
|
2011-11-24 14:37:48 -05:00
|
|
|
output = StringIO.new
|
|
|
|
json = "[\"person]\": {\"name\": \"David\"}}"
|
2012-01-04 13:52:24 -05:00
|
|
|
post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => true, 'action_dispatch.logger' => ActiveSupport::Logger.new(output)}
|
2013-05-01 09:48:01 -04:00
|
|
|
assert_response :bad_request
|
2011-11-24 14:37:48 -05:00
|
|
|
output.rewind && err = output.read
|
|
|
|
assert err =~ /Error occurred while parsing request parameters/
|
2009-08-11 00:56:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-23 09:49:43 -05:00
|
|
|
test "occurring a parse error if parsing unsuccessful" do
|
|
|
|
with_test_routing do
|
|
|
|
begin
|
|
|
|
$stderr = StringIO.new # suppress the log
|
|
|
|
json = "[\"person]\": {\"name\": \"David\"}}"
|
2012-08-24 15:55:41 -04:00
|
|
|
exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} }
|
2013-05-11 16:59:37 -04:00
|
|
|
assert_equal JSON::ParserError, exception.original_exception.class
|
2012-08-27 17:46:53 -04:00
|
|
|
assert_equal exception.original_exception.message, exception.message
|
2011-11-23 09:49:43 -05:00
|
|
|
ensure
|
|
|
|
$stderr = STDERR
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-09 16:43:32 -05:00
|
|
|
private
|
|
|
|
def assert_parses(expected, actual, headers = {})
|
2009-08-11 00:56:46 -04:00
|
|
|
with_test_routing do
|
|
|
|
post "/parse", actual, headers
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal(expected, TestController.last_request_parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_test_routing
|
2009-01-09 16:43:32 -05:00
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
post ':action', :to => ::JsonParamsParsingTest::TestController
|
2009-01-09 16:43:32 -05:00
|
|
|
end
|
2009-08-11 00:56:46 -04:00
|
|
|
yield
|
2009-01-09 16:43:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
class RootLessJSONParamsParsingTest < ActionDispatch::IntegrationTest
|
|
|
|
class UsersController < ActionController::Base
|
|
|
|
wrap_parameters :format => :json
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_request_parameters, :last_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_request_parameters = request.request_parameters
|
|
|
|
self.class.last_parameters = params
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
UsersController.last_request_parameters = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses json params for application json" do
|
|
|
|
assert_parses(
|
|
|
|
{"user" => {"username" => "sikachu"}, "username" => "sikachu"},
|
|
|
|
"{\"username\": \"sikachu\"}", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses json params for application jsonrequest" do
|
|
|
|
assert_parses(
|
|
|
|
{"user" => {"username" => "sikachu"}, "username" => "sikachu"},
|
|
|
|
"{\"username\": \"sikachu\"}", { 'CONTENT_TYPE' => 'application/jsonrequest' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-01-09 15:26:12 -05:00
|
|
|
test "parses json with non-object JSON content" do
|
|
|
|
assert_parses(
|
|
|
|
{"user" => {"_json" => "string content" }, "_json" => "string content" },
|
|
|
|
"\"string content\"", { 'CONTENT_TYPE' => 'application/json' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
private
|
|
|
|
def assert_parses(expected, actual, headers = {})
|
|
|
|
with_test_routing(UsersController) do
|
|
|
|
post "/parse", actual, headers
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal(expected, UsersController.last_request_parameters)
|
|
|
|
assert_equal(expected.merge({"action" => "parse"}), UsersController.last_parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_test_routing(controller)
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
post ':action', :to => controller
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|