Merge pull request #182 from seuros/patch-2
authorSteve Bazyl <sqrrrl@gmail.com>
Wed, 17 Dec 2014 20:20:43 +0000 (12:20 -0800)
committerSteve Bazyl <sqrrrl@gmail.com>
Wed, 17 Dec 2014 20:20:43 +0000 (12:20 -0800)
Update .travis.yml

12 files changed:
lib/google/api_client.rb
lib/google/api_client/batch.rb
lib/google/api_client/charset.rb [new file with mode: 0644]
lib/google/api_client/client_secrets.rb
lib/google/api_client/result.rb
lib/google/api_client/service.rb
spec/fixtures/files/client_secrets.json [new file with mode: 0644]
spec/fixtures/files/zoo.json [new file with mode: 0644]
spec/google/api_client/client_secrets_spec.rb [new file with mode: 0644]
spec/google/api_client/discovery_spec.rb
spec/google/api_client/gzip_spec.rb
spec/google/api_client/service_spec.rb

index 90f422bd0fbbb3d990034c74b8de165f0680e70b..eeaf2593398889bdad84d3c04dcb62a416f389d4 100644 (file)
@@ -31,6 +31,7 @@ require 'google/api_client/media'
 require 'google/api_client/service_account'
 require 'google/api_client/batch'
 require 'google/api_client/gzip'
+require 'google/api_client/charset'
 require 'google/api_client/client_secrets'
 require 'google/api_client/railtie' if defined?(Rails)
 
@@ -75,6 +76,9 @@ module Google
     # @option options [String] :ca_file
     #   Optional set of root certificates to use when validating SSL connections.
     #   By default, a bundled set of trusted roots will be used.
+    # @options options[Hash] :force_encoding
+    #   Experimental option. True if response body should be force encoded into the charset
+    #   specified in the Content-Type header. Mostly intended for compressed content.
     # @options options[Hash] :faraday_options
     #   Pass through of options to set on the Faraday connection
     def initialize(options={})
@@ -119,6 +123,7 @@ module Google
       @discovered_apis = {}
       ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
       self.connection = Faraday.new do |faraday|
+        faraday.response :charset if options[:force_encoding]
         faraday.response :gzip
         faraday.options.params_encoder = Faraday::FlatParamsEncoder
         faraday.ssl.ca_file = ca_file
@@ -265,10 +270,12 @@ module Google
     # @param [String, Symbol] api The API name.
     # @param [String] version The desired version of the API.
     # @param [Addressable::URI] uri The URI of the discovery document.
+    # @return [Google::APIClient::API] The service object.
     def register_discovery_uri(api, version, uri)
       api = api.to_s
       version = version || 'v1'
       @discovery_uris["#{api}:#{version}"] = uri
+      discovered_api(api, version)
     end
 
     ##
@@ -297,6 +304,7 @@ module Google
     # @param [String] version The desired version of the API.
     # @param [String, StringIO] discovery_document
     #   The contents of the discovery document.
+    # @return [Google::APIClient::API] The service object.
     def register_discovery_document(api, version, discovery_document)
       api = api.to_s
       version = version || 'v1'
@@ -311,6 +319,7 @@ module Google
       end
       @discovery_documents["#{api}:#{version}"] =
         MultiJson.load(discovery_document)
+      discovered_api(api, version)
     end
 
     ##
index 1082516ade032ebf9f73b7c7ec1ddb30755e5093..f7b1e096bad79b0065f41c0bc426824709e843dd 100644 (file)
@@ -139,14 +139,17 @@ module Google
       #   the HTTP response.
       def process_http_response(response)
         content_type = find_header('Content-Type', response.headers)
-        boundary = /.*boundary=(.+)/.match(content_type)[1]
-        parts = response.body.split(/--#{Regexp.escape(boundary)}/)
-        parts = parts[1...-1]
-        parts.each do |part|
-          call_response = deserialize_call_response(part)
-          _, call, callback = @calls.assoc(call_response.call_id)
-          result = Google::APIClient::Result.new(call, call_response)
-          callback.call(result) if callback
+        m = /.*boundary=(.+)/.match(content_type)
+        if m
+          boundary = m[1]
+          parts = response.body.split(/--#{Regexp.escape(boundary)}/)
+          parts = parts[1...-1]
+          parts.each do |part|
+            call_response = deserialize_call_response(part)
+            _, call, callback = @calls.assoc(call_response.call_id)
+            result = Google::APIClient::Result.new(call, call_response)
+            callback.call(result) if callback
+          end
         end
         Google::APIClient::Result.new(self, response)
       end
diff --git a/lib/google/api_client/charset.rb b/lib/google/api_client/charset.rb
new file mode 100644 (file)
index 0000000..47b11ba
--- /dev/null
@@ -0,0 +1,33 @@
+require 'faraday'
+require 'zlib'
+module Google
+  class APIClient
+    class Charset < Faraday::Response::Middleware
+      include Google::APIClient::Logging
+
+      def charset_for_content_type(type)
+        if type
+          m = type.match(/(?:charset|encoding)="?([a-z0-9-]+)"?/i)
+          if m
+            return Encoding.find(m[1])
+          end
+        end
+        nil
+      end
+
+      def adjust_encoding(env)
+        charset = charset_for_content_type(env[:response_headers]['content-type'])
+        if charset && env[:body].encoding != charset
+          env[:body].force_encoding(charset)
+        end
+      end
+      
+      def on_complete(env)
+        adjust_encoding(env)
+      end
+    end
+  end
+end
+Faraday::Response.register_middleware :charset => Google::APIClient::Charset
\ No newline at end of file
index 792f1b7cf2f507b0a5fef1579f5ee7a00a09d39c..a9cc2413896cd5edff4dccc783f80a7fb77b9d8b 100644 (file)
@@ -88,12 +88,12 @@ module Google
         @client_id = fdata[:client_id] || fdata["client_id"]
         @client_secret = fdata[:client_secret] || fdata["client_secret"]
         @redirect_uris = fdata[:redirect_uris] || fdata["redirect_uris"]
-        @redirect_uris ||= [fdata[:redirect_uri]]
+        @redirect_uris ||= [fdata[:redirect_uri] || fdata["redirect_uri"]].compact
         @javascript_origins = (
           fdata[:javascript_origins] ||
           fdata["javascript_origins"]
         )
-        @javascript_origins ||= [fdata[:javascript_origin]]
+        @javascript_origins ||= [fdata[:javascript_origin] || fdata["javascript_origin"]].compact
         @authorization_uri = fdata[:auth_uri] || fdata["auth_uri"]
         @authorization_uri ||= fdata[:authorization_uri]
         @token_credential_uri = fdata[:token_uri] || fdata["token_uri"]
@@ -120,7 +120,11 @@ module Google
       # @return [String]
       #   JSON
       def to_json
-        return MultiJson.dump({
+        return MultiJson.dump(to_hash)
+      end
+      
+      def to_hash
+        {
           self.flow => ({
             'client_id' => self.client_id,
             'client_secret' => self.client_secret,
@@ -141,7 +145,7 @@ module Google
             end
             accu
           end
-        })
+        }
       end
       
       def to_authorization
index 38d08594ec39da27a609c9647bfac822ed81c840..c48bec04a5d5d0c87807a7222e22f2e3b7650744 100644 (file)
@@ -182,8 +182,9 @@ module Google
       # Build a request for fetching the next page of data
       # 
       # @return [Google::APIClient::Request]
-      #   API request for retrieving next page
+      #   API request for retrieving next page, nil if no page token available
       def next_page
+        return nil unless self.next_page_token
         merged_parameters = Hash[self.reference.parameters].merge({
           self.page_token_param => self.next_page_token
         })
@@ -215,8 +216,9 @@ module Google
       # Build a request for fetching the previous page of data
       # 
       # @return [Google::APIClient::Request]
-      #   API request for retrieving previous page
+      #   API request for retrieving previous page, nil if no page token available
       def prev_page
+        return nil unless self.prev_page_token
         merged_parameters = Hash[self.reference.parameters].merge({
           self.page_token_param => self.prev_page_token
         })
index 685c61780934f5a75db106c24bf5c0736b94e2e2..28f2605d92ad13892bf514ab915958440ef378b0 100755 (executable)
@@ -225,7 +225,7 @@ module Google
           result = @client.execute(params)
           return Google::APIClient::Service::Result.new(request, result)
         elsif request.instance_of? Google::APIClient::Service::BatchRequest
-          @client.execute(request.base_batch)
+          @client.execute(request.base_batch, {:connection => @connection})
         end
       end
     end
diff --git a/spec/fixtures/files/client_secrets.json b/spec/fixtures/files/client_secrets.json
new file mode 100644 (file)
index 0000000..05fa7cb
--- /dev/null
@@ -0,0 +1 @@
+{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"i8YaXdGgiQ4_KrTVNGsB7QP1","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"","client_x509_cert_url":"","client_id":"898243283568.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}
\ No newline at end of file
diff --git a/spec/fixtures/files/zoo.json b/spec/fixtures/files/zoo.json
new file mode 100644 (file)
index 0000000..4abd957
--- /dev/null
@@ -0,0 +1,584 @@
+{
+ "kind": "discovery#describeItem",
+ "name": "zoo",
+ "version": "v1",
+ "description": "Zoo API used for testing",
+ "basePath": "/zoo/",
+ "rootUrl": "https://www.googleapis.com/",
+ "servicePath": "zoo/v1/",
+ "rpcPath": "/rpc",
+ "parameters": {
+  "alt": {
+   "type": "string",
+   "description": "Data format for the response.",
+   "default": "json",
+   "enum": [
+    "json"
+   ],
+   "enumDescriptions": [
+    "Responses with Content-Type of application/json"
+   ],
+   "location": "query"
+  },
+  "fields": {
+   "type": "string",
+   "description": "Selector specifying which fields to include in a partial response.",
+   "location": "query"
+  },
+  "key": {
+   "type": "string",
+   "description": "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
+   "location": "query"
+  },
+  "oauth_token": {
+   "type": "string",
+   "description": "OAuth 2.0 token for the current user.",
+   "location": "query"
+  },
+  "prettyPrint": {
+   "type": "boolean",
+   "description": "Returns response with indentations and line breaks.",
+   "default": "true",
+   "location": "query"
+  },
+  "quotaUser": {
+   "type": "string",
+   "description": "Available to use for quota purposes for server-side applications. Can be any arbitrary string assigned to a user, but should not exceed 40 characters. Overrides userIp if both are provided.",
+   "location": "query"
+  },
+  "userIp": {
+   "type": "string",
+   "description": "IP address of the site where the request originates. Use this if you want to enforce per-user limits.",
+   "location": "query"
+  }
+ },
+ "features": [
+  "dataWrapper"
+ ],
+ "schemas": {
+  "Animal": {
+   "id": "Animal",
+   "type": "object",
+   "properties": {
+    "etag": {
+     "type": "string"
+    },
+    "kind": {
+     "type": "string",
+     "default": "zoo#animal"
+    },
+    "name": {
+     "type": "string"
+    },
+    "photo": {
+     "type": "object",
+     "properties": {
+      "filename": {
+       "type": "string"
+      },
+      "hash": {
+       "type": "string"
+      },
+      "hashAlgorithm": {
+       "type": "string"
+      },
+      "size": {
+       "type": "integer"
+      },
+      "type": {
+       "type": "string"
+      }
+     }
+    }
+   }
+  },
+  "Animal2": {
+   "id": "Animal2",
+   "type": "object",
+   "properties": {
+    "kind": {
+     "type": "string",
+     "default": "zoo#animal"
+    },
+    "name": {
+     "type": "string"
+    }
+   }
+  },
+  "AnimalFeed": {
+   "id": "AnimalFeed",
+   "type": "object",
+   "properties": {
+    "etag": {
+     "type": "string"
+    },
+    "items": {
+     "type": "array",
+     "items": {
+      "$ref": "Animal"
+     }
+    },
+    "kind": {
+     "type": "string",
+     "default": "zoo#animalFeed"
+    }
+   }
+  },
+  "AnimalMap": {
+   "id": "AnimalMap",
+   "type": "object",
+   "properties": {
+    "etag": {
+     "type": "string"
+    },
+    "animals": {
+     "type": "object",
+     "description": "Map of animal id to animal data",
+     "additionalProperties": {
+      "$ref": "Animal"
+     }
+    },
+    "kind": {
+     "type": "string",
+     "default": "zoo#animalMap"
+    }
+   }
+  },
+  "LoadFeed": {
+   "id": "LoadFeed",
+   "type": "object",
+   "properties": {
+    "items": {
+     "type": "array",
+     "items": {
+      "type": "object",
+      "properties": {
+       "doubleVal": {
+        "type": "number"
+       },
+       "nullVal": {
+        "type": "null"
+       },
+       "booleanVal": {
+        "type": "boolean",
+        "description": "True or False."
+       },
+       "anyVal": {
+        "type": "any",
+        "description": "Anything will do."
+       },
+       "enumVal": {
+        "type": "string"
+       },
+       "kind": {
+        "type": "string",
+        "default": "zoo#loadValue"
+       },
+       "longVal": {
+        "type": "integer"
+       },
+       "stringVal": {
+        "type": "string"
+       }
+      }
+     }
+    },
+    "kind": {
+     "type": "string",
+     "default": "zoo#loadFeed"
+    }
+   }
+  }
+ },
+ "methods": {
+  "query": {
+   "path": "query",
+   "id": "bigquery.query",
+   "httpMethod": "GET",
+   "parameters": {
+    "q": {
+     "type": "string",
+     "location": "query",
+     "required": false,
+     "repeated": false
+    },
+    "i": {
+     "type": "integer",
+     "location": "query",
+     "required": false,
+     "repeated": false,
+     "minimum": "0",
+     "maximum": "4294967295",
+     "default": "20"
+    },
+    "n": {
+     "type": "number",
+     "location": "query",
+     "required": false,
+     "repeated": false
+    },
+    "b": {
+     "type": "boolean",
+     "location": "query",
+     "required": false,
+     "repeated": false
+    },
+    "a": {
+     "type": "any",
+     "location": "query",
+     "required": false,
+     "repeated": false
+    },
+    "o": {
+     "type": "object",
+     "location": "query",
+     "required": false,
+     "repeated": false
+    },
+    "e": {
+     "type": "string",
+     "location": "query",
+     "required": false,
+     "repeated": false,
+     "enum": [
+       "foo",
+       "bar"
+     ]
+    },
+    "er": {
+      "type": "string",
+      "location": "query",
+      "required": false,
+      "repeated": true,
+      "enum": [
+        "one",
+        "two",
+        "three"
+      ]
+    },
+    "rr": {
+     "type": "string",
+     "location": "query",
+     "required": false,
+     "repeated": true,
+     "pattern": "[a-z]+"
+    }
+   }
+  }
+ },
+ "resources": {
+  "my": {
+   "resources": {
+    "favorites": {
+     "methods": {
+      "list": {
+       "path": "favorites/@me/mine",
+       "id": "zoo.animals.mine",
+       "httpMethod": "GET",
+       "parameters": {
+        "max-results": {
+          "location": "query",
+          "required": false
+        }
+       }
+      }
+     }
+    }
+   }
+  },
+  "global": {
+   "resources": {
+    "print": {
+     "methods": {
+      "assert": {
+       "path": "global/print/assert",
+       "id": "zoo.animals.mine",
+       "httpMethod": "GET",
+       "parameters": {
+        "max-results": {
+          "location": "query",
+          "required": false
+        }
+       }
+      }
+     }
+    }
+   }
+  },
+  "animals": {
+   "methods": {
+    "crossbreed": {
+     "path": "animals/crossbreed",
+     "id": "zoo.animals.crossbreed",
+     "httpMethod": "POST",
+     "description": "Cross-breed animals",
+     "response": {
+      "$ref": "Animal2"
+     },
+     "mediaUpload": {
+      "accept": [
+       "image/png"
+      ],
+      "protocols": {
+       "simple": {
+        "multipart": true,
+        "path": "upload/activities/{userId}/@self"
+       },
+       "resumable": {
+        "multipart": true,
+        "path": "upload/activities/{userId}/@self"
+       }
+      }
+     }
+    },
+    "delete": {
+     "path": "animals/{name}",
+     "id": "zoo.animals.delete",
+     "httpMethod": "DELETE",
+     "description": "Delete animals",
+     "parameters": {
+      "name": {
+       "location": "path",
+       "required": true,
+       "description": "Name of the animal to delete",
+       "type": "string"
+      }
+     },
+     "parameterOrder": [
+      "name"
+     ]
+    },
+    "get": {
+     "path": "animals/{name}",
+     "id": "zoo.animals.get",
+     "httpMethod": "GET",
+     "description": "Get animals",
+     "supportsMediaDownload": true,
+     "parameters": {
+      "name": {
+       "location": "path",
+       "required": true,
+       "description": "Name of the animal to load",
+       "type": "string"
+      },
+      "projection": {
+       "location": "query",
+       "type": "string",
+       "enum": [
+        "full"
+       ],
+       "enumDescriptions": [
+        "Include everything"
+       ]
+      }
+     },
+     "parameterOrder": [
+      "name"
+     ],
+     "response": {
+      "$ref": "Animal"
+     }
+    },
+    "getmedia": {
+     "path": "animals/{name}",
+     "id": "zoo.animals.get",
+     "httpMethod": "GET",
+     "description": "Get animals",
+     "parameters": {
+      "name": {
+       "location": "path",
+       "required": true,
+       "description": "Name of the animal to load",
+       "type": "string"
+      },
+      "projection": {
+       "location": "query",
+       "type": "string",
+       "enum": [
+        "full"
+       ],
+       "enumDescriptions": [
+        "Include everything"
+       ]
+      }
+     },
+     "parameterOrder": [
+      "name"
+     ]
+    },
+    "insert": {
+     "path": "animals",
+     "id": "zoo.animals.insert",
+     "httpMethod": "POST",
+     "description": "Insert animals",
+     "request": {
+      "$ref": "Animal"
+     },
+     "response": {
+      "$ref": "Animal"
+     },
+     "mediaUpload": {
+      "accept": [
+       "image/png"
+      ],
+      "maxSize": "1KB",
+      "protocols": {
+       "simple": {
+        "multipart": true,
+        "path": "upload/activities/{userId}/@self"
+       },
+       "resumable": {
+        "multipart": true,
+        "path": "upload/activities/{userId}/@self"
+       }
+      }
+     }
+    },
+    "list": {
+     "path": "animals",
+     "id": "zoo.animals.list",
+     "httpMethod": "GET",
+     "description": "List animals",
+     "parameters": {
+      "max-results": {
+       "location": "query",
+       "description": "Maximum number of results to return",
+       "type": "integer",
+       "minimum": "0"
+      },
+      "name": {
+       "location": "query",
+       "description": "Restrict result to animals with this name",
+       "type": "string"
+      },
+      "projection": {
+       "location": "query",
+       "type": "string",
+       "enum": [
+        "full"
+       ],
+       "enumDescriptions": [
+        "Include absolutely everything"
+       ]
+      },
+      "start-token": {
+       "location": "query",
+       "description": "Pagination token",
+       "type": "string"
+      }
+     },
+     "response": {
+      "$ref": "AnimalFeed"
+     }
+    },
+    "patch": {
+     "path": "animals/{name}",
+     "id": "zoo.animals.patch",
+     "httpMethod": "PATCH",
+     "description": "Update animals",
+     "parameters": {
+      "name": {
+       "location": "path",
+       "required": true,
+       "description": "Name of the animal to update",
+       "type": "string"
+      }
+     },
+     "parameterOrder": [
+      "name"
+     ],
+     "request": {
+      "$ref": "Animal"
+     },
+     "response": {
+      "$ref": "Animal"
+     }
+    },
+    "update": {
+     "path": "animals/{name}",
+     "id": "zoo.animals.update",
+     "httpMethod": "PUT",
+     "description": "Update animals",
+     "parameters": {
+      "name": {
+       "location": "path",
+       "description": "Name of the animal to update",
+       "type": "string"
+      }
+     },
+     "parameterOrder": [
+      "name"
+     ],
+     "request": {
+      "$ref": "Animal"
+     },
+     "response": {
+      "$ref": "Animal"
+     }
+    }
+   }
+  },
+  "load": {
+   "methods": {
+    "list": {
+     "path": "load",
+     "id": "zoo.load.list",
+     "httpMethod": "GET",
+     "response": {
+      "$ref": "LoadFeed"
+     }
+    }
+   }
+  },
+  "loadNoTemplate": {
+   "methods": {
+    "list": {
+     "path": "loadNoTemplate",
+     "id": "zoo.loadNoTemplate.list",
+     "httpMethod": "GET"
+    }
+   }
+  },
+  "scopedAnimals": {
+   "methods": {
+    "list": {
+     "path": "scopedanimals",
+     "id": "zoo.scopedAnimals.list",
+     "httpMethod": "GET",
+     "description": "List animals (scoped)",
+     "parameters": {
+      "max-results": {
+       "location": "query",
+       "description": "Maximum number of results to return",
+       "type": "integer",
+       "minimum": "0"
+      },
+      "name": {
+       "location": "query",
+       "description": "Restrict result to animals with this name",
+       "type": "string"
+      },
+      "projection": {
+       "location": "query",
+       "type": "string",
+       "enum": [
+        "full"
+       ],
+       "enumDescriptions": [
+        "Include absolutely everything"
+       ]
+      },
+      "start-token": {
+       "location": "query",
+       "description": "Pagination token",
+       "type": "string"
+      }
+     },
+     "response": {
+      "$ref": "AnimalFeed"
+     }
+    }
+   }
+  }
+ }
+}
\ No newline at end of file
diff --git a/spec/google/api_client/client_secrets_spec.rb b/spec/google/api_client/client_secrets_spec.rb
new file mode 100644 (file)
index 0000000..ead9bf7
--- /dev/null
@@ -0,0 +1,53 @@
+# encoding:utf-8
+
+# Copyright 2013 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'spec_helper'
+
+require 'google/api_client/client_secrets'
+
+FIXTURES_PATH = File.expand_path('../../../fixtures', __FILE__)
+
+RSpec.describe Google::APIClient::ClientSecrets do
+  
+  context 'with JSON file' do
+    let(:file) { File.join(FIXTURES_PATH, 'files', 'client_secrets.json') }
+    subject(:secrets) { Google::APIClient::ClientSecrets.load(file)}
+  
+    it 'should load the correct client ID' do
+      expect(secrets.client_id).to be == '898243283568.apps.googleusercontent.com'
+    end
+
+    it 'should load the correct client secret' do
+      expect(secrets.client_secret).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
+    end
+    
+    context 'serialzed to hash' do
+      subject(:hash) { secrets.to_hash }
+      it 'should contain the flow as the first key' do
+        expect(hash).to have_key "installed"
+      end
+
+      it 'should contain the client ID' do
+        expect(hash["installed"]["client_id"]).to be == '898243283568.apps.googleusercontent.com'
+      end
+
+      it 'should contain the client secret' do
+        expect(hash["installed"]["client_secret"]).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
+      end
+
+    end
+  end
+end
\ No newline at end of file
index d1c23759ff523698bfad7f901ed8c55368e1f05d..a2eb4e5f85966666ac55b9d2a88c0257d4076f9c 100644 (file)
@@ -23,6 +23,8 @@ require 'compat/multi_json'
 require 'signet/oauth_1/client'
 require 'google/api_client'
 
+fixtures_path = File.expand_path('../../../fixtures', __FILE__)
+
 RSpec.describe Google::APIClient do
   include ConnectionHelpers
   CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
@@ -70,6 +72,15 @@ RSpec.describe Google::APIClient do
     expect(CLIENT.preferred_version('bogus')).to eq(nil)
   end
 
+  describe 'with zoo API' do
+    it 'should return API instance registered from file' do
+      zoo_json = File.join(fixtures_path, 'files', 'zoo.json')
+      contents = File.open(zoo_json, 'rb') { |io| io.read }
+      api = CLIENT.register_discovery_document('zoo', 'v1', contents)
+      expect(api).to be_kind_of(Google::APIClient::API)
+    end
+  end
+  
   describe 'with the prediction API' do
     before do
       CLIENT.authorization = nil
index 9448b009f1e1ec2df15ae4c529f896187ecc4c82..0539b97d9395c0eb6a2de20876a7470279599ced 100644 (file)
@@ -1,3 +1,4 @@
+# Encoding: utf-8
 # Copyright 2012 Google Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +21,7 @@ RSpec.describe Google::APIClient::Gzip do
 
   def create_connection(&block)
     Faraday.new do |b|
+      b.response :charset
       b.response :gzip
       b.adapter :test do |stub|
         stub.get '/', &block
@@ -43,6 +45,17 @@ RSpec.describe Google::APIClient::Gzip do
     expect(result.body).to eq("Hello world\n")
   end
   
+  it 'should inflate with the correct charset encoding' do
+    conn = create_connection do |env|
+      [200, 
+        { 'Content-Encoding' => 'deflate', 'Content-Type' => 'application/json;charset=BIG5'}, 
+        Base64.decode64('eJxb8nLp7t2VAA8fBCI=')]
+    end
+    result = conn.get('/')
+    expect(result.body.encoding).to eq(Encoding::BIG5)
+    expect(result.body).to eq('日本語'.encode("BIG5"))
+  end
+
   describe 'with API Client' do
 
     before do
index e5184372c2cc63ab25b548139fe95d0678852162..a6f6925e58cf08865a7cc8686a96b7312a8db731 100644 (file)
@@ -493,6 +493,33 @@ RSpec.describe Google::APIClient::Service::Result do
 end
 
 RSpec.describe Google::APIClient::Service::BatchRequest do
+  
+  include ConnectionHelpers
+  
+  context 'with a service connection' do
+    before do
+      @conn = stub_connection do |stub|
+        stub.post('/batch') do |env|
+          [500, {'Content-Type' => 'application/json'}, '{}']
+        end
+      end
+      @discovery = Google::APIClient::Service.new('discovery', 'v1',
+          {:application_name => APPLICATION_NAME, :authorization => nil,
+           :cache_store => nil, :connection => @conn})
+      @calls = [
+        @discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
+        @discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
+      ]
+    end
+
+    it 'should use the service connection' do
+      batch = @discovery.batch(@calls) do
+      end
+      batch.execute
+      @conn.verify
+    end  
+  end
+  
   describe 'with the discovery API' do
     before do
       @discovery = Google::APIClient::Service.new('discovery', 'v1',
@@ -585,7 +612,7 @@ RSpec.describe Google::APIClient::Service::BatchRequest do
         batch.execute
         expect(call1_returned).to eq(true)
         expect(call2_returned).to eq(true)
-      end
+      end      
     end
   end
 end
\ No newline at end of file