Unit testing XML REST


#1

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:
0.0.0.0
/events?title=cool
80
POST

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


#2

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


#3

On 2/12/07, Ed Hickey removed_email_address@domain.invalid wrote:

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.


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com


#4

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


#5

On 2/16/07, Ed Hickey removed_email_address@domain.invalid wrote:

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.

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


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com


#6

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


#7

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:inmethod_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:input’
from (irb):8:in test_test' from test/integration/api_test.rb:40:intest_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:inrun’
from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in run' from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:inrun’
from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in run' from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:inrun’
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:instart_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:inrun’
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:200:in run' from /usr/lib/ruby/1.8/test/unit/autorunner.rb:13:inrun’
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:inmethod_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:indelete’
from (irb):10:in test_test' from test/integration/api_test.rb:40:intest_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:inrun’
from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in run' from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:inrun’
from /usr/lib/ruby/1.8/test/unit/testsuite.rb:32:in run' from /usr/lib/ruby/1.8/test/unit/testsuite.rb:31:inrun’
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:instart_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:inrun’
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:200:in run' from /usr/lib/ruby/1.8/test/unit/autorunner.rb:13:inrun’
from /usr/lib/ruby/1.8/test/unit.rb:285
from test/integration/api_test.rb:106

ed