diff options
-rw-r--r-- | chef/lib/chef/rest.rb | 3 | ||||
-rw-r--r-- | chef/spec/unit/rest_spec.rb | 15 |
2 files changed, 18 insertions, 0 deletions
diff --git a/chef/lib/chef/rest.rb b/chef/lib/chef/rest.rb index 27b2498ac1..1a0aab3170 100644 --- a/chef/lib/chef/rest.rb +++ b/chef/lib/chef/rest.rb @@ -250,6 +250,9 @@ class Chef elsif redirect_location = redirected_to(response) follow_redirect {api_request(:GET, create_url(redirect_location))} else + # have to decompress the body before making an exception for it. But the body could be nil. + response.body.replace(decompress_body(response)) if response.body.respond_to?(:replace) + if response['content-type'] =~ /json/ exception = Chef::JSONCompat.from_json(response_body) msg = "HTTP Request Returned #{response.code} #{response.message}: " diff --git a/chef/spec/unit/rest_spec.rb b/chef/spec/unit/rest_spec.rb index d82cc772e1..fff3926b89 100644 --- a/chef/spec/unit/rest_spec.rb +++ b/chef/spec/unit/rest_spec.rb @@ -419,6 +419,21 @@ describe Chef::REST do @log_stringio.string.should match(Regexp.escape('WARN: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four')) end + it "decompresses 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") + http_response.add_field("content-encoding", "deflate") + unzipped_body = '{ "error":[ "Ears get sore!", "Not even four" ] }' + gzipped_body = Zlib::Deflate.deflate(unzipped_body, 1) + http_response.stub!(:body).and_return gzipped_body + http_response.stub!(:read_body) + @rest.stub!(:sleep) + @http_client.stub!(:request).and_yield(http_response).and_return(http_response) + + lambda {@rest.run_request(:GET, @url)}.should raise_error(Net::HTTPFatalError) + @log_stringio.string.should match(Regexp.escape('WARN: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four')) + end + it "should raise an exception on an unsuccessful request" do http_response = Net::HTTPServerError.new("1.1", "500", "drooling from inside of mouth") http_response.stub!(:body) |