2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2013-11-05 23:05:58 -05:00
|
|
|
require 'active_support/json/decoding'
|
2006-03-05 13:59:58 -05:00
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class WebServiceTest < ActionDispatch::IntegrationTest
|
2006-03-05 13:59:58 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def assign_parameters
|
2006-03-15 16:46:41 -05:00
|
|
|
if params[:full]
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: dump_params_keys
|
2006-03-15 16:46:41 -05:00
|
|
|
else
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: (params.keys - ['controller', 'action']).sort.join(", ")
|
2006-03-15 16:46:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
def dump_params_keys(hash = params)
|
2006-03-15 16:46:41 -05:00
|
|
|
hash.keys.sort.inject("") do |s, k|
|
|
|
|
value = hash[k]
|
2015-07-13 16:43:21 -04:00
|
|
|
|
|
|
|
if value.is_a?(Hash) || value.is_a?(ActionController::Parameters)
|
|
|
|
value = "(#{dump_params_keys(value)})"
|
|
|
|
else
|
|
|
|
value = ""
|
|
|
|
end
|
|
|
|
|
2006-03-15 16:46:41 -05:00
|
|
|
s << ", " unless s.empty?
|
|
|
|
s << "#{k}#{value}"
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
|
2006-03-05 13:59:58 -05:00
|
|
|
def setup
|
|
|
|
@controller = TestController.new
|
2010-09-28 16:32:18 -04:00
|
|
|
@integration_session = nil
|
2007-05-18 02:24:50 -04:00
|
|
|
end
|
|
|
|
|
2006-03-05 13:59:58 -05:00
|
|
|
def test_check_parameters
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
get "/"
|
2011-06-10 14:19:24 -04:00
|
|
|
assert_equal '', @controller.response.body
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
|
2013-02-19 15:41:03 -05:00
|
|
|
def test_post_json
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2015-01-04 04:35:06 -05:00
|
|
|
post "/",
|
|
|
|
params: '{"entry":{"summary":"content..."}}',
|
2015-01-29 09:19:41 -05:00
|
|
|
headers: { 'CONTENT_TYPE' => 'application/json' }
|
2008-12-04 02:24:24 -05:00
|
|
|
|
|
|
|
assert_equal 'entry', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:entry)
|
|
|
|
assert_equal 'content...', @controller.params["entry"]['summary']
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2013-02-19 15:41:03 -05:00
|
|
|
def test_put_json
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2015-01-04 04:35:06 -05:00
|
|
|
put "/",
|
|
|
|
params: '{"entry":{"summary":"content..."}}',
|
|
|
|
headers: { 'CONTENT_TYPE' => 'application/json' }
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
assert_equal 'entry', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:entry)
|
|
|
|
assert_equal 'content...', @controller.params["entry"]['summary']
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
|
2013-02-19 15:41:03 -05:00
|
|
|
def test_register_and_use_json_simple
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2015-10-05 01:14:04 -04:00
|
|
|
with_params_parsers Mime[:json] => Proc.new { |data| ActiveSupport::JSON.decode(data)['request'].with_indifferent_access } do
|
2015-01-04 04:35:06 -05:00
|
|
|
post "/",
|
|
|
|
params: '{"request":{"summary":"content...","title":"JSON"}}',
|
2015-01-29 09:19:41 -05:00
|
|
|
headers: { 'CONTENT_TYPE' => 'application/json' }
|
2009-08-21 17:49:33 -04:00
|
|
|
|
|
|
|
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"]
|
2013-02-19 15:41:03 -05:00
|
|
|
assert_equal 'JSON', @controller.params["title"]
|
2009-08-21 17:49:33 -04:00
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-05 14:16:55 -05:00
|
|
|
end
|
2006-03-18 13:56:19 -05:00
|
|
|
|
2013-02-19 15:41:03 -05:00
|
|
|
def test_use_json_with_empty_request
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2015-01-29 09:19:41 -05:00
|
|
|
assert_nothing_raised { post "/", headers: { 'CONTENT_TYPE' => 'application/json' } }
|
2011-06-10 14:19:24 -04:00
|
|
|
assert_equal '', @controller.response.body
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-18 13:56:19 -05:00
|
|
|
end
|
2006-03-15 16:46:41 -05:00
|
|
|
|
2013-02-19 15:41:03 -05:00
|
|
|
def test_dasherized_keys_as_json
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2015-01-04 04:35:06 -05:00
|
|
|
post "/?full=1",
|
|
|
|
params: '{"first-key":{"sub-key":"..."}}',
|
2015-01-29 09:19:41 -05:00
|
|
|
headers: { 'CONTENT_TYPE' => 'application/json' }
|
2013-02-19 15:41:03 -05:00
|
|
|
assert_equal 'action, controller, first-key(sub-key), full', @controller.response.body
|
|
|
|
assert_equal "...", @controller.params['first-key']['sub-key']
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-18 01:21:04 -05:00
|
|
|
end
|
|
|
|
|
2014-11-23 16:04:31 -05:00
|
|
|
def test_parsing_json_doesnot_rescue_exception
|
2015-09-18 16:03:07 -04:00
|
|
|
req = Class.new(ActionDispatch::Request) do
|
|
|
|
def params_parsers
|
2015-10-05 01:14:04 -04:00
|
|
|
{ Mime[:json] => Proc.new { |data| raise Interrupt } }
|
2014-11-23 16:04:31 -05:00
|
|
|
end
|
2015-09-18 16:03:07 -04:00
|
|
|
|
|
|
|
def content_length; get_header('rack.input').length; end
|
|
|
|
end.new({ 'rack.input' => StringIO.new('{"title":"JSON"}}'), 'CONTENT_TYPE' => 'application/json' })
|
|
|
|
|
|
|
|
assert_raises(Interrupt) do
|
|
|
|
req.request_parameters
|
2014-11-23 16:04:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
private
|
2009-08-21 17:49:33 -04:00
|
|
|
def with_params_parsers(parsers = {})
|
|
|
|
old_session = @integration_session
|
2015-09-18 16:03:07 -04:00
|
|
|
original_parsers = ActionDispatch::Request.parameter_parsers
|
|
|
|
ActionDispatch::Request.parameter_parsers = original_parsers.merge parsers
|
2009-09-26 21:51:05 -04:00
|
|
|
reset!
|
2009-08-21 17:49:33 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2015-09-18 16:03:07 -04:00
|
|
|
ActionDispatch::Request.parameter_parsers = original_parsers
|
2009-08-21 17:49:33 -04:00
|
|
|
@integration_session = old_session
|
|
|
|
end
|
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
match '/', :to => 'web_service_test/test#assign_parameters', :via => :all
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|