Rails REST testing using XML

One of the things that has been bugging me about my REST interfaces is that, although they are thoroughly tested in functional tests with all the GETs and POSTs and PUTs (and occasionally DELETEs), it just isn’t quite the same as literally POSTing the XML.

So, this morning I took the time to figure out a way to do this. It turns out that with an integration test, it’s quite easy. It’s also probably the Right Placeā„¢ to do this.

Witness:

class RestXmlTest < ActionController::IntegrationTest
  fixtures :model_fixture

  # Test creating a new resource by actually POSTing the XML.
  def test_create_resource
    post "/path_to_resource.xml",
      "<resource><attribute_1>attribute value</attribute_1>...</resource>",
      {:content_type => "application/xml"}

    assert_response 201
  end
end

Leave a Reply