Skip to content

Commit

Permalink
Fixing empty response bodies with an unparseable header
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed Jul 24, 2018
1 parent 25193da commit 9474b39
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 4 additions & 1 deletion lib/flexirest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" +
Expand Down
2 changes: 1 addition & 1 deletion lib/flexirest/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Flexirest
VERSION = "1.6.8"
VERSION = "1.6.9"
end
5 changes: 5 additions & 0 deletions spec/lib/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9474b39

Please sign in to comment.