summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel DeLeo <dan@opscode.com>2012-01-18 18:07:04 -0800
committerDaniel DeLeo <dan@opscode.com>2012-01-18 18:07:04 -0800
commit7c957be1664cee63f0209160b99ab554d15ff4ec (patch)
tree829588e8b838d153d23dba633d4eb29c884e4759
parentba48a30239ae9e19707338bd184630d45d9601ce (diff)
downloadchef-7c957be1664cee63f0209160b99ab554d15ff4ec.tar.gz
[CHEF-2872] decompress body when request is unsuccessful
-rw-r--r--chef/lib/chef/rest.rb3
-rw-r--r--chef/spec/unit/rest_spec.rb15
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)