Skip to content

Commit 80a2383

Browse files
committed
Ensure get_parse_node does not fail when response body is empty
1 parent 3a63521 commit 80a2383

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/microsoft_kiota_faraday/faraday_request_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def get_root_parse_node(response)
7171
raise StandardError, 'response cannot be null' unless response
7272
response_content_type = self.get_response_content_type(response);
7373
raise StandardError, 'no response content type found for deserialization' unless response_content_type
74+
return if response.body.nil? || response.body.empty?
7475
return @parse_node_factory.get_parse_node(response_content_type, response.body)
7576
end
7677

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
RSpec.describe MicrosoftKiotaFaraday::FaradayRequestAdapter do
3+
subject(:adapter) { described_class.new(authentication_provider) }
4+
5+
let(:authentication_provider) { double("authentication_provider") }
6+
let(:headers) { { 'content-type' => 'application/json' } }
7+
let(:body) { { key: "value" }.to_json }
8+
9+
describe "#get_root_parse_node" do
10+
context "when response is null" do
11+
it "raises an error" do
12+
expect { adapter.get_root_parse_node(nil) }.to raise_error(StandardError, 'response cannot be null')
13+
end
14+
end
15+
16+
context "when response content type is not found" do
17+
let(:response) { instance_double(Faraday::Response, body:, headers: {}) }
18+
19+
it "raises an error" do
20+
expect { adapter.get_root_parse_node(response) }.to raise_error(StandardError, 'no response content type found for deserialization')
21+
end
22+
end
23+
24+
context "when response body is nil" do
25+
let(:response) { instance_double(Faraday::Response, body: nil, headers:) }
26+
27+
it "returns nil" do
28+
expect(adapter.get_root_parse_node(response)).to be_nil
29+
end
30+
end
31+
32+
context "when response body is empty" do
33+
let(:response) { instance_double(Faraday::Response, body: "", headers:) }
34+
35+
it "returns nil" do
36+
expect(adapter.get_root_parse_node(response)).to be_nil
37+
end
38+
end
39+
40+
context "when response body is not nil" do
41+
subject(:adapter) { described_class.new(authentication_provider, parse_node_factory) }
42+
43+
let(:response) { instance_double(Faraday::Response, body:, headers:) }
44+
let(:parse_node_factory) { double("parse_node_factory") }
45+
let(:parse_node) { double("parse_node") }
46+
47+
before do
48+
allow(parse_node_factory).to receive(:get_parse_node).with("application/json", body).and_return(parse_node)
49+
end
50+
51+
it "returns the parse node" do
52+
expect(adapter.get_root_parse_node(response)).to eq(parse_node)
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)