Forum: Ruby on Rails Unit testing XML REST

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3726bda7f0d852f7e8296fb0d69aa9e0?d=identicon&s=25 Andy Triboletti (Guest)
on 2007-02-13 02:39
(Received via mailing list)
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
15c80c9bf8be5ba6e5eeac9cb0304464?d=identicon&s=25 Ed Hickey (Guest)
on 2007-02-13 03:06
(Received via mailing list)
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-02-13 03:09
(Received via mailing list)
On 2/12/07, Ed Hickey <bassnode@gmail.com> wrote:
> you can set these:
> @request.env['Content-Type'] = 'application/xml'
>  @request.env["HTTP_ACCEPT"] = 'application/xml'

:format => :xml.  Much easier :)

> 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 Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
3726bda7f0d852f7e8296fb0d69aa9e0?d=identicon&s=25 Andy Triboletti (Guest)
on 2007-02-13 03:25
(Received via mailing list)
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
15c80c9bf8be5ba6e5eeac9cb0304464?d=identicon&s=25 Ed Hickey (Guest)
on 2007-02-16 20:39
(Received via mailing list)
>
> 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-02-16 21:07
(Received via mailing list)
On 2/16/07, Ed Hickey <bassnode@gmail.com> 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 Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
15c80c9bf8be5ba6e5eeac9cb0304464?d=identicon&s=25 Ed Hickey (Guest)
on 2007-02-16 21:48
(Received via mailing list)
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
This topic is locked and can not be replied to.