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
7 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 [...]
Hi, thanks for sharing this tipe. I was wondering if I have some simple authentication system in place, how does one use curl to post and put data and, even before talking about posting and putting data, how does one “login” with curl and have the server remember or have curl remember that it is logged in?
-nik
If you have found the answer to your question I would be very grateful if you could post your findings.
cheers,
Karl
Leave a reply