Thursday 19 May 2011

Rspec testing IP based authentication

Setting up a simple service in Sinatra with IP based authentication where localhost is in the whitelist of allowed IP. The problem was that all tests were run under local host, fortunately you can pass rack environment as parameters to the get request. Example test

it "should fail authentication" do
get '/services/order/43439', {}, 'REMOTE_ADDR' => '192.168.9.873'
last_response.status.should == 401
attributes = Hash.from_xml(last_response.body)
attributes["errors"]["error"].should == "You are not authorised"
end

Where 192.168.9.873 is not in the whitelist

No comments:

Post a Comment