Skip to content

Commit 6f4dcc6

Browse files
-- specs covering TimeoutError, ConnectionError, and HTTPError (status, body, and message all asserted).
1 parent 53854a4 commit 6f4dcc6

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe CrewAI::Client do
4+
let(:configuration) do
5+
CrewAI::Configuration.new.tap do |c|
6+
c.access_token = "test-token"
7+
c.uri_base = "https://test.crewai.com"
8+
end
9+
end
10+
let(:client) { described_class.new(configuration: configuration) }
11+
let(:uri_base) { "https://test.crewai.com" }
12+
13+
describe "transport errors" do
14+
context "when the request times out" do
15+
before do
16+
stub_request(:get, "#{uri_base}/healthcheck")
17+
.to_raise(Faraday::TimeoutError.new("timed out"))
18+
end
19+
20+
it "raises CrewAI::TimeoutError" do
21+
expect { client.healthcheck }.to raise_error(CrewAI::TimeoutError)
22+
end
23+
end
24+
25+
context "when the connection fails" do
26+
before do
27+
stub_request(:get, "#{uri_base}/healthcheck")
28+
.to_raise(Faraday::ConnectionFailed.new("connection refused"))
29+
end
30+
31+
it "raises CrewAI::ConnectionError" do
32+
expect { client.healthcheck }.to raise_error(CrewAI::ConnectionError, /connection refused/)
33+
end
34+
end
35+
end
36+
37+
describe "unexpected HTTP status (catch-all)" do
38+
context "when the server returns 429 Too Many Requests" do
39+
before do
40+
stub_request(:get, "#{uri_base}/healthcheck")
41+
.to_return(
42+
status: 429,
43+
body: { "message" => "rate limit exceeded" }.to_json,
44+
headers: { "Content-Type" => "application/json" }
45+
)
46+
end
47+
48+
it "raises CrewAI::HTTPError" do
49+
expect { client.healthcheck }.to raise_error(CrewAI::HTTPError)
50+
end
51+
52+
it "exposes the HTTP status on the exception" do
53+
client.healthcheck
54+
rescue CrewAI::HTTPError => e
55+
expect(e.status).to eq(429)
56+
end
57+
58+
it "exposes the parsed body on the exception" do
59+
client.healthcheck
60+
rescue CrewAI::HTTPError => e
61+
expect(e.body).to eq({ "message" => "rate limit exceeded" })
62+
end
63+
64+
it "includes the server message in the exception message" do
65+
expect { client.healthcheck }.to raise_error(CrewAI::HTTPError, /rate limit exceeded/)
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)