require 'test_helper' require 'json' class RequestTest < Test::Unit::TestCase context "Request no SSL" do setup do @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, false) end context "rooms" do should "should raise error if invalid subdomain" do stub_rooms_request({:code => 404}) assert_raises Kindling::InvalidSubdomain do Kindling::Request.rooms(@campfire) end end should "should raise error if invalid api_token" do stub_rooms_request({:code => 401}) assert_raises Kindling::InvalidAPIToken do Kindling::Request.rooms(@campfire) end end should "retry with SSL if 405" do stub_rooms_request({:code => 405}) stub_rooms_request({:code => 200, :[] => JSON.parse(fixture_file("rooms.json"))["rooms"]}, true) assert_equal [{"name"=>"Room 1", "id"=>201712}, {"name"=>"Room 2", "id"=>298625}, {"name"=>"Room 3", "id"=>206877}], Kindling::Request.rooms(@campfire) end should "should return rooms list" do stub_rooms_request({:code => 200, :[] => JSON.parse(fixture_file("rooms.json"))["rooms"]}) assert_equal [{"name"=>"Room 1", "id"=>201712}, {"name"=>"Room 2", "id"=>298625}, {"name"=>"Room 3", "id"=>206877}], Kindling::Request.rooms(@campfire) end end context "send message" do context "with room id" do should "send message successfully if 20x" do [200, 201].each do |code| stub_send_message_request('test message', {:code => code}) Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID) end end should "raise error if invalid room id" do stub_send_message_request('test message', {:code => 404}) assert_raises Kindling::InvalidRoomID do Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID) end end should "raise error if invalid API token" do stub_send_message_request('test message', {:code => 401}) assert_raises Kindling::InvalidAPIToken do Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID) end end should "retry with SSL if 405" do stub_send_message_request('test message', {:code => 405}) stub_send_message_request('test message', {:code => 200}, true) Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID) end end end end context "Request SSL" do setup do @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, true) end context "with room id" do should "send message successfully if 20x" do [200, 201].each do |code| stub_send_message_request('test message', {:code => code}, true) Kindling::Request.speak("test message", @campfire, TEST_ROOM_ID) end end end end end