{"id":26,"date":"2007-02-27T03:00:05","date_gmt":"2007-02-27T02:00:05","guid":{"rendered":"http:\/\/schuerig.de\/michael\/blog\/index.php\/2007\/02\/27\/xml-round-tripping\/"},"modified":"2021-11-24T09:20:09","modified_gmt":"2021-11-24T08:20:09","slug":"xml-round-tripping","status":"publish","type":"post","link":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/02\/27\/xml-round-tripping\/","title":{"rendered":"XML round-trip testing for Rails resources"},"content":{"rendered":"<p>I&#8217;ve recently started to add XML support to a Rails application, meaning that the application provides data in XML format, if the request asks for it, and it understands XML data on create or update.<\/p>\n<p>To keep the application as well as myself sane, I&#8217;ve written a test that ensures the round trip of getting XML and updating an object by sending XML works. This is only a very basic test and there surely is more that can and should be tested.<\/p>\n<p><code><\/p>\n<pre>\r\nclass XmlRoundtripTest &lt; ActionController::IntegrationTest\r\n\r\n  RESOURCES = [\r\n    :people, :things\r\n  ]\r\n  fixtures :stuff, *RESOURCES\r\n\r\n  def self.assert_roundtrippability_for(*resources)\r\n    resources.each do |resource|\r\n      define_method(\"test_xml_roundtrip_for_#{resource}\") do\r\n        @user = user\r\n        @user.logs_in\r\n\r\n        xml = @user.gets_xml(resource)\r\n        @user.changes_object_name!(xml)\r\n\r\n        old_version = @user.extracts_lock_version(xml)\r\n        @user.sends_xml(xml, resource)\r\n\r\n        # Make sure that the resource was really updated\r\n        new_version = @user.extracts_lock_version(@user.gets_xml(resource))\r\n        assert_equal(old_version + 1, new_version)\r\n      end\r\n    end\r\n  end\r\n\r\n  assert_roundtrippability_for *RESOURCES\r\n\r\n  def user\r\n    open_session do |user|\r\n      def user.gets_xml(resources, id = 1)\r\n        get \"\/#{resources}\/#{id}.xml\"\r\n        assert_response :success\r\n        @response.body\r\n      end\r\n\r\n      def user.sends_xml(xml, resources, id = 1)\r\n        put \"\/#{resources}\/#{id}.xml\", xml, :content_type =&gt; 'application\/xml'\r\n        assert_response :success\r\n        @response.body\r\n      end\r\n\r\n      def user.changes_object_name!(xml)\r\n        # arbitrarily change an attribute we know is there\r\n        xml.gsub!(%r{&lt;name&gt;(.*?)&lt;\/name&gt;}, '&lt;name&gt;X\\1Y&lt;\/name&gt;')\r\n      end\r\n\r\n      def user.extracts_lock_version(xml)\r\n        xml =~ %r{&lt;lock-version&gt;(\\d+)&lt;\/lock-version&gt;}\r\n        $1.to_i\r\n      end\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently started to add XML support to a Rails application, meaning that the application provides data in XML format, if the request asks for it, and it understands XML data on create or update. To keep the application as &hellip; <a href=\"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/02\/27\/xml-round-tripping\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-rails","category-testing"],"_links":{"self":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":1,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}