2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
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]
|
|
|
|
render :text => dump_params_keys
|
|
|
|
else
|
|
|
|
render :text => (params.keys - ['controller', 'action']).sort.join(", ")
|
|
|
|
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]
|
|
|
|
value = Hash === value ? "(#{dump_params_keys(value)})" : ""
|
|
|
|
s << ", " unless s.empty?
|
|
|
|
s << "#{k}#{value}"
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e) raise 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 "/"
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_blank @controller.response.body
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_xml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
post "/", '<entry attributed="true"><summary>content...</summary></entry>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
|
|
|
|
assert_equal 'entry', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:entry)
|
|
|
|
assert_equal 'content...', @controller.params["entry"]['summary']
|
|
|
|
assert_equal 'true', @controller.params["entry"]['attributed']
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2006-03-05 13:59:58 -05:00
|
|
|
def test_put_xml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
put "/", '<entry attributed="true"><summary>content...</summary></entry>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
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']
|
|
|
|
assert_equal 'true', @controller.params["entry"]['attributed']
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
|
|
|
|
2008-02-27 18:11:08 -05:00
|
|
|
def test_put_xml_using_a_type_node
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
put "/", '<type attributed="true"><summary>content...</summary></type>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
assert_equal 'type', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:type)
|
|
|
|
assert_equal 'content...', @controller.params["type"]['summary']
|
|
|
|
assert_equal 'true', @controller.params["type"]['attributed']
|
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_put_xml_using_a_type_node_and_attribute
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
put "/", '<type attributed="true"><summary type="boolean">false</summary></type>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
assert_equal 'type', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:type)
|
|
|
|
assert_equal false, @controller.params["type"]['summary']
|
|
|
|
assert_equal 'true', @controller.params["type"]['attributed']
|
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_xml_using_a_type_node
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
post "/", '<font attributed="true"><type>arial</type></font>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
assert_equal 'font', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:font)
|
|
|
|
assert_equal 'arial', @controller.params['font']['type']
|
|
|
|
assert_equal 'true', @controller.params["font"]['attributed']
|
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_xml_using_a_root_node_named_type
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
post "/", '<type type="integer">33</type>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
2008-02-27 18:11:08 -05:00
|
|
|
|
2008-12-04 02:24:24 -05:00
|
|
|
assert @controller.params.has_key?(:type)
|
|
|
|
assert_equal 33, @controller.params['type']
|
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_xml_using_an_attributted_node_named_type
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2009-08-21 17:49:33 -04:00
|
|
|
with_params_parsers Mime::XML => Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } do
|
|
|
|
post "/", '<request><type type="string">Arial,12</type><z>3</z></request>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
|
|
|
|
assert_equal 'type, z', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:type)
|
|
|
|
assert_equal 'Arial,12', @controller.params['type'], @controller.params.inspect
|
|
|
|
assert_equal '3', @controller.params['z'], @controller.params.inspect
|
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2008-02-27 18:11:08 -05:00
|
|
|
end
|
|
|
|
|
2006-03-05 13:59:58 -05:00
|
|
|
def test_register_and_use_yaml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2009-08-21 17:49:33 -04:00
|
|
|
with_params_parsers Mime::YAML => Proc.new { |d| YAML.load(d) } do
|
|
|
|
post "/", {"entry" => "loaded from yaml"}.to_yaml,
|
|
|
|
{'CONTENT_TYPE' => 'application/x-yaml'}
|
2008-12-04 02:24:24 -05:00
|
|
|
|
2009-08-21 17:49:33 -04:00
|
|
|
assert_equal 'entry', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:entry)
|
|
|
|
assert_equal 'loaded from yaml', @controller.params["entry"]
|
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
|
2006-03-05 14:16:55 -05:00
|
|
|
def test_register_and_use_yaml_as_symbol
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2009-08-21 17:49:33 -04:00
|
|
|
with_params_parsers Mime::YAML => :yaml do
|
|
|
|
post "/", {"entry" => "loaded from yaml"}.to_yaml,
|
|
|
|
{'CONTENT_TYPE' => 'application/x-yaml'}
|
2008-12-04 02:24:24 -05:00
|
|
|
|
2009-08-21 17:49:33 -04:00
|
|
|
assert_equal 'entry', @controller.response.body
|
|
|
|
assert @controller.params.has_key?(:entry)
|
|
|
|
assert_equal 'loaded from yaml', @controller.params["entry"]
|
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
2006-03-05 14:16:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_register_and_use_xml_simple
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2009-08-21 17:49:33 -04:00
|
|
|
with_params_parsers Mime::XML => Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } do
|
|
|
|
post "/", '<request><summary>content...</summary><title>SimpleXml</title></request>',
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
|
|
|
|
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 'SimpleXml', @controller.params["title"]
|
|
|
|
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
|
|
|
|
|
|
|
def test_use_xml_ximple_with_empty_request
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_blank @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
|
|
|
|
|
|
|
def test_dasherized_keys_as_xml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
post "/?full=1", "<first-key>\n<sub-key>...</sub-key>\n</first-key>",
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
|
|
|
|
assert_equal "...", @controller.params[:first_key][:sub_key]
|
|
|
|
end
|
2006-03-18 00:07:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_typecast_as_xml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
xml = <<-XML
|
|
|
|
<data>
|
|
|
|
<a type="integer">15</a>
|
|
|
|
<b type="boolean">false</b>
|
|
|
|
<c type="boolean">true</c>
|
|
|
|
<d type="date">2005-03-17</d>
|
|
|
|
<e type="datetime">2005-03-17T21:41:07Z</e>
|
|
|
|
<f>unparsed</f>
|
|
|
|
<g type="integer">1</g>
|
|
|
|
<g>hello</g>
|
|
|
|
<g type="date">1974-07-25</g>
|
|
|
|
</data>
|
|
|
|
XML
|
|
|
|
post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
|
|
|
|
params = @controller.params
|
|
|
|
assert_equal 15, params[:data][:a]
|
|
|
|
assert_equal false, params[:data][:b]
|
|
|
|
assert_equal true, params[:data][:c]
|
|
|
|
assert_equal Date.new(2005,3,17), params[:data][:d]
|
|
|
|
assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
|
|
|
|
assert_equal "unparsed", params[:data][:f]
|
|
|
|
assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
|
|
|
|
end
|
2006-03-15 16:46:41 -05:00
|
|
|
end
|
|
|
|
|
2006-03-18 01:21:04 -05:00
|
|
|
def test_entities_unescaped_as_xml_simple
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
|
|
|
xml = <<-XML
|
|
|
|
<data><foo "bar's" & friends></data>
|
|
|
|
XML
|
|
|
|
post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
assert_equal %(<foo "bar's" & friends>), @controller.params[:data]
|
|
|
|
end
|
2006-03-18 01:21:04 -05:00
|
|
|
end
|
|
|
|
|
2006-03-18 00:07:27 -05:00
|
|
|
def test_typecast_as_yaml
|
2008-12-04 02:24:24 -05:00
|
|
|
with_test_route_set do
|
2009-08-21 17:49:33 -04:00
|
|
|
with_params_parsers Mime::YAML => :yaml do
|
2011-01-21 18:43:34 -05:00
|
|
|
yaml = (<<-YAML).strip
|
2009-08-21 17:49:33 -04:00
|
|
|
---
|
|
|
|
data:
|
|
|
|
a: 15
|
|
|
|
b: false
|
|
|
|
c: true
|
|
|
|
d: 2005-03-17
|
|
|
|
e: 2005-03-17T21:41:07Z
|
|
|
|
f: unparsed
|
|
|
|
g:
|
|
|
|
- 1
|
|
|
|
- hello
|
|
|
|
- 1974-07-25
|
|
|
|
YAML
|
|
|
|
post "/", yaml, {'CONTENT_TYPE' => 'application/x-yaml'}
|
|
|
|
params = @controller.params
|
|
|
|
assert_equal 15, params[:data][:a]
|
|
|
|
assert_equal false, params[:data][:b]
|
|
|
|
assert_equal true, params[:data][:c]
|
|
|
|
assert_equal Date.new(2005,3,17), params[:data][:d]
|
|
|
|
assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
|
|
|
|
assert_equal "unparsed", params[:data][:f]
|
|
|
|
assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
|
|
|
|
end
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-08-21 17:49:33 -04:00
|
|
|
def with_params_parsers(parsers = {})
|
|
|
|
old_session = @integration_session
|
2010-03-30 15:05:42 -04:00
|
|
|
@app = ActionDispatch::ParamsParser.new(app.routes, parsers)
|
2009-09-26 21:51:05 -04:00
|
|
|
reset!
|
2009-08-21 17:49:33 -04:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@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
|
2009-12-08 17:52:26 -05:00
|
|
|
match '/', :to => 'web_service_test/test#assign_parameters'
|
2008-12-04 02:24:24 -05:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2006-03-05 13:59:58 -05:00
|
|
|
end
|