From 9474b39e359e41fd92f801633c36d2c8d1386593 Mon Sep 17 00:00:00 2001 From: Andy Jeffries Date: Tue, 24 Jul 2018 10:51:09 +0100 Subject: [PATCH] Fixing empty response bodies with an unparseable header --- CHANGELOG.md | 6 ++++++ lib/flexirest/request.rb | 5 ++++- lib/flexirest/version.rb | 2 +- spec/lib/request_spec.rb | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad1624d..a519b29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.6.9 + +Feature: + +- Empty response bodies with an unparseable content-type are forced to be JSON with an empty JSON body + ## 1.6.8 Feature: diff --git a/lib/flexirest/request.rb b/lib/flexirest/request.rb index 4ddaada..df5f0cf 100644 --- a/lib/flexirest/request.rb +++ b/lib/flexirest/request.rb @@ -524,7 +524,10 @@ def handle_cached_response(cached) def handle_response(response, cached = nil) @response = response status = @response.status || 200 - @response.body = "{}" if @response.body.blank? + if @response.body.blank? + @response.response_headers['Content-Type'] = "application/json" + @response.body = "{}" + end if cached && response.status == 304 Flexirest::Logger.debug " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name}" + diff --git a/lib/flexirest/version.rb b/lib/flexirest/version.rb index ef1693e..e80ab18 100644 --- a/lib/flexirest/version.rb +++ b/lib/flexirest/version.rb @@ -1,3 +1,3 @@ module Flexirest - VERSION = "1.6.8" + VERSION = "1.6.9" end diff --git a/spec/lib/request_spec.rb b/spec/lib/request_spec.rb index bc31ba6..4f59c70 100644 --- a/spec/lib/request_spec.rb +++ b/spec/lib/request_spec.rb @@ -430,6 +430,11 @@ class WhitelistedDateClient < Flexirest::Base expect(ExampleClient.all).to be_truthy end + it "should return true from 200 with empty bodies even if they don't have a correct content type" do + expect_any_instance_of(Flexirest::Connection).to receive(:get).with(any_args).and_return(::FaradayResponseMock.new(OpenStruct.new(status:200, response_headers:{"Content-Type" => "text/plain"}, body: nil))) + expect(ExampleClient.all).to be_truthy + end + it "should return a lazy loader object if lazy loading is enabled" do object = LazyLoadedExampleClient.fake id:1234, debug:true expect(object).to be_an_instance_of(Flexirest::LazyLoader)