Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing RESTful API with Cucumber in a front end less application

Hi I do not have any front end in my app. I am willing to release just a RESTful API which can be used by different clients. Any pointers how should I proceed towards testing it with cucumber? Every action in the controller generates XML feed only. Any pointers or suggestions?

like image 427
Waseem Avatar asked Jul 25 '09 16:07

Waseem


2 Answers

The visit function of webrat accepts a http_method as a second parameter. You can also test your api like in the following cucumber rule:

When /^I restfully delete (?:|the )user "([^\"]*)"$/ do |login|
  visit(path_to("user \"#{login}\" page"), :delete)
end
like image 128
schastar Avatar answered Sep 20 '22 05:09

schastar


I think Webrat is more than what you need. For XML feed testing, you don't need a browser simulator like Webrat which would load pages and analyse all the markup (links, forms etc.) when you really don't have any HTML pages.

You rather need something like Curl (http://curl.haxx.se) or Curb (on rubyforge, which are ruby bindings for Curl), or Patron (on rubyforge).

These libraries can make a request header as per your liking (e.g. setting Content-Type, choosing among GET PUT POST DELETE HEAD etc.) and obtain the response, and probably follow 302 redirections when needed.

The response returned, can be then turned into XML object, and XML parsers available for Ruby can be used to test the output. Also, you can write XMLMapping classes (on rubyforge) to convert XML output into Ruby objects and test their attributes etc. This is much cleaner, IMHO.

like image 42
Kazim Zaidi Avatar answered Sep 22 '22 05:09

Kazim Zaidi