POST/GET using TinyGet and Ruby
// July 22nd, 2008 // Programming
At Huddle we’ve recently been adding to our JSON RESTful API and have been having problems testing it quickly and efficiently.
For testing GET/POST requests, and their responses, we did originally use Fiddler2, but it doesn’t support SSL, and it’s awkward to use. We also tried Modify Headers, a Firefox extension, but it was problematic and I don’t like using a browser for such testing.
The solution we settled on was to use TinyGet (part of the IIS6 Resource Kit) for GET’s and a Ruby script for POST’s, and also POST’s, PUT’s and DELETE’s.
TinyGet
A small and rather excellent application that allows one line GET commands, including assertions for HTTP return code, and response text. Here’s an example:
tinyget -t -s:4 -a:basic -u:user -p:password -status:403 -srv:api.huddle.net -uri:/v1/json/workspace -testContainString:”Success\”:true”
Especially notice the “testContainString” and “status” parameters, that allow us to make an assertion as to what we expect in the response.
We have a number of these commands to test each type of scenario. They all exist inside the same .bat file, so when testing they can all be run with a single click. I may also include the .bat file (and any others we write) as part of the Cruise Control build as TinyGet will return an error code if any of them fail. Here are some further examples:
tinyget -s:4 -t -a:basic -u:user -p:password -status:200 -srv:api.huddle.dev -uri:”/v1/json/tasks”
tinyget -s:4 -t -a:basic -u:user -p:password -status:200 -srv:api.huddle.dev -uri:”/v1/json/tasks?status=upcoming”
tinyget -s:4 -t -a:basic -u:user -p:password -status:200 -srv:api.huddle.dev -uri:”/v1/json/tasks/”
tinyget -s:4 -t -a:basic -u:user -p:password -status:200 -srv:api.huddle.dev -uri:”/v1/json/tasks/?status=upcoming”
The above are examples of 404 tests I use. Whenever changes are made to the ISAPI Rewrite rules, we need a way of checking all the url’s still work. With a trailing slash, without, with a querystring etc…
TinyGet has a range of other useful arguments you can append. Rather than me post them here, it’s best to use “tinyget -help” in the command line to bring up all the possible options. For example -s:4 means secure connection, 4 being the type of secure connection (TSL I think). -a:basic is the type of authentication, ie Basic Authentication.
Ruby:
Having looked at C# to create POST requests, it required a whole class, and was overly complex for what I wanted to do. Enter Ruby and Python. Both are able to perform an HTTP POST needin only a line or two of code.
In the end I chose Ruby. There’s a Window’s installer that sets up all the necessary file associations. It’s then a simple case of writing a .rb script and executing it via command line. Here’s an example:
require ‘net/http’
res = Net::HTTP.post_form(URI.parse(‘https://user:password@api.huddle.net/v1/json/workspaces’), {‘Title’=>’My Title’, ‘Text’=>’Body Text’})
print res.body
Ruby, Not So Good:
We unfortunately hit a stumbling block with Ruby. The Windows installer for v1-186 has a bug in net\protocol.rb whereby the request always timesout rather than returning a response, or error code etc…
There are more details on this thread, and a possible fix, which didn’t work for me:
http://www.ruby-forum.com/topic/105212
So as an alternative we’re looking at either quickly writing our own C# “tinypost” application or using something like curl, either via Linux or Cygwin.
UPDATE 20/01/2009 – I’ve just noticed there is a “verb” argument for TinyGet (ie -verb:DELETE). Looks like you can do POST, DELETE etc… using TinyGet. Don’t know how I missed that first time round. Anyway I’ll see if it works and if so write up a follow up article regarding it (as I’m also making a custom NAnt task for it).
Stress Testing
As mentioned in a post, it is possible to stress test with Tinyget. It’s very simple, you need to add the following to your command. Here’s an example using 5 threads and looping a command 10 times.
-threads:5 -loops:10
When stress testing you may also want to add -b to your command to catch exceptions and abort gracefully.
Someone asked about the -script parameter. I’ve not used this though, and from briefly looking at it can’t really guide you on what to do.
Tinyget Posts
Also TinyGet can actually do posts. I don’t know why I missed this first time round but there is a -verb parameter. You can use it for GET, POST, DELETE, and put. For example:
-verb:POST
I’ve not used this yet, but intend to as we work on our RESTful API, as we’ll need to be able to test POST’s, PUT’s and DELETE’s.
TinyGet For NAnt
I know you can call it using an “exec” task, but I’m currently wrapping up TinyGet into a nice NAnt custom task. This will make it easier to call, and hopefully give more control. That way I can include it as part of one of our nightly build projects that run a load of integration and stress tests. I’ll update this post when it’s ready.
Is it possible to use tinyget to write autmomated scripts to stress test a web server. if so, any help on how to script it would be really helpful
I’ve added a few bits regarding stress testing to the article above.
How to use -script parameter in tinyget. i want to pass some parameters randomly, what should i do?
I’ve updated the post with new stuff I’ve learnt since writing the original article.
@rushikesh, unfortunately I’ve not used the -script parameter and can’t see how to use it. If I get time this week I’ll try and figure it out. Chances are I’ll be busy though.