Tuesday, 15 October 2013

HTTParty with cookies

On the past few months I've been doing automated testing work on an rest API that happens to rely a lot on cookies for user authentication stuff.
HTTParty is a great gem for working with rest API's, it's easy and simple, but it doesn't provide a care free way of dealing with cookies. I mean, you have a way to read the cookies from the response, you have a way to add them to the request, but you'll have to do it manually. So like any good Rubist I've made the code for it into a little gem. 
I hope it helps someone else as well. 
You can find most of the information on github.

From the readme:
This gem allows you to use HTTParty with auto cookie management.
Cookies returned in the responses will be saved and automatically sent in the next requests!
Getting started:
"gem install httparty_with_cookies"
Then you'll need a class to use it:
require 'httparty_with_cookies'

class My_awesome_cookie_using_api
    include HTTParty_with_cookies

api = My_awesome_cookie_using_api.new
api.get 'http://someurl.com/endpoint'
api.cookies['darkside'] #=> 'We have cookies!'
If you're familiar with HTTParty. then the rest of the gem's usage will be simple to you, since this gem is basically handling saving and send the cookies in HTTParty. If you don't know HTTParty, you can read about ithere, this gem just making using cookies with it brainless and painless.


Currently this gem will read all the cookies the server returns save it on your class' instance and for any next request it will send the cookies back - pays no attention to domain or expiration information.
This works as a module providing instance methods to a class and not as module with class methods as HTTParty!
Like most open source, I've made this because it solves my problem, and perhaps other people's with similar problems, feel free to add if it's not enough for you :)


On spec/server there's a very small sinatra sever with cookie setting page, cookie reading page and a no cookies at all page, made to support get, post, delete and put methods, you'll need to fire this server up before running the spec tests. I cheated a little bit on the spec, don't hate me.