Politics, Programming and Possibilities
4 Aug
I’ve begun experimenting with the new Edge Rails code that includes built-in support for REST. (The code that was once in the simply_restful plugin is now a part of Edge Rails.) With the capability to generate multiple types of output per action, it has become more and more useful to test actions on the command-line using cURL. Here are some useful cURL parameters I’ve used:
curl -X DELETE http://localhost:3000/books/1
curl -d "book[title]=Test" -d "book[copyright]=1998" http://localhost:3000/books
curl -H "Accept: text/xml" http://localhost:3000/books/sections/1
Putting it all together, here’s a cURL command that updates the title of a Book object using the PUT action and expects an xml response:
curl -H "Accept: text/xml" -X PUT -d "book[title]=Testing Again" http://localhost:3000/books/1
5 Responses for "How to Use cURL to Test RESTful Rails"
What about rake test_functional. What can you not do through your tests, that cUrl can do?
[...] need a client tool. For those who want to use command line only, cURL is highly recommended (see Duane Johnson’s tutorial for how to use it in testing.) However, I want all processes are visual, so I use Rest-Client [...]
Be sure to send also XML Content-Type, as suggested here: http://stackoverflow.com/questions/269487/how-to-deal-with-protectfromforgery-to-make-rails-applications-communicate
Your curl command should therefore look something like this:
curl -H “Accept: text/xml” -H “Content-Type: text/xml” -d “Fix InvalidAuthenticityToken” http://localhost:3000/tasks
@karmi: Thanks! I hadn’t tried these curl commands since the new fandangled authenticity features were enabled in Rails
[...] for Testing Web Applications CURL and Ruby on Rails Related PostsTesting is a confusing enough area as it isTesting is a confusing enough area as it is [...]
Leave a reply