X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Factionpack%2Ftest%2Fcontroller%2Fwebservice_test.rb;fp=vendor%2Frails%2Factionpack%2Ftest%2Fcontroller%2Fwebservice_test.rb;h=e89d6bb960db2973132315fad27c7c92603db0a6;hb=437aa336c44c74a30aeea16a06743c32747ed661;hp=0000000000000000000000000000000000000000;hpb=97a0772b06264134cfe38e7494f9427efe0840a0;p=feedcatcher.git diff --git a/vendor/rails/actionpack/test/controller/webservice_test.rb b/vendor/rails/actionpack/test/controller/webservice_test.rb new file mode 100644 index 0000000..e89d6bb --- /dev/null +++ b/vendor/rails/actionpack/test/controller/webservice_test.rb @@ -0,0 +1,260 @@ +require 'abstract_unit' + +class WebServiceTest < ActionController::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 + + def rescue_action(e) raise end + end + + def setup + @controller = TestController.new + @default_param_parsers = ActionController::Base.param_parsers.dup + end + + def teardown + ActionController::Base.param_parsers = @default_param_parsers + end + + def test_check_parameters + with_test_route_set do + get "/" + assert_equal '', @controller.response.body + end + end + + def test_post_xml + with_test_route_set do + post "/", 'content...', + {'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 + end + + def test_put_xml + with_test_route_set do + put "/", 'content...', + {'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 + end + + def test_put_xml_using_a_type_node + with_test_route_set do + put "/", 'content...', + {'CONTENT_TYPE' => 'application/xml'} + + 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 + end + + def test_put_xml_using_a_type_node_and_attribute + with_test_route_set do + put "/", 'false', + {'CONTENT_TYPE' => 'application/xml'} + + 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 + end + + def test_post_xml_using_a_type_node + with_test_route_set do + post "/", 'arial', + {'CONTENT_TYPE' => 'application/xml'} + + 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 + end + + def test_post_xml_using_a_root_node_named_type + with_test_route_set do + post "/", '33', + {'CONTENT_TYPE' => 'application/xml'} + + assert @controller.params.has_key?(:type) + assert_equal 33, @controller.params['type'] + end + end + + def test_post_xml_using_an_attributted_node_named_type + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } + post "/", 'Arial,123', + {'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 + end + + def test_register_and_use_yaml + with_test_route_set do + ActionController::Base.param_parsers[Mime::YAML] = Proc.new { |d| YAML.load(d) } + post "/", {"entry" => "loaded from yaml"}.to_yaml, + {'CONTENT_TYPE' => 'application/x-yaml'} + + assert_equal 'entry', @controller.response.body + assert @controller.params.has_key?(:entry) + assert_equal 'loaded from yaml', @controller.params["entry"] + end + end + + def test_register_and_use_yaml_as_symbol + with_test_route_set do + ActionController::Base.param_parsers[Mime::YAML] = :yaml + post "/", {"entry" => "loaded from yaml"}.to_yaml, + {'CONTENT_TYPE' => 'application/x-yaml'} + + assert_equal 'entry', @controller.response.body + assert @controller.params.has_key?(:entry) + assert_equal 'loaded from yaml', @controller.params["entry"] + end + end + + def test_register_and_use_xml_simple + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } + post "/", 'content...SimpleXml', + {'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 + end + + def test_use_xml_ximple_with_empty_request + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = :xml_simple + assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} } + assert_equal "", @controller.response.body + end + end + + def test_dasherized_keys_as_xml + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = :xml_simple + post "/?full=1", "\n...\n", + {'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 + end + + def test_typecast_as_xml + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = :xml_simple + xml = <<-XML + + 15 + false + true + 2005-03-17 + 2005-03-17T21:41:07Z + unparsed + 1 + hello + 1974-07-25 + + 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 + end + + def test_entities_unescaped_as_xml_simple + with_test_route_set do + ActionController::Base.param_parsers[Mime::XML] = :xml_simple + xml = <<-XML + <foo "bar's" & friends> + XML + post "/", xml, {'CONTENT_TYPE' => 'application/xml'} + assert_equal %(), @controller.params[:data] + end + end + + def test_typecast_as_yaml + with_test_route_set do + ActionController::Base.param_parsers[Mime::YAML] = :yaml + yaml = <<-YAML + --- + 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 + end + + private + def with_test_route_set + with_routing do |set| + set.draw do |map| + map.with_options :controller => "web_service_test/test" do |c| + c.connect "/", :action => "assign_parameters" + end + end + yield + end + end +end