Twitter Updates

Testing RESTful Rails controllers by POSTing XML

I ran into a problem this morning trying to test a Rails controller with an XML document that needed to made in an HTTP POST request. It turns out that it’s actually pretty easy using Rails IntegrationTest and a couple small tweaks:

class MyXmlPostClass < ActionController::IntegrationTest
  test "should successfully post XML" do
    # assume you have an XML object named xml_request
    @headers ||= {}
    @headers['HTTP_ACCEPT'] = @headers['CONTENT_TYPE'] = 'application/xml'
    post '/controller_path', xml_request.to_s, @headers
    # use standard assert on response object
  end
end

That’s it!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Slashdot
  • Twitter
  • Reddit