summaryrefslogtreecommitdiffhomepage
path: root/spec/dispatch/adapter/claude/http_client_spec.rb
blob: 1301b6d55d51980640424c5258d67f6749e70633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# frozen_string_literal: true

require "webmock/rspec"

RSpec.describe Dispatch::Adapter::Claude::HttpClient do
  let(:base_url)     { "https://api.anthropic.com" }
  let(:test_headers) { { "Content-Type" => "application/json", "Accept" => "application/json" } }
  let(:headers_proc) do
    lambda { |stream: false|
      _ = stream
      test_headers
    }
  end

  subject(:client) { described_class.new(base_url: base_url, headers_proc: headers_proc) }

  before { WebMock.disable_net_connect! }
  after  { WebMock.reset! }

  # ── post_json ─────────────────────────────────────────────────────────────

  describe "#post_json" do
    let(:path)         { "/v1/messages" }
    let(:request_body) { { model: "claude-sonnet-4-6", messages: [] } }
    let(:response_body) do
      { "id" => "msg_01", "type" => "message", "content" => [], "role" => "assistant" }
    end

    context "on a 200 response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 200, body: JSON.generate(response_body),
                     headers: { "Content-Type" => "application/json" })
      end

      it "returns the parsed JSON response body" do
        result = client.post_json(path, request_body)
        expect(result).to eq(response_body)
      end

      it "sends the body serialized as JSON" do
        client.post_json(path, request_body)
        expect(WebMock).to have_requested(:post, "#{base_url}#{path}")
          .with(body: JSON.generate(request_body))
      end

      it "sends the headers from the headers_proc" do
        client.post_json(path, request_body)
        expect(WebMock).to have_requested(:post, "#{base_url}#{path}")
          .with(headers: { "Content-Type" => "application/json" })
      end
    end

    context "on a 401 response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 401,
                     body: JSON.generate({ "error" => { "message" => "Unauthorized" } }),
                     headers: { "Content-Type" => "application/json" })
      end

      it "raises AuthenticationError" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::AuthenticationError)
      end

      it "includes the error message" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::AuthenticationError, /Unauthorized/)
      end
    end

    context "on a 429 response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 429,
                     body: JSON.generate({ "error" => { "message" => "Rate limited" } }),
                     headers: { "Content-Type" => "application/json", "Retry-After" => "30" })
      end

      it "raises RateLimitError" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::RateLimitError)
      end
    end

    context "on a 500 response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 500,
                     body: JSON.generate({ "error" => { "message" => "Internal error" } }),
                     headers: { "Content-Type" => "application/json" })
      end

      it "raises ServerError" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::ServerError)
      end
    end

    context "with malformed JSON response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 200, body: "not json at all",
                     headers: { "Content-Type" => "text/plain" })
      end

      it "raises RequestError" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::RequestError, /Failed to parse JSON/)
      end
    end
  end

  # ── get_json ──────────────────────────────────────────────────────────────

  describe "#get_json" do
    let(:path) { "/v1/models" }
    let(:response_body) { { "data" => [{ "id" => "claude-sonnet-4-6" }] } }

    context "on a 200 response" do
      before do
        stub_request(:get, "#{base_url}#{path}")
          .to_return(status: 200, body: JSON.generate(response_body),
                     headers: { "Content-Type" => "application/json" })
      end

      it "returns the parsed JSON response body" do
        result = client.get_json(path)
        expect(result).to eq(response_body)
      end

      it "uses a GET request (not POST)" do
        client.get_json(path)
        expect(WebMock).to have_requested(:get, "#{base_url}#{path}")
        expect(WebMock).not_to have_requested(:post, "#{base_url}#{path}")
      end
    end

    context "on a 401 response" do
      before do
        stub_request(:get, "#{base_url}#{path}")
          .to_return(status: 401,
                     body: JSON.generate({ "error" => { "message" => "Unauthorized" } }),
                     headers: { "Content-Type" => "application/json" })
      end

      it "raises AuthenticationError" do
        expect { client.get_json(path) }
          .to raise_error(Dispatch::Adapter::AuthenticationError)
      end
    end
  end

  # ── stream ────────────────────────────────────────────────────────────────

  describe "#stream" do
    let(:path)         { "/v1/messages" }
    let(:request_body) { { model: "claude-sonnet-4-6", stream: true } }
    let(:sse_body)     { "data: {\"type\":\"message_start\"}\n\ndata: [DONE]\n\n" }

    context "on a 200 SSE response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 200, body: sse_body,
                     headers: { "Content-Type" => "text/event-stream" })
      end

      it "yields a Net::HTTPResponse object" do
        yielded = nil
        client.stream(path, request_body) { |resp| yielded = resp }
        expect(yielded).to be_a(Net::HTTPResponse)
      end

      it "yields a response that responds to read_body" do
        client.stream(path, request_body) do |resp|
          expect(resp).to respond_to(:read_body)
        end
      end

      it "yields the response before it raises" do
        was_yielded = false
        client.stream(path, request_body) { |_resp| was_yielded = true }
        expect(was_yielded).to be true
      end

      it "sends a POST request with Accept: text/event-stream" do
        stream_headers = { "Content-Type" => "application/json", "Accept" => "text/event-stream" }
        stream_headers_proc = ->(stream: false) { stream ? stream_headers : test_headers }
        stream_client = described_class.new(base_url: base_url, headers_proc: stream_headers_proc)
        stream_client.stream(path, request_body) { |_r| nil }
        expect(WebMock).to have_requested(:post, "#{base_url}#{path}")
          .with(headers: { "Accept" => "text/event-stream" })
      end
    end

    context "when called without a block" do
      it "raises ArgumentError" do
        expect { client.stream(path, request_body) }
          .to raise_error(ArgumentError, /block/)
      end
    end

    context "on a 401 SSE response" do
      before do
        stub_request(:post, "#{base_url}#{path}")
          .to_return(status: 401,
                     body: JSON.generate({ "error" => { "message" => "Unauthorized" } }),
                     headers: { "Content-Type" => "application/json" })
      end

      it "raises AuthenticationError after yielding the response" do
        yielded = false
        expect do
          client.stream(path, request_body) { |_r| yielded = true }
        end.to raise_error(Dispatch::Adapter::AuthenticationError)
        expect(yielded).to be true
      end
    end
  end

  # ── Connection failures ───────────────────────────────────────────────────

  describe "connection failures" do
    let(:path)         { "/v1/messages" }
    let(:request_body) { {} }

    {
      "Errno::ECONNREFUSED" => Errno::ECONNREFUSED,
      "Errno::EHOSTUNREACH" => Errno::EHOSTUNREACH,
      "Errno::ETIMEDOUT" => Errno::ETIMEDOUT,
      "SocketError" => SocketError
    }.each do |error_name, error_class|
      context "when #{error_name} is raised" do
        before do
          stub_request(:post, "#{base_url}#{path}").to_raise(error_class)
        end

        it "raises ConnectionError" do
          expect { client.post_json(path, request_body) }
            .to raise_error(Dispatch::Adapter::ConnectionError)
        end

        it "includes the provider name in the error message" do
          expect { client.post_json(path, request_body) }
            .to raise_error(Dispatch::Adapter::ConnectionError,
                            /Anthropic \(Claude\)/)
        end

        it "sets the provider attribute on the error" do
          client.post_json(path, request_body)
        rescue Dispatch::Adapter::ConnectionError => e
          expect(e.provider).to eq("Anthropic (Claude)")
        end
      end
    end

    context "when Net::ReadTimeout is raised" do
      before do
        stub_request(:post, "#{base_url}#{path}").to_raise(Net::ReadTimeout)
      end

      it "raises ConnectionError" do
        expect { client.post_json(path, request_body) }
          .to raise_error(Dispatch::Adapter::ConnectionError)
      end
    end

    context "when SocketError is raised on get_json" do
      before do
        stub_request(:get, "#{base_url}/v1/models").to_raise(SocketError)
      end

      it "raises ConnectionError" do
        expect { client.get_json("/v1/models") }
          .to raise_error(Dispatch::Adapter::ConnectionError)
      end
    end
  end

  # ── Path normalization ────────────────────────────────────────────────────

  describe "path normalization" do
    it "adds a leading slash if the path does not have one" do
      stub_request(:get, "#{base_url}/v1/models")
        .to_return(status: 200, body: "{}", headers: {})
      # Should not raise — path is normalized correctly
      expect { client.get_json("v1/models") }.not_to raise_error
    end

    it "preserves an existing leading slash" do
      stub_request(:get, "#{base_url}/v1/models")
        .to_return(status: 200, body: "{}", headers: {})
      expect { client.get_json("/v1/models") }.not_to raise_error
    end
  end

  # ── base_url trailing slash tolerance ─────────────────────────────────────

  describe "base_url with trailing slash" do
    subject(:client_trailing) do
      described_class.new(base_url: "https://api.anthropic.com/",
                          headers_proc: headers_proc)
    end

    it "still connects to the correct host" do
      stub_request(:get, "https://api.anthropic.com/v1/models")
        .to_return(status: 200, body: "{}", headers: {})
      expect { client_trailing.get_json("/v1/models") }.not_to raise_error
    end
  end

  # ── Timeout constants ─────────────────────────────────────────────────────

  describe "timeout constants" do
    it "has OPEN_TIMEOUT of 30" do
      expect(described_class::OPEN_TIMEOUT).to eq(30)
    end

    it "has READ_TIMEOUT of 120" do
      expect(described_class::READ_TIMEOUT).to eq(120)
    end

    it "has STREAM_TIMEOUT of 300" do
      expect(described_class::STREAM_TIMEOUT).to eq(300)
    end
  end
end