views:

425

answers:

4

Ive seen mentions of curl here and there on rails blogs and ive scanned some posts here on stackoverflow but im still a bit in the dark as to its use especially when it comes to rails development.

Is it useful for testing? Im currently learning the ins and outs of testing and one of the things i need to do is test a before filter that only allows an action to be called if the user came from a certain external site. Is this an occasion where curl would be used?

+2  A: 

No, I don't think you'd use cURL for TDD. Using cURL would imply communicating with a remote service, which would be an integration test.

Are you talking about this? http://www.viget.com/extend/curl-and-your-rails-2-app/

In that post, cURL isn't being used in the rails app at all. It's being used on the terminal to test/demo the app's REST service, not in a TDD fashion.

cURL is good for this purpose because its many options allow you to simulate different headers from user agents, such as language acceptance, characterset acceptance, cache usage, etc...

Ben James
+2  A: 

curl is a command line program which can retrieve urls, it's quite flexible, for example it can use limited regexes to download a range of files.

You might want to use it for testing, by seeing what happens to your application under certain circumstances, if you want to test what happens if your user comes from a certain site then use the -e option on curl.

The man page is online Here

gorilla
ahh that sounds good - so if i call curl in my test using the flag and specify an external site then when i call request.env["HTTP_REFERER"]it should hold that dummy external site url...right?is there a way to do this with the testing framework taht comes with rails without resorting to curl?
adam
Curl is quite useful for testing the JSON response type of your controller actions (which might be your API). I recommend the "cheat" gem (gem install cheat), it is a concise curl reference. From cheat, something I use all the time: `curl -H "Accept: application/json" -i -X GET http://localhost:3000/projects/3`
Andy Atkinson
+1  A: 
John Topley
Hurl is actually built with Sinatra. http://github.com/defunkt/hurl
Andy Atkinson
So it is, thanks. I got confused because I know that it was entered in the 2009 Rails Rumble!
John Topley
A: 

Ruby (Rail?) hackers might also be interested in one of the many Ruby bindings for libcurl, the library that powers the curl tool. Like them mentioned here: http://curl.haxx.se/libcurl/ruby/

Daniel Stenberg