diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 215fe0c..7327b92 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -207,7 +207,9 @@ module RestClient end def self.decode content_encoding, body - if content_encoding == 'gzip' and not body.empty? + if (!body) || body.empty? + body + elsif content_encoding == 'gzip' Zlib::GzipReader.new(StringIO.new(body)).read elsif content_encoding == 'deflate' Zlib::Inflate.new.inflate body diff --git a/spec/request_spec.rb b/spec/request_spec.rb index 4227ab6..f0dac7a 100644 --- a/spec/request_spec.rb +++ b/spec/request_spec.rb @@ -26,10 +26,16 @@ describe RestClient::Request do end describe "compression" do + it "decodes an uncompressed result body by passing it straight through" do RestClient::Request.decode(nil, 'xyz').should == 'xyz' end + it "doesn't fail for nil bodies" do + RestClient::Request.decode('gzip', nil).should be_nil + end + + it "decodes a gzip body" do RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n" end