Skip to content

Commit 5f777cd

Browse files
Test that faraday errors are thrown if error response is not seam api error
1 parent 1578c5c commit 5f777cd

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

spec/seam_client/request_spec.rb

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,45 @@
4848
end
4949
end
5050

51-
context "when other API error occurs" do
52-
let(:error_message) { "An unknown error occurred" }
51+
context "when non-Seam API error occurs" do
5352
let(:error_status) { 500 }
54-
let(:error_response) do
55-
{
56-
error: {
57-
type: "api_error",
58-
message: error_message,
59-
data: nil
60-
}
61-
}.to_json
53+
let(:error_response) { "Internal Server Error" }
54+
55+
before do
56+
stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list")
57+
.to_return(status: error_status, body: error_response, headers: {"Content-Type" => "text/plain"})
58+
end
59+
60+
it "raises Faraday error" do
61+
expect { seam.devices.list }.to raise_error(Faraday::ServerError)
6262
end
63+
end
64+
65+
context "when malformed JSON response" do
66+
let(:error_status) { 500 }
67+
let(:error_response) { "{invalid json" }
6368

6469
before do
6570
stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list")
66-
.to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json",
67-
"seam-request-id" => request_id})
71+
.to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json"})
6872
end
6973

70-
it "raises ApiError with the correct details" do
71-
expect { seam.devices.list }.to raise_error(Seam::Http::ApiError) do |error|
72-
expect(error.message).to eq(error_message)
73-
expect(error.status_code).to eq(error_status)
74-
expect(error.request_id).to eq(request_id)
75-
expect(error.data).to be_nil
76-
end
74+
it "raises Faraday error" do
75+
expect { seam.devices.list }.to raise_error(Faraday::ServerError)
76+
end
77+
end
78+
79+
context "when JSON response without error object" do
80+
let(:error_status) { 500 }
81+
let(:error_response) { '{"message": "Some error"}' }
82+
83+
before do
84+
stub_request(:post, "#{Seam::DEFAULT_ENDPOINT}/devices/list")
85+
.to_return(status: error_status, body: error_response, headers: {"Content-Type" => "application/json"})
86+
end
87+
88+
it "raises Faraday error" do
89+
expect { seam.devices.list }.to raise_error(Faraday::ServerError)
7790
end
7891
end
7992
end

0 commit comments

Comments
 (0)