|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Layer::Api::Error do |
| 4 | + describe ".from_response" do |
| 5 | + it 'should raise NotFound error when status is 404' do |
| 6 | + VCR.use_cassette('conversation') do |
| 7 | + layer = Layer::Api::Client.new |
| 8 | + |
| 9 | + expect { |
| 10 | + layer.get_conversation("notfound") |
| 11 | + }.to raise_error(Layer::Api::NotFound) |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + it "should raise BadRequest error when status is 400" do |
| 16 | + response = {status: 400} |
| 17 | + expect { |
| 18 | + raise Layer::Api::Error.from_response(response) |
| 19 | + }.to raise_error(Layer::Api::BadRequest) |
| 20 | + end |
| 21 | + |
| 22 | + (500..599).each do |error| |
| 23 | + it "should raise ServerError when status is #{error}" do |
| 24 | + response = {status: error} |
| 25 | + expect { |
| 26 | + raise Layer::Api::Error.from_response(response) |
| 27 | + }.to raise_error(Layer::Api::ServerError) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + it "should return Error when status isn't found/handled" do |
| 32 | + expect { |
| 33 | + raise Layer::Api::Error.from_response({}) |
| 34 | + }.to raise_error(Layer::Api::Error) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe ".initialize" do |
| 39 | + it "should set @response instance variable" do |
| 40 | + begin |
| 41 | + raise Layer::Api::Error.new({}) |
| 42 | + rescue => e |
| 43 | + expect(e.response).to eq({}) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it "should build correct error message containing status & body" do |
| 48 | + status = '200' |
| 49 | + body = 'random_test_body' |
| 50 | + args = { |
| 51 | + status: status, |
| 52 | + body: body |
| 53 | + } |
| 54 | + error_msg = Layer::Api::Error.new(args).send(:build_error_message) |
| 55 | + expect(error_msg).to include(status) |
| 56 | + expect(error_msg).to include(body) |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments