Thursday, August 12, 2010

using curl to test rails app session management

I am building a rails app that communicates json back and forth to a client. I needed to test the session creation mechanisms and also some posting of objects. Here are some handy curl calls:

Post a json object { "FishID":"greatwhite" } while also passing the Content-Type and Accept headers as "application/json" so rails will autmatically turn the body of the request into params in the params[] array the below would be translated into [:FishID=>"greatwhite"]

curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"FishID\":\"greatwhite\"}" http://74.116.250.34/createFish


Post a token to a session start action that will return the session id to the client then save the cookies that are sent back to a file that can be re-used in subsequent curl calls thus simulating a longer running session

curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"token\":\"ja9er4fn9\"}" -c cookies.txt http://localhost/beginSession

Now put the two together:

Re-use a text file with cookies saved by a previous curl session to re-use the old session just use the -b switch again
curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"FishID\":\"greatwhite\"}" -b cookies.txt http://localhost/createFish

That last call should be able to instantiate a fish for the account of the user based on the session from the session cookie.

No comments: