Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,42 @@
end
end

context "with a HTTP error response with body containing JSON split across chunks" do
it "raise an error" do
env = Faraday::Env.from(
method: :post,
url: URI("http://example.com"),
status: 400,
request: {},
response: Faraday::Response.new
)

expected_body = {
"error" => {
"message" => "Test error",
"type" => "test_error",
"param" => nil,
"code" => "test"
}
}

json = expected_body.to_json
# Split the JSON into two chunks in the middle
chunks = [json[0..(json.length / 2)], json[(json.length / 2)..]]

begin
chunks.each do |chunk|
stream.call(chunk, 0, env)
end
rescue Faraday::BadRequestError => e
expect(e.response).to include(status: 400)
expect(e.response[:body]).to eq(expected_body)
else
raise "Expected to raise Faraday::BadRequestError"
end
end
end

context "when called with JSON split across chunks" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{ "foo": "bar" }'))
Expand Down