From 1056520a116879cfb1998057d09396d525118c24 Mon Sep 17 00:00:00 2001 From: Ates Goral Date: Mon, 13 May 2024 14:19:25 -0400 Subject: [PATCH] Add test case for chunked error body --- spec/openai/client/http_spec.rb | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/spec/openai/client/http_spec.rb b/spec/openai/client/http_spec.rb index 24d9fa7c..0ecc6f40 100644 --- a/spec/openai/client/http_spec.rb +++ b/spec/openai/client/http_spec.rb @@ -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" }'))