From 80a238329a78440600bae7bdfe789aa35f30c266 Mon Sep 17 00:00:00 2001 From: Louis-Michel Couture Date: Thu, 15 Feb 2024 21:18:58 -0500 Subject: [PATCH 1/2] Ensure get_parse_node does not fail when response body is empty --- .../faraday_request_adapter.rb | 1 + .../faraday_request_adapter_spec.rb | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 spec/microsoft_kiota_faraday/faraday_request_adapter_spec.rb diff --git a/lib/microsoft_kiota_faraday/faraday_request_adapter.rb b/lib/microsoft_kiota_faraday/faraday_request_adapter.rb index 72e2940..85cee2e 100644 --- a/lib/microsoft_kiota_faraday/faraday_request_adapter.rb +++ b/lib/microsoft_kiota_faraday/faraday_request_adapter.rb @@ -71,6 +71,7 @@ def get_root_parse_node(response) raise StandardError, 'response cannot be null' unless response response_content_type = self.get_response_content_type(response); raise StandardError, 'no response content type found for deserialization' unless response_content_type + return if response.body.nil? || response.body.empty? return @parse_node_factory.get_parse_node(response_content_type, response.body) end diff --git a/spec/microsoft_kiota_faraday/faraday_request_adapter_spec.rb b/spec/microsoft_kiota_faraday/faraday_request_adapter_spec.rb new file mode 100644 index 0000000..289fefb --- /dev/null +++ b/spec/microsoft_kiota_faraday/faraday_request_adapter_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true +RSpec.describe MicrosoftKiotaFaraday::FaradayRequestAdapter do + subject(:adapter) { described_class.new(authentication_provider) } + + let(:authentication_provider) { double("authentication_provider") } + let(:headers) { { 'content-type' => 'application/json' } } + let(:body) { { key: "value" }.to_json } + + describe "#get_root_parse_node" do + context "when response is null" do + it "raises an error" do + expect { adapter.get_root_parse_node(nil) }.to raise_error(StandardError, 'response cannot be null') + end + end + + context "when response content type is not found" do + let(:response) { instance_double(Faraday::Response, body:, headers: {}) } + + it "raises an error" do + expect { adapter.get_root_parse_node(response) }.to raise_error(StandardError, 'no response content type found for deserialization') + end + end + + context "when response body is nil" do + let(:response) { instance_double(Faraday::Response, body: nil, headers:) } + + it "returns nil" do + expect(adapter.get_root_parse_node(response)).to be_nil + end + end + + context "when response body is empty" do + let(:response) { instance_double(Faraday::Response, body: "", headers:) } + + it "returns nil" do + expect(adapter.get_root_parse_node(response)).to be_nil + end + end + + context "when response body is not nil" do + subject(:adapter) { described_class.new(authentication_provider, parse_node_factory) } + + let(:response) { instance_double(Faraday::Response, body:, headers:) } + let(:parse_node_factory) { double("parse_node_factory") } + let(:parse_node) { double("parse_node") } + + before do + allow(parse_node_factory).to receive(:get_parse_node).with("application/json", body).and_return(parse_node) + end + + it "returns the parse node" do + expect(adapter.get_root_parse_node(response)).to eq(parse_node) + end + end + end +end From 5b1bd32b3e89facd820cde247eedc53780d7456a Mon Sep 17 00:00:00 2001 From: Louis-Michel Couture Date: Fri, 16 Feb 2024 09:41:01 -0500 Subject: [PATCH 2/2] Add changelog and bump version --- CHANGELOG.md | 6 ++++++ lib/microsoft_kiota_faraday/version.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b404ed..2f7816c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [0.15.0] - 2024-02-16 + +### Changed + +- Ensure get_parse_node does not fail when response body is empty. [#27](https://github.com/microsoft/kiota-http-ruby/pull/28) + ## [0.14.0] - 2024-02-14 ### Changed diff --git a/lib/microsoft_kiota_faraday/version.rb b/lib/microsoft_kiota_faraday/version.rb index 5a05b70..72c4bfd 100644 --- a/lib/microsoft_kiota_faraday/version.rb +++ b/lib/microsoft_kiota_faraday/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MicrosoftKiotaFaraday - VERSION = '0.14.0' + VERSION = '0.15.0' end