<commit_before>require 'abstract_unit'

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

    def rescue_action(e) raise end
  end

  def setup
    @controller = TestController.new
    @integration_session = nil
  end

  def test_check_parameters
    with_test_route_set do
      get "/"
      assert_blank @controller.response.body
    end
  end

  def test_post_xml
    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
  end

  def test_put_xml
    with_test_route_set do
      put "/", '<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
  end

  def test_put_xml_using_a_type_node
    with_test_route_set do
      put "/", '<type attributed="true"><summary>content...</summary></type>',
        {'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 "/", '<type attributed="true"><summary type="boolean">false</summary></type>',
        {'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 "/", '<font attributed="true"><type>arial</type></font>',
        {'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 "/", '<type type="integer">33</type>',
        {'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
      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
    end
  end

  def test_register_and_use_yaml
    with_test_route_set do
      with_params_parsers Mime::YAML => Proc.new { |d| YAML.load(d) } do
        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
  end

  def test_register_and_use_yaml_as_symbol
    with_test_route_set do
      with_params_parsers Mime::YAML => :yaml do
        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
  end

  def test_register_and_use_xml_simple
    with_test_route_set do
      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
    end
  end

  def test_use_xml_ximple_with_empty_request
    with_test_route_set do
      assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
      assert_blank @controller.response.body
    end
  end

  def test_dasherized_keys_as_xml
    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
  end

  def test_typecast_as_xml
    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><commit_msg>try not to use assert_blank when nil will would be considered as failure<commit_after>require 'abstract_unit'

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

    def rescue_action(e) raise 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_xml
    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
  end

  def test_put_xml
    with_test_route_set do
      put "/", '<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
  end

  def test_put_xml_using_a_type_node
    with_test_route_set do
      put "/", '<type attributed="true"><summary>content...</summary></type>',
        {'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 "/", '<type attributed="true"><summary type="boolean">false</summary></type>',
        {'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 "/", '<font attributed="true"><type>arial</type></font>',
        {'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 "/", '<type type="integer">33</type>',
        {'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
      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
    end
  end

  def test_register_and_use_yaml
    with_test_route_set do
      with_params_parsers Mime::YAML => Proc.new { |d| YAML.load(d) } do
        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
  end

  def test_register_and_use_yaml_as_symbol
    with_test_route_set do
      with_params_parsers Mime::YAML => :yaml do
        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
  end

  def test_register_and_use_xml_simple
    with_test_route_set do
      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
    end
  end

  def test_use_xml_ximple_with_empty_request
    with_test_route_set do
      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
      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
  end

  def test_typecast_as_xml
    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>
{'fraction_non_alphanumeric': 0.14671460261607894, 'fraction_numerical': 0.006731431194064102, 'mean_word_length': 3.2683643486777667, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 157, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 48}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3945025', 'n_tokens_mistral': 4176, 'n_tokens_neox': 4061, 'n_words': 743}