diff options
-rw-r--r-- | lib/chef/rest.rb | 6 | ||||
-rw-r--r-- | spec/unit/rest_spec.rb | 9 |
2 files changed, 13 insertions, 2 deletions
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb index 43525082f2..ab9c3b20e3 100644 --- a/lib/chef/rest.rb +++ b/lib/chef/rest.rb @@ -280,8 +280,10 @@ class Chef Chef::JSONCompat.from_json(response_body.chomp) else Chef::Log.warn("Expected JSON response, but got content-type '#{response['content-type']}'") - response_body + response_body.to_s end + elsif response.kind_of?(Net::HTTPNotModified) # Must be tested before Net::HTTPRedirection because it's subclass. + false elsif redirect_location = redirected_to(response) follow_redirect {api_request(:GET, create_url(redirect_location))} else @@ -306,7 +308,7 @@ class Chef end def decompress_body(response) - if gzip_disabled? + if gzip_disabled? || response.body.nil? response.body else case response[CONTENT_ENCODING] diff --git a/spec/unit/rest_spec.rb b/spec/unit/rest_spec.rb index 5eacf89eed..1675384d1e 100644 --- a/spec/unit/rest_spec.rb +++ b/spec/unit/rest_spec.rb @@ -457,6 +457,15 @@ describe Chef::REST do end end + it "should return `false` when response is 304 NotModified" do + http_response = Net::HTTPNotModified.new("1.1", "304", "it's the same as when you asked 5 minutes ago") + http_response.stub!(:read_body) + + @http_client.stub!(:request).and_yield(http_response).and_return(http_response) + + @rest.api_request(:GET, @url).should be_false + end + it "should show the JSON error message on an unsuccessful request" do http_response = Net::HTTPServerError.new("1.1", "500", "drooling from inside of mouth") http_response.add_field("content-type", "application/json") |