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
|
|
|
|
|
2009-08-11 00:56:46 -04:00
|
|
|
test "logs error if parsing unsuccessful" do
|
|
|
|
with_test_routing do
|
|
|
|
begin
|
|
|
|
$stderr = StringIO.new
|
|
|
|
json = "[\"person]\": {\"name\": \"David\"}}"
|
2010-01-19 10:05:34 -05:00
|
|
|
post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => true}
|
2009-08-11 00:56:46 -04:00
|
|
|
assert_response :error
|
|
|
|
$stderr.rewind && err = $stderr.read
|
|
|
|
assert err =~ /Error occurred while parsing request parameters/
|
|
|
|
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
|
2009-11-04 18:25:15 -05:00
|
|
|
match ':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
|