Issue 59 - handle 204 responses more gracefully
authorSteven Bazyl <sqrrrl@gmail.com>
Fri, 5 Oct 2012 01:17:54 +0000 (18:17 -0700)
committerSteven Bazyl <sqrrrl@gmail.com>
Wed, 10 Oct 2012 20:28:46 +0000 (14:28 -0600)
lib/google/api_client/result.rb
spec/google/api_client/result_spec.rb

index 8920f942137eba9864db6d0099a492a2e570c4ad..38d08594ec39da27a609c9647bfac822ed81c840 100644 (file)
@@ -71,7 +71,11 @@ module Google
         _, content_type = self.headers.detect do |h, v|
           h.downcase == 'Content-Type'.downcase
         end
-        content_type[/^([^;]*);?.*$/, 1].strip.downcase
+        if content_type
+          return content_type[/^([^;]*);?.*$/, 1].strip.downcase
+        else
+          return nil
+        end
       end
       
       ##
@@ -121,7 +125,7 @@ module Google
       # @return [TrueClass, FalseClass]
       #   true if body can be parsed
       def data?
-        self.media_type == 'application/json'
+        !(self.body.nil? || self.body.empty? || self.media_type != 'application/json')
       end
       
       ##
@@ -132,26 +136,28 @@ module Google
       #   Object if body parsable from API schema, Hash if JSON, raw body if unable to parse
       def data
         return @data ||= (begin
-          media_type = self.media_type
-          data = self.body
-          case media_type
-          when 'application/json'
-            data = MultiJson.load(data)
-            # Strip data wrapper, if present
-            data = data['data'] if data.has_key?('data')
-          else
-            raise ArgumentError,
-              "Content-Type not supported for parsing: #{media_type}"
-          end
-          if @request.api_method && @request.api_method.response_schema
-            # Automatically parse using the schema designated for the
-            # response of this API method.
-            data = @request.api_method.response_schema.new(data)
-            data
-          else
-            # Otherwise, return the raw unparsed value.
-            # This value must be indexable like a Hash.
-            data
+          if self.data?
+            media_type = self.media_type
+            data = self.body
+            case media_type
+            when 'application/json'
+              data = MultiJson.load(data)
+              # Strip data wrapper, if present
+              data = data['data'] if data.has_key?('data')
+            else
+              raise ArgumentError,
+                "Content-Type not supported for parsing: #{media_type}"
+            end
+            if @request.api_method && @request.api_method.response_schema
+              # Automatically parse using the schema designated for the
+              # response of this API method.
+              data = @request.api_method.response_schema.new(data)
+              data
+            else
+              # Otherwise, return the raw unparsed value.
+              # This value must be indexable like a Hash.
+              data
+            end
           end
         end)
       end
index eba302348b2a7a9b4ebbc88d78042f0f1dd5214a..dea1bc1542b8dc6492f6fe5d1b2cf8fd84d1c08e 100644 (file)
@@ -180,7 +180,27 @@ describe Google::APIClient::Result do
       it 'should return the correct error message' do
         @result.error_message.should == 'Parse Error'
       end
+    end
+    
+    describe 'with 204 No Content response' do
+      before do
+        @response.stub(:body).and_return('')
+        @response.stub(:status).and_return(204)
+        @response.stub(:headers).and_return({})
+        @result = Google::APIClient::Result.new(@reference, @response)
+      end
 
+      it 'should indicate no data is available' do
+        @result.data?.should be_false
+      end
+      
+      it 'should return nil for data' do
+        @result.data.should == nil
+      end
+      
+      it 'should return nil for media_type' do
+        @result.media_type.should == nil
+      end
     end
   end
 end