Unit testing XML REST

How are people testing REST webservices that require you to POST xml data? Are you using the post and get shortcuts in your tests, or net::http?

With the post shortcut is there any way to specify you want to send xml instead of regular old parameters?

I tried this in one of my controller tests for creating a thing
   post :create, {:title => "cool"}, {'Content-Type' => 'application/xml'}
   puts @request.env.to_xml
   puts @response.headers.to_xml

And sadly the request env didn't include Content-Type. It only has:
   <REMOTE-ADDR>0.0.0.0</REMOTE-ADDR>
   <REQUEST-URI>/events?title=cool</REQUEST-URI>
   <SERVER-PORT type="integer">80</SERVER-PORT>
   <REQUEST-METHOD>POST</REQUEST-METHOD>

So I'm not sure what I'm doing wrong with sending headers. And also I'd like to specify some xml to send instead of URL params.

According to the rdocs post takes these parameters:
path, parameters, headers

Thanks!
Andy

you can set these:
@request.env[‘Content-Type’] = ‘application/xml’
@request.env[“HTTP_ACCEPT”] = ‘application/xml’

as for posting XML, I just use the to_xml methods, but it’s not as slick as I’d like it to be either.

anyone else?

ed

you can set these:
@request.env['Content-Type'] = 'application/xml'
@request.env["HTTP_ACCEPT"] = 'application/xml'

:format => :xml. Much easier :slight_smile:

as for posting XML, I just use the to_xml methods, but it's not as slick as
I'd like it to be either.

I use integration tests to post XML. Functional tests bypass the
param parsers IIRC.

Can you elaborate on how you’re using to_xml to post the data? What do you put as the name on that value?

post :create, {“RAW_POST_DATA” => things.to_xml} , :format => :xml

I use integration tests to post XML. Functional tests bypass the
param parsers IIRC.

Would you mind posting or linking to a snippet of an intergration test that posts/puts XML? I’ve been messing with it for awhile and can’t get the rails to parse the XML into a params hash. It works if I pass a hash, but not an XML string.

thanks,
ed

sess.post '/foos.xml', Foo.new(...).to_xml, :content_type => 'application/xml'

it seems like that only works for get and post…or am i missing something? eg:

headers = {:content_type=>“application/xml”, :http_authorization=>“Basic cXVlbnRpbjp0ZXM0\n”}
get ‘/campaigns/1.xml’, {}, headers

=> 200

post ‘/campaigns.xml’, {:name => “test 123”}.to_xml(:root => “campaign”), headers
=> 201

put ‘/campaigns/1.xml’, {:name => “test 123”}.to_xml(:root => “campaign”), headers

NoMethodError: undefined method recycle!' for #<ActionController::CgiRequest:0xb6f928a4> from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/cgi_process.rb:139:in method_missing’

    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/test_process.rb:370:in `process'
    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/test_process.rb:353:in `put'

    from (irb):8:in `test_test'
    from test/integration/api_test.rb:40:in `test_test'
    from /usr/lib/ruby/1.8/test/unit/testcase.rb:70:in `run'
    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/integration.rb:453:in `run'

    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
    from /usr/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:44:in `run_suite'
    from /usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:65:in `start_mediator'

    from /usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:39:in `start'
    from /usr/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:27:in `run'
    from /usr/lib/ruby/1.8/test/unit/autorunner.rb:200:in `run'

    from /usr/lib/ruby/1.8/test/unit/autorunner.rb:13:in `run'
    from /usr/lib/ruby/1.8/test/unit.rb:285

delete ‘/campaigns/1.xml’, {}, headers
NoMethodError: undefined method `recycle!’ for #ActionController::CgiRequest:0xb6ec6f9c

    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/cgi_process.rb:139:in `method_missing'
    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/test_process.rb:370:in `process'

    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/test_process.rb:353:in `delete'
    from (irb):10:in `test_test'
    from test/integration/api_test.rb:40:in `test_test'

    from /usr/lib/ruby/1.8/test/unit/testcase.rb:70:in `run'
    from /var/www/rails/config/../vendor/rails/actionpack/lib/action_controller/integration.rb:453:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'

    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'
    from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
    from /usr/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:44:in `run_suite'
    from /usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:65:in `start_mediator'
    from /usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:39:in `start'

    from /usr/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:27:in `run'
    from /usr/lib/ruby/1.8/test/unit/autorunner.rb:200:in `run'
    from /usr/lib/ruby/1.8/test/unit/autorunner.rb:13:in `run'

    from /usr/lib/ruby/1.8/test/unit.rb:285
    from test/integration/api_test.rb:106

ed