From 31ad4f91c8c234b15b0d415eb2c088f6838846dc Mon Sep 17 00:00:00 2001 From: Alex Vollmer Date: Wed, 27 Aug 2008 13:38:19 -0700 Subject: [PATCH] Treat nil or empty bodies specially to avoid blowing up during response parsing. --- lib/httparty.rb | 1 + spec/httparty_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/httparty.rb b/lib/httparty.rb index 3d7dee7..00a105e 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -139,6 +139,7 @@ module HTTParty end def parse_response(body) #:nodoc: + return nil if body.nil? or body.empty? case @format when :xml Hash.from_xml(body) diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 1cf5778..85b6143 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -141,5 +141,20 @@ describe HTTParty do Foo.send(:send_request, 'get', '/foo', :basic_auth => 'string') end.should raise_error(ArgumentError) end + + it "should not attempt to parse empty responses" do + http = Net::HTTP.new('localhost', 80) + Foo.stub!(:http).and_return(http) + response = Net::HTTPNoContent.new("1.1", 204, "No content for you") + response.stub!(:body).and_return(nil) + http.stub!(:request).and_return(response) + + Foo.headers.clear # clear out bogus settings from other specs + Foo.format :xml + Foo.send(:send_request, 'get', '/bar').should be_nil + + response.stub!(:body).and_return("") + Foo.send(:send_request, 'get', 'bar').should be_nil + end end end \ No newline at end of file