require 'spec_helper' describe Faraday::Response do before do @client = Capgun::Client.new end { 400 => Capgun::Error::BadRequest, 401 => Capgun::Error::Unauthorized, 403 => Capgun::Error::Forbidden, 404 => Capgun::Error::NotFound, 406 => Capgun::Error::NotAcceptable, 500 => Capgun::Error::InternalServerError, 502 => Capgun::Error::BadGateway, 503 => Capgun::Error::ServiceUnavailable, }.each do |status, exception| if (status >= 500) context "when HTTP status is #{status}" do before do stub_request(:get, "https://api.capgun.io/v1/jobs/abc123.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.0.3'}). to_return(:status => status, :body => "", :headers => {}) end it "should raise #{exception.name} error" do lambda do @client.status('abc123') end.should raise_error(exception) end end else [nil, "message", "error", "errors"].each do |body| context "when HTTP status is #{status} and body is #{body||='nil'}" do before do body_message = '{"'+body+'":"test"}' unless body.nil? stub_request(:get, "https://api.capgun.io/v1/jobs/abc123.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.0.3'}). to_return(:status => status, :body => body_message, :headers => {}) end it "should raise #{exception.name} error" do lambda do @client.status('abc123') end.should raise_error(exception) end end end end end context "when response status is 404 from lookup" do before do stub_request(:get, "https://api.capgun.io/v1/jobs/abc123.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.0.3'}). to_return(:status => 404, :body => fixture('notfound.json'), :headers => {}) end it "should raise Capgun::Error::NotFound" do lambda do @client.status('abc123') end.should raise_error(Capgun::Error::NotFound) end end end