Skip to content

Commit 9fe9d88

Browse files
authored
Merge pull request #28 from hooktstudios/bugfix/nil-empty-content-body
Ensure get_parse_node does not fail when response body is empty
2 parents 3a63521 + 5b1bd32 commit 9fe9d88

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
## [0.15.0] - 2024-02-16
15+
16+
### Changed
17+
18+
- Ensure get_parse_node does not fail when response body is empty. [#27](https://github.com/microsoft/kiota-http-ruby/pull/28)
19+
1420
## [0.14.0] - 2024-02-14
1521

1622
### Changed

lib/microsoft_kiota_faraday/faraday_request_adapter.rb

+1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module MicrosoftKiotaFaraday
4-
VERSION = '0.14.0'
4+
VERSION = '0.15.0'
55
end
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)