1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/webservice_test.rb
Godfrey Chan ff1192fea4 Eliminate JSON.{parse,load,generate,dump} and def to_json
JSON.{dump,generate} offered by the JSON gem is not compatiable with
Rails at the moment and can cause a lot of subtle bugs when passed
certain data structures. This changed all direct usage of the JSON gem
in internal Rails code to always go through AS::JSON.{decode,encode}.

We also shouldn't be implementing `to_json` most of the time, and
these occurances are replaced with an equivilent `as_json`
implementation to avoid problems down the road.

See [1] for all the juicy details.

[1]: intridea/multi_json#138 (comment)
2013-11-05 22:26:45 -08:00

104 lines
3.1 KiB
Ruby

require 'abstract_unit'
require 'active_support/json/decoding'
class WebServiceTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
def assign_parameters
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
end
def setup
@controller = TestController.new
@integration_session = nil
end
def test_check_parameters
with_test_route_set do
get "/"
assert_equal '', @controller.response.body
end
end
def test_post_json
with_test_route_set do
post "/", '{"entry":{"summary":"content..."}}', 'CONTENT_TYPE' => 'application/json'
assert_equal 'entry', @controller.response.body
assert @controller.params.has_key?(:entry)
assert_equal 'content...', @controller.params["entry"]['summary']
end
end
def test_put_json
with_test_route_set do
put "/", '{"entry":{"summary":"content..."}}', 'CONTENT_TYPE' => 'application/json'
assert_equal 'entry', @controller.response.body
assert @controller.params.has_key?(:entry)
assert_equal 'content...', @controller.params["entry"]['summary']
end
end
def test_register_and_use_json_simple
with_test_route_set do
with_params_parsers Mime::JSON => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
post "/", '{"request":{"summary":"content...","title":"JSON"}}',
'CONTENT_TYPE' => 'application/json'
assert_equal 'summary, title', @controller.response.body
assert @controller.params.has_key?(:summary)
assert @controller.params.has_key?(:title)
assert_equal 'content...', @controller.params["summary"]
assert_equal 'JSON', @controller.params["title"]
end
end
end
def test_use_json_with_empty_request
with_test_route_set do
assert_nothing_raised { post "/", "", 'CONTENT_TYPE' => 'application/json' }
assert_equal '', @controller.response.body
end
end
def test_dasherized_keys_as_json
with_test_route_set do
post "/?full=1", '{"first-key":{"sub-key":"..."}}', 'CONTENT_TYPE' => 'application/json'
assert_equal 'action, controller, first-key(sub-key), full', @controller.response.body
assert_equal "...", @controller.params['first-key']['sub-key']
end
end
private
def with_params_parsers(parsers = {})
old_session = @integration_session
@app = ActionDispatch::ParamsParser.new(app.routes, parsers)
reset!
yield
ensure
@integration_session = old_session
end
def with_test_route_set
with_routing do |set|
set.draw do
match '/', :to => 'web_service_test/test#assign_parameters', :via => :all
end
yield
end
end
end