blob: a6635a35235ea0d052c208bba0d89334e4a08ba0 [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
Joe Gregoriof863f7a2011-02-24 03:24:44 -05002# -*- coding: utf-8 -*-
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04003#
Craig Citro751b7fb2014-09-23 11:20:38 -07004# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04005#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040018
19"""Discovery document tests
20
21Unit tests for objects created from discovery documents.
22"""
INADA Naokid898a372015-03-04 03:52:46 +090023from __future__ import absolute_import
24import six
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040025
26__author__ = 'jcgregorio@google.com (Joe Gregorio)'
27
Pat Ferateed9affd2015-03-03 16:03:15 -080028from six import BytesIO, StringIO
Pat Ferated5b61bd2015-03-03 16:04:11 -080029from six.moves.urllib.parse import urlparse, parse_qs
Pat Ferateed9affd2015-03-03 16:03:15 -080030
Daniel Hermesc2113242013-02-27 10:16:13 -080031import copy
Joe Gregoriodc106fc2012-11-20 14:30:14 -050032import datetime
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040033import httplib2
Craig Citro7ee535d2015-02-23 10:11:14 -080034import itertools
Craig Citro6ae34d72014-08-18 23:10:09 -070035import json
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040036import os
Joe Gregoriodc106fc2012-11-20 14:30:14 -050037import pickle
Phil Ruffwind26178fc2015-10-13 19:00:33 -040038import re
Joe Gregorioc80ac9d2012-08-21 14:09:09 -040039import sys
Pat Ferate497a90f2015-03-09 09:52:54 -070040import unittest2 as unittest
Joe Gregoriodc106fc2012-11-20 14:30:14 -050041
Takashi Matsuo30125122015-08-19 11:42:32 -070042import mock
43
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080044import google.auth.credentials
45import google_auth_httplib2
John Asmuth864311d2014-04-24 15:46:08 -040046from googleapiclient.discovery import _fix_up_media_upload
47from googleapiclient.discovery import _fix_up_method_description
48from googleapiclient.discovery import _fix_up_parameters
Craig Citro7ee535d2015-02-23 10:11:14 -080049from googleapiclient.discovery import _urljoin
John Asmuth864311d2014-04-24 15:46:08 -040050from googleapiclient.discovery import build
51from googleapiclient.discovery import build_from_document
52from googleapiclient.discovery import DISCOVERY_URI
53from googleapiclient.discovery import key2param
54from googleapiclient.discovery import MEDIA_BODY_PARAMETER_DEFAULT_VALUE
Brian J. Watson38051ac2016-10-25 07:53:08 -070055from googleapiclient.discovery import MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE
John Asmuth864311d2014-04-24 15:46:08 -040056from googleapiclient.discovery import ResourceMethodParameters
57from googleapiclient.discovery import STACK_QUERY_PARAMETERS
58from googleapiclient.discovery import STACK_QUERY_PARAMETER_DEFAULT_VALUE
Takashi Matsuo30125122015-08-19 11:42:32 -070059from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
60from googleapiclient.discovery_cache.base import Cache
John Asmuth864311d2014-04-24 15:46:08 -040061from googleapiclient.errors import HttpError
62from googleapiclient.errors import InvalidJsonError
63from googleapiclient.errors import MediaUploadSizeError
64from googleapiclient.errors import ResumableUploadError
65from googleapiclient.errors import UnacceptableMimeTypeError
Takashi Matsuo3772f9d2015-09-04 12:25:55 -070066from googleapiclient.errors import UnknownApiNameOrVersion
Brian J. Watson38051ac2016-10-25 07:53:08 -070067from googleapiclient.errors import UnknownFileType
Igor Maravić22435292017-01-19 22:28:22 +010068from googleapiclient.http import build_http
Pepper Lebeck-Jobe860836f2015-06-12 20:42:23 -040069from googleapiclient.http import BatchHttpRequest
John Asmuth864311d2014-04-24 15:46:08 -040070from googleapiclient.http import HttpMock
71from googleapiclient.http import HttpMockSequence
72from googleapiclient.http import MediaFileUpload
73from googleapiclient.http import MediaIoBaseUpload
74from googleapiclient.http import MediaUpload
75from googleapiclient.http import MediaUploadProgress
76from googleapiclient.http import tunnel_patch
Thomas Coffee20af04d2017-02-10 15:24:44 -080077from googleapiclient.model import JsonModel
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +090078from googleapiclient.schema import Schemas
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -080079from oauth2client import GOOGLE_TOKEN_URI
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080080from oauth2client.client import OAuth2Credentials, GoogleCredentials
Joe Gregorio79daca02013-03-29 16:25:52 -040081
Helen Koikede13e3b2018-04-26 16:05:16 -030082from googleapiclient import _helpers as util
Jon Wayne Parrott36d4e1b2016-10-17 13:31:33 -070083
Joe Gregoriodc106fc2012-11-20 14:30:14 -050084import uritemplate
85
Joe Gregoriocb8103d2011-02-11 23:20:52 -050086
87DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
88
Joe Gregorioa98733f2011-09-16 10:12:28 -040089
Joe Gregoriof1ba7f12012-11-16 15:10:47 -050090def assertUrisEqual(testcase, expected, actual):
91 """Test that URIs are the same, up to reordering of query parameters."""
Pat Ferated5b61bd2015-03-03 16:04:11 -080092 expected = urlparse(expected)
93 actual = urlparse(actual)
Joe Gregoriof1ba7f12012-11-16 15:10:47 -050094 testcase.assertEqual(expected.scheme, actual.scheme)
95 testcase.assertEqual(expected.netloc, actual.netloc)
96 testcase.assertEqual(expected.path, actual.path)
97 testcase.assertEqual(expected.params, actual.params)
98 testcase.assertEqual(expected.fragment, actual.fragment)
99 expected_query = parse_qs(expected.query)
100 actual_query = parse_qs(actual.query)
INADA Naokid898a372015-03-04 03:52:46 +0900101 for name in list(expected_query.keys()):
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500102 testcase.assertEqual(expected_query[name], actual_query[name])
INADA Naokid898a372015-03-04 03:52:46 +0900103 for name in list(actual_query.keys()):
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500104 testcase.assertEqual(expected_query[name], actual_query[name])
105
106
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500107def datafile(filename):
108 return os.path.join(DATA_DIR, filename)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400109
110
Joe Gregorio504a17f2012-12-07 14:14:26 -0500111class SetupHttplib2(unittest.TestCase):
Daniel Hermesc2113242013-02-27 10:16:13 -0800112
Joe Gregorio504a17f2012-12-07 14:14:26 -0500113 def test_retries(self):
John Asmuth864311d2014-04-24 15:46:08 -0400114 # Merely loading googleapiclient.discovery should set the RETRIES to 1.
Joe Gregorio504a17f2012-12-07 14:14:26 -0500115 self.assertEqual(1, httplib2.RETRIES)
116
117
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400118class Utilities(unittest.TestCase):
Daniel Hermesc2113242013-02-27 10:16:13 -0800119
120 def setUp(self):
121 with open(datafile('zoo.json'), 'r') as fh:
Craig Citro6ae34d72014-08-18 23:10:09 -0700122 self.zoo_root_desc = json.loads(fh.read())
Daniel Hermesc2113242013-02-27 10:16:13 -0800123 self.zoo_get_method_desc = self.zoo_root_desc['methods']['query']
Daniel Hermes954e1242013-02-28 09:28:37 -0800124 self.zoo_animals_resource = self.zoo_root_desc['resources']['animals']
125 self.zoo_insert_method_desc = self.zoo_animals_resource['methods']['insert']
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900126 self.zoo_schema = Schemas(self.zoo_root_desc)
Daniel Hermesc2113242013-02-27 10:16:13 -0800127
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400128 def test_key2param(self):
129 self.assertEqual('max_results', key2param('max-results'))
130 self.assertEqual('x007_bond', key2param('007-bond'))
131
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900132 def _base_fix_up_parameters_test(
133 self, method_desc, http_method, root_desc, schema):
Daniel Hermesc2113242013-02-27 10:16:13 -0800134 self.assertEqual(method_desc['httpMethod'], http_method)
135
136 method_desc_copy = copy.deepcopy(method_desc)
137 self.assertEqual(method_desc, method_desc_copy)
138
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900139 parameters = _fix_up_parameters(method_desc_copy, root_desc, http_method,
140 schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800141
142 self.assertNotEqual(method_desc, method_desc_copy)
143
144 for param_name in STACK_QUERY_PARAMETERS:
145 self.assertEqual(STACK_QUERY_PARAMETER_DEFAULT_VALUE,
146 parameters[param_name])
147
INADA Naokid898a372015-03-04 03:52:46 +0900148 for param_name, value in six.iteritems(root_desc.get('parameters', {})):
Daniel Hermesc2113242013-02-27 10:16:13 -0800149 self.assertEqual(value, parameters[param_name])
150
151 return parameters
152
153 def test_fix_up_parameters_get(self):
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900154 parameters = self._base_fix_up_parameters_test(
155 self.zoo_get_method_desc, 'GET', self.zoo_root_desc, self.zoo_schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800156 # Since http_method is 'GET'
INADA Naoki0bceb332014-08-20 15:27:52 +0900157 self.assertFalse('body' in parameters)
Daniel Hermesc2113242013-02-27 10:16:13 -0800158
159 def test_fix_up_parameters_insert(self):
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900160 parameters = self._base_fix_up_parameters_test(
161 self.zoo_insert_method_desc, 'POST', self.zoo_root_desc, self.zoo_schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800162 body = {
163 'description': 'The request body.',
164 'type': 'object',
165 'required': True,
166 '$ref': 'Animal',
167 }
168 self.assertEqual(parameters['body'], body)
169
170 def test_fix_up_parameters_check_body(self):
171 dummy_root_desc = {}
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900172 dummy_schema = {
173 'Request': {
174 'properties': {
175 "description": "Required. Dummy parameter.",
176 "type": "string"
177 }
178 }
179 }
Daniel Hermesc2113242013-02-27 10:16:13 -0800180 no_payload_http_method = 'DELETE'
181 with_payload_http_method = 'PUT'
182
183 invalid_method_desc = {'response': 'Who cares'}
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900184 valid_method_desc = {
185 'request': {
186 'key1': 'value1',
187 'key2': 'value2',
188 '$ref': 'Request'
189 }
190 }
Daniel Hermesc2113242013-02-27 10:16:13 -0800191
192 parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900193 no_payload_http_method, dummy_schema)
INADA Naoki0bceb332014-08-20 15:27:52 +0900194 self.assertFalse('body' in parameters)
Daniel Hermesc2113242013-02-27 10:16:13 -0800195
196 parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900197 no_payload_http_method, dummy_schema)
INADA Naoki0bceb332014-08-20 15:27:52 +0900198 self.assertFalse('body' in parameters)
Daniel Hermesc2113242013-02-27 10:16:13 -0800199
200 parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900201 with_payload_http_method, dummy_schema)
INADA Naoki0bceb332014-08-20 15:27:52 +0900202 self.assertFalse('body' in parameters)
Daniel Hermesc2113242013-02-27 10:16:13 -0800203
204 parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900205 with_payload_http_method, dummy_schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800206 body = {
207 'description': 'The request body.',
208 'type': 'object',
209 'required': True,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900210 '$ref': 'Request',
Daniel Hermesc2113242013-02-27 10:16:13 -0800211 'key1': 'value1',
212 'key2': 'value2',
213 }
214 self.assertEqual(parameters['body'], body)
215
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900216 def test_fix_up_parameters_optional_body(self):
217 # Request with no parameters
218 dummy_schema = {'Request': {'properties': {}}}
219 method_desc = {'request': {'$ref': 'Request'}}
220
221 parameters = _fix_up_parameters(method_desc, {}, 'POST', dummy_schema)
222 self.assertFalse(parameters['body']['required'])
223
Daniel Hermesc2113242013-02-27 10:16:13 -0800224 def _base_fix_up_method_description_test(
225 self, method_desc, initial_parameters, final_parameters,
226 final_accept, final_max_size, final_media_path_url):
227 fake_root_desc = {'rootUrl': 'http://root/',
228 'servicePath': 'fake/'}
229 fake_path_url = 'fake-path/'
230
231 accept, max_size, media_path_url = _fix_up_media_upload(
232 method_desc, fake_root_desc, fake_path_url, initial_parameters)
233 self.assertEqual(accept, final_accept)
234 self.assertEqual(max_size, final_max_size)
235 self.assertEqual(media_path_url, final_media_path_url)
236 self.assertEqual(initial_parameters, final_parameters)
237
238 def test_fix_up_media_upload_no_initial_invalid(self):
239 invalid_method_desc = {'response': 'Who cares'}
240 self._base_fix_up_method_description_test(invalid_method_desc, {}, {},
241 [], 0, None)
242
243 def test_fix_up_media_upload_no_initial_valid_minimal(self):
244 valid_method_desc = {'mediaUpload': {'accept': []}}
Brian J. Watson38051ac2016-10-25 07:53:08 -0700245 final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
246 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE}
Daniel Hermesc2113242013-02-27 10:16:13 -0800247 self._base_fix_up_method_description_test(
248 valid_method_desc, {}, final_parameters, [], 0,
249 'http://root/upload/fake/fake-path/')
250
251 def test_fix_up_media_upload_no_initial_valid_full(self):
252 valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}}
Brian J. Watson38051ac2016-10-25 07:53:08 -0700253 final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
254 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE}
Daniel Hermesc2113242013-02-27 10:16:13 -0800255 ten_gb = 10 * 2**30
256 self._base_fix_up_method_description_test(
257 valid_method_desc, {}, final_parameters, ['*/*'],
258 ten_gb, 'http://root/upload/fake/fake-path/')
259
260 def test_fix_up_media_upload_with_initial_invalid(self):
261 invalid_method_desc = {'response': 'Who cares'}
262 initial_parameters = {'body': {}}
263 self._base_fix_up_method_description_test(
264 invalid_method_desc, initial_parameters,
265 initial_parameters, [], 0, None)
266
267 def test_fix_up_media_upload_with_initial_valid_minimal(self):
268 valid_method_desc = {'mediaUpload': {'accept': []}}
269 initial_parameters = {'body': {}}
270 final_parameters = {'body': {'required': False},
Brian J. Watson38051ac2016-10-25 07:53:08 -0700271 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
272 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE}
Daniel Hermesc2113242013-02-27 10:16:13 -0800273 self._base_fix_up_method_description_test(
274 valid_method_desc, initial_parameters, final_parameters, [], 0,
275 'http://root/upload/fake/fake-path/')
276
277 def test_fix_up_media_upload_with_initial_valid_full(self):
278 valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}}
279 initial_parameters = {'body': {}}
280 final_parameters = {'body': {'required': False},
Brian J. Watson38051ac2016-10-25 07:53:08 -0700281 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
282 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE}
Daniel Hermesc2113242013-02-27 10:16:13 -0800283 ten_gb = 10 * 2**30
284 self._base_fix_up_method_description_test(
285 valid_method_desc, initial_parameters, final_parameters, ['*/*'],
286 ten_gb, 'http://root/upload/fake/fake-path/')
287
288 def test_fix_up_method_description_get(self):
289 result = _fix_up_method_description(self.zoo_get_method_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900290 self.zoo_root_desc, self.zoo_schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800291 path_url = 'query'
292 http_method = 'GET'
293 method_id = 'bigquery.query'
294 accept = []
INADA Naoki0bceb332014-08-20 15:27:52 +0900295 max_size = 0
Daniel Hermesc2113242013-02-27 10:16:13 -0800296 media_path_url = None
297 self.assertEqual(result, (path_url, http_method, method_id, accept,
298 max_size, media_path_url))
299
300 def test_fix_up_method_description_insert(self):
301 result = _fix_up_method_description(self.zoo_insert_method_desc,
Jean-Loup Roussel-Clouet0c0c8972018-04-28 05:42:43 +0900302 self.zoo_root_desc, self.zoo_schema)
Daniel Hermesc2113242013-02-27 10:16:13 -0800303 path_url = 'animals'
304 http_method = 'POST'
305 method_id = 'zoo.animals.insert'
306 accept = ['image/png']
INADA Naoki0bceb332014-08-20 15:27:52 +0900307 max_size = 1024
Daniel Hermesc2113242013-02-27 10:16:13 -0800308 media_path_url = 'https://www.googleapis.com/upload/zoo/v1/animals'
309 self.assertEqual(result, (path_url, http_method, method_id, accept,
310 max_size, media_path_url))
311
Craig Citro7ee535d2015-02-23 10:11:14 -0800312 def test_urljoin(self):
313 # We want to exhaustively test various URL combinations.
314 simple_bases = ['https://www.googleapis.com', 'https://www.googleapis.com/']
315 long_urls = ['foo/v1/bar:custom?alt=json', '/foo/v1/bar:custom?alt=json']
316
317 long_bases = [
318 'https://www.googleapis.com/foo/v1',
319 'https://www.googleapis.com/foo/v1/',
320 ]
321 simple_urls = ['bar:custom?alt=json', '/bar:custom?alt=json']
322
323 final_url = 'https://www.googleapis.com/foo/v1/bar:custom?alt=json'
324 for base, url in itertools.product(simple_bases, long_urls):
325 self.assertEqual(final_url, _urljoin(base, url))
326 for base, url in itertools.product(long_bases, simple_urls):
327 self.assertEqual(final_url, _urljoin(base, url))
328
329
Daniel Hermes954e1242013-02-28 09:28:37 -0800330 def test_ResourceMethodParameters_zoo_get(self):
331 parameters = ResourceMethodParameters(self.zoo_get_method_desc)
332
333 param_types = {'a': 'any',
334 'b': 'boolean',
335 'e': 'string',
336 'er': 'string',
337 'i': 'integer',
338 'n': 'number',
339 'o': 'object',
340 'q': 'string',
341 'rr': 'string'}
INADA Naokid898a372015-03-04 03:52:46 +0900342 keys = list(param_types.keys())
Daniel Hermes954e1242013-02-28 09:28:37 -0800343 self.assertEqual(parameters.argmap, dict((key, key) for key in keys))
344 self.assertEqual(parameters.required_params, [])
345 self.assertEqual(sorted(parameters.repeated_params), ['er', 'rr'])
346 self.assertEqual(parameters.pattern_params, {'rr': '[a-z]+'})
347 self.assertEqual(sorted(parameters.query_params),
348 ['a', 'b', 'e', 'er', 'i', 'n', 'o', 'q', 'rr'])
349 self.assertEqual(parameters.path_params, set())
350 self.assertEqual(parameters.param_types, param_types)
351 enum_params = {'e': ['foo', 'bar'],
352 'er': ['one', 'two', 'three']}
353 self.assertEqual(parameters.enum_params, enum_params)
354
355 def test_ResourceMethodParameters_zoo_animals_patch(self):
356 method_desc = self.zoo_animals_resource['methods']['patch']
357 parameters = ResourceMethodParameters(method_desc)
358
359 param_types = {'name': 'string'}
INADA Naokid898a372015-03-04 03:52:46 +0900360 keys = list(param_types.keys())
Daniel Hermes954e1242013-02-28 09:28:37 -0800361 self.assertEqual(parameters.argmap, dict((key, key) for key in keys))
362 self.assertEqual(parameters.required_params, ['name'])
363 self.assertEqual(parameters.repeated_params, [])
364 self.assertEqual(parameters.pattern_params, {})
365 self.assertEqual(parameters.query_params, [])
366 self.assertEqual(parameters.path_params, set(['name']))
367 self.assertEqual(parameters.param_types, param_types)
368 self.assertEqual(parameters.enum_params, {})
369
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400370
Joe Gregorioc0e0fe92011-03-04 16:16:55 -0500371class DiscoveryErrors(unittest.TestCase):
372
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400373 def test_tests_should_be_run_with_strict_positional_enforcement(self):
374 try:
375 plus = build('plus', 'v1', None)
376 self.fail("should have raised a TypeError exception over missing http=.")
377 except TypeError:
378 pass
379
Joe Gregorioc0e0fe92011-03-04 16:16:55 -0500380 def test_failed_to_parse_discovery_json(self):
381 self.http = HttpMock(datafile('malformed.json'), {'status': '200'})
382 try:
Takashi Matsuo30125122015-08-19 11:42:32 -0700383 plus = build('plus', 'v1', http=self.http, cache_discovery=False)
Joe Gregorioc0e0fe92011-03-04 16:16:55 -0500384 self.fail("should have raised an exception over malformed JSON.")
Joe Gregorio49396552011-03-08 10:39:00 -0500385 except InvalidJsonError:
386 pass
Joe Gregorioc0e0fe92011-03-04 16:16:55 -0500387
Takashi Matsuo3772f9d2015-09-04 12:25:55 -0700388 def test_unknown_api_name_or_version(self):
389 http = HttpMockSequence([
390 ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()),
Ethan Bao12b7cd32016-03-14 14:25:10 -0700391 ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()),
Takashi Matsuo3772f9d2015-09-04 12:25:55 -0700392 ])
393 with self.assertRaises(UnknownApiNameOrVersion):
394 plus = build('plus', 'v1', http=http, cache_discovery=False)
395
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800396 def test_credentials_and_http_mutually_exclusive(self):
397 http = HttpMock(datafile('plus.json'), {'status': '200'})
398 with self.assertRaises(ValueError):
399 build(
400 'plus', 'v1', http=http, credentials=mock.sentinel.credentials)
401
Joe Gregorioc0e0fe92011-03-04 16:16:55 -0500402
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100403class DiscoveryFromDocument(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800404 MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials)
Joe Gregorioa98733f2011-09-16 10:12:28 -0400405
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100406 def test_can_build_from_local_document(self):
Joe Gregorio79daca02013-03-29 16:25:52 -0400407 discovery = open(datafile('plus.json')).read()
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800408 plus = build_from_document(
409 discovery, base="https://www.googleapis.com/",
410 credentials=self.MOCK_CREDENTIALS)
Joe Gregorio7b70f432011-11-09 10:18:51 -0500411 self.assertTrue(plus is not None)
Joe Gregorio4772f3d2012-12-10 10:22:37 -0500412 self.assertTrue(hasattr(plus, 'activities'))
413
414 def test_can_build_from_local_deserialized_document(self):
Joe Gregorio79daca02013-03-29 16:25:52 -0400415 discovery = open(datafile('plus.json')).read()
Craig Citro6ae34d72014-08-18 23:10:09 -0700416 discovery = json.loads(discovery)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800417 plus = build_from_document(
418 discovery, base="https://www.googleapis.com/",
419 credentials=self.MOCK_CREDENTIALS)
Joe Gregorio4772f3d2012-12-10 10:22:37 -0500420 self.assertTrue(plus is not None)
421 self.assertTrue(hasattr(plus, 'activities'))
Joe Gregorioa98733f2011-09-16 10:12:28 -0400422
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100423 def test_building_with_base_remembers_base(self):
Joe Gregorio79daca02013-03-29 16:25:52 -0400424 discovery = open(datafile('plus.json')).read()
Joe Gregorioa98733f2011-09-16 10:12:28 -0400425
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100426 base = "https://www.example.com/"
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800427 plus = build_from_document(
428 discovery, base=base, credentials=self.MOCK_CREDENTIALS)
Joe Gregorioa2838152012-07-16 11:52:17 -0400429 self.assertEquals("https://www.googleapis.com/plus/v1/", plus._baseUrl)
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100430
Igor Maravić22435292017-01-19 22:28:22 +0100431 def test_building_with_optional_http_with_authorization(self):
Jonathan Wayne Parrotta6e6fbd2015-07-16 15:33:57 -0700432 discovery = open(datafile('plus.json')).read()
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800433 plus = build_from_document(
434 discovery, base="https://www.googleapis.com/",
435 credentials=self.MOCK_CREDENTIALS)
Igor Maravić22435292017-01-19 22:28:22 +0100436
437 # plus service requires Authorization, hence we expect to see AuthorizedHttp object here
438 self.assertIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp)
439 self.assertIsInstance(plus._http.http, httplib2.Http)
440 self.assertIsInstance(plus._http.http.timeout, int)
441 self.assertGreater(plus._http.http.timeout, 0)
442
443 def test_building_with_optional_http_with_no_authorization(self):
444 discovery = open(datafile('plus.json')).read()
445 # Cleanup auth field, so we would use plain http client
446 discovery = json.loads(discovery)
447 discovery['auth'] = {}
448 discovery = json.dumps(discovery)
449
450 plus = build_from_document(
451 discovery, base="https://www.googleapis.com/",
Arunpn9d779cc2018-11-30 10:25:01 -0800452 credentials=None)
Igor Maravić22435292017-01-19 22:28:22 +0100453 # plus service requires Authorization
454 self.assertIsInstance(plus._http, httplib2.Http)
455 self.assertIsInstance(plus._http.timeout, int)
456 self.assertGreater(plus._http.timeout, 0)
Jonathan Wayne Parrotta6e6fbd2015-07-16 15:33:57 -0700457
458 def test_building_with_explicit_http(self):
459 http = HttpMock()
460 discovery = open(datafile('plus.json')).read()
461 plus = build_from_document(
462 discovery, base="https://www.googleapis.com/", http=http)
463 self.assertEquals(plus._http, http)
ade@google.com6a8c1cb2011-09-06 17:40:00 +0100464
Jon Wayne Parrott068eb352017-02-08 10:13:06 -0800465 def test_building_with_developer_key_skips_adc(self):
466 discovery = open(datafile('plus.json')).read()
467 plus = build_from_document(
468 discovery, base="https://www.googleapis.com/", developerKey='123')
469 self.assertIsInstance(plus._http, httplib2.Http)
470 # It should not be an AuthorizedHttp, because that would indicate that
471 # application default credentials were used.
472 self.assertNotIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp)
473
474
Joe Gregorioa98733f2011-09-16 10:12:28 -0400475class DiscoveryFromHttp(unittest.TestCase):
Joe Gregorio583d9e42011-09-16 15:54:15 -0400476 def setUp(self):
Joe Bedafb463cb2011-09-19 17:39:49 -0700477 self.old_environ = os.environ.copy()
Joe Gregorioa98733f2011-09-16 10:12:28 -0400478
Joe Gregorio583d9e42011-09-16 15:54:15 -0400479 def tearDown(self):
480 os.environ = self.old_environ
481
482 def test_userip_is_added_to_discovery_uri(self):
Joe Gregorioa98733f2011-09-16 10:12:28 -0400483 # build() will raise an HttpError on a 400, use this to pick the request uri
484 # out of the raised exception.
Joe Gregorio583d9e42011-09-16 15:54:15 -0400485 os.environ['REMOTE_ADDR'] = '10.0.0.1'
Joe Gregorioa98733f2011-09-16 10:12:28 -0400486 try:
487 http = HttpMockSequence([
Joe Gregorio79daca02013-03-29 16:25:52 -0400488 ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
Joe Gregorioa98733f2011-09-16 10:12:28 -0400489 ])
Arunpn9d779cc2018-11-30 10:25:01 -0800490 zoo = build('zoo', 'v1', http=http, developerKey=None,
Joe Gregorioa98733f2011-09-16 10:12:28 -0400491 discoveryServiceUrl='http://example.com')
492 self.fail('Should have raised an exception.')
INADA Naokic1505df2014-08-20 15:19:53 +0900493 except HttpError as e:
Joe Gregorio583d9e42011-09-16 15:54:15 -0400494 self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1')
Joe Gregorioa98733f2011-09-16 10:12:28 -0400495
Joe Gregorio583d9e42011-09-16 15:54:15 -0400496 def test_userip_missing_is_not_added_to_discovery_uri(self):
Joe Gregorioa98733f2011-09-16 10:12:28 -0400497 # build() will raise an HttpError on a 400, use this to pick the request uri
498 # out of the raised exception.
499 try:
500 http = HttpMockSequence([
Joe Gregorio79daca02013-03-29 16:25:52 -0400501 ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
Joe Gregorioa98733f2011-09-16 10:12:28 -0400502 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400503 zoo = build('zoo', 'v1', http=http, developerKey=None,
Joe Gregorioa98733f2011-09-16 10:12:28 -0400504 discoveryServiceUrl='http://example.com')
505 self.fail('Should have raised an exception.')
INADA Naokic1505df2014-08-20 15:19:53 +0900506 except HttpError as e:
Joe Gregorioa98733f2011-09-16 10:12:28 -0400507 self.assertEqual(e.uri, 'http://example.com')
508
Arunpn9d779cc2018-11-30 10:25:01 -0800509 def test_key_is_added_to_discovery_uri(self):
510 # build() will raise an HttpError on a 400, use this to pick the request uri
511 # out of the raised exception.
512 try:
513 http = HttpMockSequence([
514 ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
515 ])
516 zoo = build('zoo', 'v1', http=http, developerKey='foo',
517 discoveryServiceUrl='http://example.com')
518 self.fail('Should have raised an exception.')
519 except HttpError as e:
520 self.assertEqual(e.uri, 'http://example.com?key=foo')
521
Ethan Bao12b7cd32016-03-14 14:25:10 -0700522 def test_discovery_loading_from_v2_discovery_uri(self):
523 http = HttpMockSequence([
524 ({'status': '404'}, 'Not found'),
525 ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()),
526 ])
527 zoo = build('zoo', 'v1', http=http, cache_discovery=False)
528 self.assertTrue(hasattr(zoo, 'animals'))
Joe Gregorioa98733f2011-09-16 10:12:28 -0400529
Takashi Matsuo30125122015-08-19 11:42:32 -0700530class DiscoveryFromAppEngineCache(unittest.TestCase):
531 def test_appengine_memcache(self):
532 # Hack module import
533 self.orig_import = __import__
534 self.mocked_api = mock.MagicMock()
535
eesheeshc6425a02016-02-12 15:07:06 +0000536 def import_mock(name, *args, **kwargs):
Takashi Matsuo30125122015-08-19 11:42:32 -0700537 if name == 'google.appengine.api':
538 return self.mocked_api
eesheeshc6425a02016-02-12 15:07:06 +0000539 return self.orig_import(name, *args, **kwargs)
Takashi Matsuo30125122015-08-19 11:42:32 -0700540
541 import_fullname = '__builtin__.__import__'
542 if sys.version_info[0] >= 3:
543 import_fullname = 'builtins.__import__'
544
545 with mock.patch(import_fullname, side_effect=import_mock):
546 namespace = 'google-api-client'
547 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
548
549 self.mocked_api.memcache.get.return_value = None
550
551 plus = build('plus', 'v1', http=self.http)
552
553 # memcache.get is called once
554 url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
555 self.mocked_api.memcache.get.assert_called_once_with(url,
556 namespace=namespace)
557
558 # memcache.set is called once
559 with open(datafile('plus.json')) as f:
560 content = f.read()
561 self.mocked_api.memcache.set.assert_called_once_with(
562 url, content, time=DISCOVERY_DOC_MAX_AGE, namespace=namespace)
563
564 # Returns the cached content this time.
565 self.mocked_api.memcache.get.return_value = content
566
567 # Make sure the contents are returned from the cache.
568 # (Otherwise it should through an error)
569 self.http = HttpMock(None, {'status': '200'})
570
571 plus = build('plus', 'v1', http=self.http)
572
573 # memcache.get is called twice
574 self.mocked_api.memcache.get.assert_has_calls(
575 [mock.call(url, namespace=namespace),
576 mock.call(url, namespace=namespace)])
577
578 # memcahce.set is called just once
579 self.mocked_api.memcache.set.assert_called_once_with(
580 url, content, time=DISCOVERY_DOC_MAX_AGE,namespace=namespace)
581
582
583class DictCache(Cache):
584 def __init__(self):
585 self.d = {}
586 def get(self, url):
587 return self.d.get(url, None)
588 def set(self, url, content):
589 self.d[url] = content
590 def contains(self, url):
591 return url in self.d
592
593
594class DiscoveryFromFileCache(unittest.TestCase):
595 def test_file_based_cache(self):
596 cache = mock.Mock(wraps=DictCache())
Jon Wayne Parrott36d4e1b2016-10-17 13:31:33 -0700597 with mock.patch('googleapiclient.discovery_cache.autodetect',
598 return_value=cache):
Takashi Matsuo30125122015-08-19 11:42:32 -0700599 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
600
601 plus = build('plus', 'v1', http=self.http)
602
603 # cache.get is called once
604 url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
605 cache.get.assert_called_once_with(url)
606
607 # cache.set is called once
608 with open(datafile('plus.json')) as f:
609 content = f.read()
610 cache.set.assert_called_once_with(url, content)
611
612 # Make sure there is a cache entry for the plus v1 discovery doc.
613 self.assertTrue(cache.contains(url))
614
615 # Make sure the contents are returned from the cache.
616 # (Otherwise it should through an error)
617 self.http = HttpMock(None, {'status': '200'})
618
619 plus = build('plus', 'v1', http=self.http)
620
621 # cache.get is called twice
622 cache.get.assert_has_calls([mock.call(url), mock.call(url)])
623
624 # cahce.set is called just once
625 cache.set.assert_called_once_with(url, content)
626
627
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400628class Discovery(unittest.TestCase):
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400629
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400630 def test_method_error_checking(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -0500631 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400632 plus = build('plus', 'v1', http=self.http)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400633
634 # Missing required parameters
635 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500636 plus.activities().list()
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400637 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900638 except TypeError as e:
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400639 self.assertTrue('Missing' in str(e))
640
Joe Gregorio2467afa2012-06-20 12:21:25 -0400641 # Missing required parameters even if supplied as None.
642 try:
643 plus.activities().list(collection=None, userId=None)
644 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900645 except TypeError as e:
Joe Gregorio2467afa2012-06-20 12:21:25 -0400646 self.assertTrue('Missing' in str(e))
647
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400648 # Parameter doesn't match regex
649 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500650 plus.activities().list(collection='not_a_collection_name', userId='me')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400651 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900652 except TypeError as e:
Joe Gregorioca876e42011-02-22 19:39:42 -0500653 self.assertTrue('not an allowed value' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400654
655 # Unexpected parameter
656 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500657 plus.activities().list(flubber=12)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400658 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900659 except TypeError as e:
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400660 self.assertTrue('unexpected' in str(e))
661
Joe Gregoriobee86832011-02-22 10:00:19 -0500662 def _check_query_types(self, request):
Pat Ferated5b61bd2015-03-03 16:04:11 -0800663 parsed = urlparse(request.uri)
Joe Gregoriobee86832011-02-22 10:00:19 -0500664 q = parse_qs(parsed[4])
665 self.assertEqual(q['q'], ['foo'])
666 self.assertEqual(q['i'], ['1'])
667 self.assertEqual(q['n'], ['1.0'])
668 self.assertEqual(q['b'], ['false'])
669 self.assertEqual(q['a'], ['[1, 2, 3]'])
670 self.assertEqual(q['o'], ['{\'a\': 1}'])
671 self.assertEqual(q['e'], ['bar'])
672
673 def test_type_coercion(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400674 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400675 zoo = build('zoo', 'v1', http=http)
Joe Gregoriobee86832011-02-22 10:00:19 -0500676
Joe Gregorioa98733f2011-09-16 10:12:28 -0400677 request = zoo.query(
678 q="foo", i=1.0, n=1.0, b=0, a=[1,2,3], o={'a':1}, e='bar')
Joe Gregoriobee86832011-02-22 10:00:19 -0500679 self._check_query_types(request)
Joe Gregorioa98733f2011-09-16 10:12:28 -0400680 request = zoo.query(
681 q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar')
Joe Gregoriobee86832011-02-22 10:00:19 -0500682 self._check_query_types(request)
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500683
Joe Gregorioa98733f2011-09-16 10:12:28 -0400684 request = zoo.query(
Craig Citro1e742822012-03-01 12:59:22 -0800685 q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar', er='two')
Joe Gregorio6804c7a2011-11-18 14:30:32 -0500686
687 request = zoo.query(
Craig Citro1e742822012-03-01 12:59:22 -0800688 q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar',
689 er=['one', 'three'], rr=['foo', 'bar'])
Joe Gregoriobee86832011-02-22 10:00:19 -0500690 self._check_query_types(request)
691
Craig Citro1e742822012-03-01 12:59:22 -0800692 # Five is right out.
Joe Gregorio20c26e52012-03-02 15:58:31 -0500693 self.assertRaises(TypeError, zoo.query, er=['one', 'five'])
Craig Citro1e742822012-03-01 12:59:22 -0800694
Joe Gregorio13217952011-02-22 15:37:38 -0500695 def test_optional_stack_query_parameters(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400696 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400697 zoo = build('zoo', 'v1', http=http)
Joe Gregoriof4153422011-03-18 22:45:18 -0400698 request = zoo.query(trace='html', fields='description')
Joe Gregorio13217952011-02-22 15:37:38 -0500699
Pat Ferated5b61bd2015-03-03 16:04:11 -0800700 parsed = urlparse(request.uri)
Joe Gregorioca876e42011-02-22 19:39:42 -0500701 q = parse_qs(parsed[4])
702 self.assertEqual(q['trace'], ['html'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400703 self.assertEqual(q['fields'], ['description'])
704
Joe Gregorio2467afa2012-06-20 12:21:25 -0400705 def test_string_params_value_of_none_get_dropped(self):
Joe Gregorio4b4002f2012-06-14 15:41:01 -0400706 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400707 zoo = build('zoo', 'v1', http=http)
Joe Gregorio2467afa2012-06-20 12:21:25 -0400708 request = zoo.query(trace=None, fields='description')
709
Pat Ferated5b61bd2015-03-03 16:04:11 -0800710 parsed = urlparse(request.uri)
Joe Gregorio2467afa2012-06-20 12:21:25 -0400711 q = parse_qs(parsed[4])
712 self.assertFalse('trace' in q)
Joe Gregorio4b4002f2012-06-14 15:41:01 -0400713
Joe Gregorioe08a1662011-12-07 09:48:22 -0500714 def test_model_added_query_parameters(self):
715 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400716 zoo = build('zoo', 'v1', http=http)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500717 request = zoo.animals().get(name='Lion')
718
Pat Ferated5b61bd2015-03-03 16:04:11 -0800719 parsed = urlparse(request.uri)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500720 q = parse_qs(parsed[4])
721 self.assertEqual(q['alt'], ['json'])
722 self.assertEqual(request.headers['accept'], 'application/json')
723
724 def test_fallback_to_raw_model(self):
725 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400726 zoo = build('zoo', 'v1', http=http)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500727 request = zoo.animals().getmedia(name='Lion')
728
Pat Ferated5b61bd2015-03-03 16:04:11 -0800729 parsed = urlparse(request.uri)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500730 q = parse_qs(parsed[4])
731 self.assertTrue('alt' not in q)
732 self.assertEqual(request.headers['accept'], '*/*')
733
Joe Gregoriof4153422011-03-18 22:45:18 -0400734 def test_patch(self):
735 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400736 zoo = build('zoo', 'v1', http=http)
Joe Gregoriof4153422011-03-18 22:45:18 -0400737 request = zoo.animals().patch(name='lion', body='{"description": "foo"}')
738
739 self.assertEqual(request.method, 'PATCH')
740
Pepper Lebeck-Jobe860836f2015-06-12 20:42:23 -0400741 def test_batch_request_from_discovery(self):
742 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
743 # zoo defines a batchPath
744 zoo = build('zoo', 'v1', http=self.http)
745 batch_request = zoo.new_batch_http_request()
746 self.assertEqual(batch_request._batch_uri,
747 "https://www.googleapis.com/batchZoo")
748
749 def test_batch_request_from_default(self):
750 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
751 # plus does not define a batchPath
752 plus = build('plus', 'v1', http=self.http)
753 batch_request = plus.new_batch_http_request()
754 self.assertEqual(batch_request._batch_uri,
755 "https://www.googleapis.com/batch")
756
Joe Gregoriof4153422011-03-18 22:45:18 -0400757 def test_tunnel_patch(self):
758 http = HttpMockSequence([
Joe Gregorio79daca02013-03-29 16:25:52 -0400759 ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()),
Joe Gregoriof4153422011-03-18 22:45:18 -0400760 ({'status': '200'}, 'echo_request_headers_as_json'),
761 ])
762 http = tunnel_patch(http)
Takashi Matsuo30125122015-08-19 11:42:32 -0700763 zoo = build('zoo', 'v1', http=http, cache_discovery=False)
Joe Gregorioa98733f2011-09-16 10:12:28 -0400764 resp = zoo.animals().patch(
765 name='lion', body='{"description": "foo"}').execute()
Joe Gregoriof4153422011-03-18 22:45:18 -0400766
767 self.assertTrue('x-http-method-override' in resp)
Joe Gregorioca876e42011-02-22 19:39:42 -0500768
Joe Gregorio7b70f432011-11-09 10:18:51 -0500769 def test_plus_resources(self):
770 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400771 plus = build('plus', 'v1', http=self.http)
Joe Gregorio7b70f432011-11-09 10:18:51 -0500772 self.assertTrue(getattr(plus, 'activities'))
773 self.assertTrue(getattr(plus, 'people'))
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400774
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800775 def test_oauth2client_credentials(self):
776 credentials = mock.Mock(spec=GoogleCredentials)
777 credentials.create_scoped_required.return_value = False
Orest Bolohane92c9002014-05-30 11:15:43 -0700778
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800779 discovery = open(datafile('plus.json')).read()
780 service = build_from_document(discovery, credentials=credentials)
781 self.assertEqual(service._http, credentials.authorize.return_value)
Orest Bolohane92c9002014-05-30 11:15:43 -0700782
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800783 def test_google_auth_credentials(self):
784 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
785 discovery = open(datafile('plus.json')).read()
786 service = build_from_document(discovery, credentials=credentials)
787
788 self.assertIsInstance(service._http, google_auth_httplib2.AuthorizedHttp)
789 self.assertEqual(service._http.credentials, credentials)
790
791 def test_no_scopes_no_credentials(self):
792 # Zoo doesn't have scopes
793 discovery = open(datafile('zoo.json')).read()
794 service = build_from_document(discovery)
795 # Should be an ordinary httplib2.Http instance and not AuthorizedHttp.
796 self.assertIsInstance(service._http, httplib2.Http)
Orest Bolohane92c9002014-05-30 11:15:43 -0700797
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400798 def test_full_featured(self):
799 # Zoo should exercise all discovery facets
800 # and should also have no future.json file.
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500801 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400802 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400803 self.assertTrue(getattr(zoo, 'animals'))
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500804
Joe Gregoriof4153422011-03-18 22:45:18 -0400805 request = zoo.animals().list(name='bat', projection="full")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800806 parsed = urlparse(request.uri)
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400807 q = parse_qs(parsed[4])
808 self.assertEqual(q['name'], ['bat'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400809 self.assertEqual(q['projection'], ['full'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400810
Joe Gregorio3fada332011-01-07 17:07:45 -0500811 def test_nested_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500812 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400813 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio3fada332011-01-07 17:07:45 -0500814 self.assertTrue(getattr(zoo, 'animals'))
815 request = zoo.my().favorites().list(max_results="5")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800816 parsed = urlparse(request.uri)
Joe Gregorio3fada332011-01-07 17:07:45 -0500817 q = parse_qs(parsed[4])
818 self.assertEqual(q['max-results'], ['5'])
819
Pat Feratec6050872015-03-03 18:24:59 -0800820 @unittest.skipIf(six.PY3, 'print is not a reserved name in Python 3')
Joe Gregoriod92897c2011-07-07 11:44:56 -0400821 def test_methods_with_reserved_names(self):
822 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400823 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod92897c2011-07-07 11:44:56 -0400824 self.assertTrue(getattr(zoo, 'animals'))
825 request = zoo.global_().print_().assert_(max_results="5")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800826 parsed = urlparse(request.uri)
Joe Gregorioa2838152012-07-16 11:52:17 -0400827 self.assertEqual(parsed[2], '/zoo/v1/global/print/assert')
Joe Gregoriod92897c2011-07-07 11:44:56 -0400828
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500829 def test_top_level_functions(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500830 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400831 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500832 self.assertTrue(getattr(zoo, 'query'))
833 request = zoo.query(q="foo")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800834 parsed = urlparse(request.uri)
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500835 q = parse_qs(parsed[4])
836 self.assertEqual(q['q'], ['foo'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400837
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400838 def test_simple_media_uploads(self):
839 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400840 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400841 doc = getattr(zoo.animals().insert, '__doc__')
842 self.assertTrue('media_body' in doc)
843
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400844 def test_simple_media_upload_no_max_size_provided(self):
845 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400846 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400847 request = zoo.animals().crossbreed(media_body=datafile('small.png'))
848 self.assertEquals('image/png', request.headers['content-type'])
Pat Ferate2b140222015-03-03 18:05:11 -0800849 self.assertEquals(b'PNG', request.body[1:4])
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400850
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400851 def test_simple_media_raise_correct_exceptions(self):
852 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400853 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400854
855 try:
856 zoo.animals().insert(media_body=datafile('smiley.png'))
857 self.fail("should throw exception if media is too large.")
858 except MediaUploadSizeError:
859 pass
860
861 try:
862 zoo.animals().insert(media_body=datafile('small.jpg'))
863 self.fail("should throw exception if mimetype is unacceptable.")
864 except UnacceptableMimeTypeError:
865 pass
866
867 def test_simple_media_good_upload(self):
868 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400869 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400870
871 request = zoo.animals().insert(media_body=datafile('small.png'))
872 self.assertEquals('image/png', request.headers['content-type'])
Pat Ferate2b140222015-03-03 18:05:11 -0800873 self.assertEquals(b'PNG', request.body[1:4])
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500874 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -0400875 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -0500876 request.uri)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400877
Brian J. Watson38051ac2016-10-25 07:53:08 -0700878 def test_simple_media_unknown_mimetype(self):
879 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
880 zoo = build('zoo', 'v1', http=self.http)
881
882 try:
883 zoo.animals().insert(media_body=datafile('small-png'))
884 self.fail("should throw exception if mimetype is unknown.")
885 except UnknownFileType:
886 pass
887
888 request = zoo.animals().insert(media_body=datafile('small-png'),
889 media_mime_type='image/png')
890 self.assertEquals('image/png', request.headers['content-type'])
891 self.assertEquals(b'PNG', request.body[1:4])
892 assertUrisEqual(self,
893 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json',
894 request.uri)
895
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400896 def test_multipart_media_raise_correct_exceptions(self):
897 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400898 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400899
900 try:
901 zoo.animals().insert(media_body=datafile('smiley.png'), body={})
902 self.fail("should throw exception if media is too large.")
903 except MediaUploadSizeError:
904 pass
905
906 try:
907 zoo.animals().insert(media_body=datafile('small.jpg'), body={})
908 self.fail("should throw exception if mimetype is unacceptable.")
909 except UnacceptableMimeTypeError:
910 pass
911
912 def test_multipart_media_good_upload(self):
913 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400914 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400915
916 request = zoo.animals().insert(media_body=datafile('small.png'), body={})
Joe Gregorioa98733f2011-09-16 10:12:28 -0400917 self.assertTrue(request.headers['content-type'].startswith(
918 'multipart/related'))
Phil Ruffwind26178fc2015-10-13 19:00:33 -0400919 with open(datafile('small.png'), 'rb') as f:
920 contents = f.read()
921 boundary = re.match(b'--=+([^=]+)', request.body).group(1)
922 self.assertEqual(
923 request.body.rstrip(b"\n"), # Python 2.6 does not add a trailing \n
924 b'--===============' + boundary + b'==\n' +
925 b'Content-Type: application/json\n' +
926 b'MIME-Version: 1.0\n\n' +
927 b'{"data": {}}\n' +
928 b'--===============' + boundary + b'==\n' +
929 b'Content-Type: image/png\n' +
930 b'MIME-Version: 1.0\n' +
931 b'Content-Transfer-Encoding: binary\n\n' +
932 contents +
933 b'\n--===============' + boundary + b'==--')
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500934 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -0400935 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=multipart&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -0500936 request.uri)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400937
938 def test_media_capable_method_without_media(self):
939 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400940 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400941
942 request = zoo.animals().insert(body={})
943 self.assertTrue(request.headers['content-type'], 'application/json')
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400944
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500945 def test_resumable_multipart_media_good_upload(self):
946 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400947 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500948
949 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
950 request = zoo.animals().insert(media_body=media_upload, body={})
951 self.assertTrue(request.headers['content-type'].startswith(
Joe Gregorio945be3e2012-01-27 17:01:06 -0500952 'application/json'))
953 self.assertEquals('{"data": {}}', request.body)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500954 self.assertEquals(media_upload, request.resumable)
955
956 self.assertEquals('image/png', request.resumable.mimetype())
957
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500958 self.assertNotEquals(request.body, None)
959 self.assertEquals(request.resumable_uri, None)
960
961 http = HttpMockSequence([
962 ({'status': '200',
963 'location': 'http://upload.example.com'}, ''),
964 ({'status': '308',
Matt Carroll94a53942016-12-20 13:56:43 -0800965 'location': 'http://upload.example.com/2'}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500966 ({'status': '308',
967 'location': 'http://upload.example.com/3',
Matt Carroll94a53942016-12-20 13:56:43 -0800968 'range': '0-12'}, ''),
969 ({'status': '308',
970 'location': 'http://upload.example.com/4',
Joe Gregorio945be3e2012-01-27 17:01:06 -0500971 'range': '0-%d' % (media_upload.size() - 2)}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500972 ({'status': '200'}, '{"foo": "bar"}'),
973 ])
974
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400975 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500976 self.assertEquals(None, body)
977 self.assertTrue(isinstance(status, MediaUploadProgress))
Matt Carroll94a53942016-12-20 13:56:43 -0800978 self.assertEquals(0, status.resumable_progress)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500979
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500980 # Two requests should have been made and the resumable_uri should have been
981 # updated for each one.
982 self.assertEquals(request.resumable_uri, 'http://upload.example.com/2')
Matt Carroll94a53942016-12-20 13:56:43 -0800983 self.assertEquals(media_upload, request.resumable)
984 self.assertEquals(0, request.resumable_progress)
985
986 # This next chuck call should upload the first chunk
987 status, body = request.next_chunk(http=http)
988 self.assertEquals(request.resumable_uri, 'http://upload.example.com/3')
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500989 self.assertEquals(media_upload, request.resumable)
990 self.assertEquals(13, request.resumable_progress)
991
Matt Carroll94a53942016-12-20 13:56:43 -0800992 # This call will upload the next chunk
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400993 status, body = request.next_chunk(http=http)
Matt Carroll94a53942016-12-20 13:56:43 -0800994 self.assertEquals(request.resumable_uri, 'http://upload.example.com/4')
Joe Gregorio945be3e2012-01-27 17:01:06 -0500995 self.assertEquals(media_upload.size()-1, request.resumable_progress)
996 self.assertEquals('{"data": {}}', request.body)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500997
998 # Final call to next_chunk should complete the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400999 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001000 self.assertEquals(body, {"foo": "bar"})
1001 self.assertEquals(status, None)
1002
1003
1004 def test_resumable_media_good_upload(self):
1005 """Not a multipart upload."""
1006 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001007 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001008
1009 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1010 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001011 self.assertEquals(media_upload, request.resumable)
1012
1013 self.assertEquals('image/png', request.resumable.mimetype())
1014
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001015 self.assertEquals(request.body, None)
1016 self.assertEquals(request.resumable_uri, None)
1017
1018 http = HttpMockSequence([
1019 ({'status': '200',
1020 'location': 'http://upload.example.com'}, ''),
1021 ({'status': '308',
1022 'location': 'http://upload.example.com/2',
1023 'range': '0-12'}, ''),
1024 ({'status': '308',
1025 'location': 'http://upload.example.com/3',
Joe Gregorio945be3e2012-01-27 17:01:06 -05001026 'range': '0-%d' % (media_upload.size() - 2)}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001027 ({'status': '200'}, '{"foo": "bar"}'),
1028 ])
1029
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001030 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001031 self.assertEquals(None, body)
1032 self.assertTrue(isinstance(status, MediaUploadProgress))
1033 self.assertEquals(13, status.resumable_progress)
1034
1035 # Two requests should have been made and the resumable_uri should have been
1036 # updated for each one.
1037 self.assertEquals(request.resumable_uri, 'http://upload.example.com/2')
1038
1039 self.assertEquals(media_upload, request.resumable)
1040 self.assertEquals(13, request.resumable_progress)
1041
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001042 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001043 self.assertEquals(request.resumable_uri, 'http://upload.example.com/3')
Joe Gregorio945be3e2012-01-27 17:01:06 -05001044 self.assertEquals(media_upload.size()-1, request.resumable_progress)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001045 self.assertEquals(request.body, None)
1046
1047 # Final call to next_chunk should complete the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001048 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001049 self.assertEquals(body, {"foo": "bar"})
1050 self.assertEquals(status, None)
1051
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001052 def test_resumable_media_good_upload_from_execute(self):
1053 """Not a multipart upload."""
1054 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001055 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001056
1057 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1058 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregoriof1ba7f12012-11-16 15:10:47 -05001059 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -04001060 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=resumable&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -05001061 request.uri)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001062
1063 http = HttpMockSequence([
1064 ({'status': '200',
1065 'location': 'http://upload.example.com'}, ''),
1066 ({'status': '308',
1067 'location': 'http://upload.example.com/2',
1068 'range': '0-12'}, ''),
1069 ({'status': '308',
1070 'location': 'http://upload.example.com/3',
Joe Gregorio945be3e2012-01-27 17:01:06 -05001071 'range': '0-%d' % media_upload.size()}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001072 ({'status': '200'}, '{"foo": "bar"}'),
1073 ])
1074
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001075 body = request.execute(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001076 self.assertEquals(body, {"foo": "bar"})
1077
1078 def test_resumable_media_fail_unknown_response_code_first_request(self):
1079 """Not a multipart upload."""
1080 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001081 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001082
1083 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1084 request = zoo.animals().insert(media_body=media_upload, body=None)
1085
1086 http = HttpMockSequence([
1087 ({'status': '400',
1088 'location': 'http://upload.example.com'}, ''),
1089 ])
1090
Joe Gregoriobaf04802013-03-01 12:27:06 -05001091 try:
1092 request.execute(http=http)
1093 self.fail('Should have raised ResumableUploadError.')
INADA Naokic1505df2014-08-20 15:19:53 +09001094 except ResumableUploadError as e:
Joe Gregoriobaf04802013-03-01 12:27:06 -05001095 self.assertEqual(400, e.resp.status)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001096
1097 def test_resumable_media_fail_unknown_response_code_subsequent_request(self):
1098 """Not a multipart upload."""
1099 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001100 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001101
1102 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1103 request = zoo.animals().insert(media_body=media_upload, body=None)
1104
1105 http = HttpMockSequence([
1106 ({'status': '200',
1107 'location': 'http://upload.example.com'}, ''),
1108 ({'status': '400'}, ''),
1109 ])
1110
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001111 self.assertRaises(HttpError, request.execute, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001112 self.assertTrue(request._in_error_state)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001113
Joe Gregorio910b9b12012-06-12 09:36:30 -04001114 http = HttpMockSequence([
1115 ({'status': '308',
1116 'range': '0-5'}, ''),
1117 ({'status': '308',
1118 'range': '0-6'}, ''),
1119 ])
1120
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001121 status, body = request.next_chunk(http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001122 self.assertEquals(status.resumable_progress, 7,
1123 'Should have first checked length and then tried to PUT more.')
1124 self.assertFalse(request._in_error_state)
1125
1126 # Put it back in an error state.
1127 http = HttpMockSequence([
1128 ({'status': '400'}, ''),
1129 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001130 self.assertRaises(HttpError, request.execute, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001131 self.assertTrue(request._in_error_state)
1132
1133 # Pretend the last request that 400'd actually succeeded.
1134 http = HttpMockSequence([
1135 ({'status': '200'}, '{"foo": "bar"}'),
1136 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001137 status, body = request.next_chunk(http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001138 self.assertEqual(body, {'foo': 'bar'})
1139
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001140 def test_media_io_base_stream_unlimited_chunksize_resume(self):
1141 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1142 zoo = build('zoo', 'v1', http=self.http)
1143
Pat Ferateed9affd2015-03-03 16:03:15 -08001144 # Set up a seekable stream and try to upload in single chunk.
Pat Ferate2b140222015-03-03 18:05:11 -08001145 fd = BytesIO(b'01234"56789"')
Pat Ferateed9affd2015-03-03 16:03:15 -08001146 media_upload = MediaIoBaseUpload(
1147 fd=fd, mimetype='text/plain', chunksize=-1, resumable=True)
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001148
Pat Ferateed9affd2015-03-03 16:03:15 -08001149 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001150
Pat Ferateed9affd2015-03-03 16:03:15 -08001151 # The single chunk fails, restart at the right point.
1152 http = HttpMockSequence([
1153 ({'status': '200',
1154 'location': 'http://upload.example.com'}, ''),
1155 ({'status': '308',
1156 'location': 'http://upload.example.com/2',
1157 'range': '0-4'}, ''),
1158 ({'status': '200'}, 'echo_request_body'),
1159 ])
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001160
Pat Ferateed9affd2015-03-03 16:03:15 -08001161 body = request.execute(http=http)
1162 self.assertEqual('56789', body)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001163
1164 def test_media_io_base_stream_chunksize_resume(self):
1165 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1166 zoo = build('zoo', 'v1', http=self.http)
1167
Pat Ferateed9affd2015-03-03 16:03:15 -08001168 # Set up a seekable stream and try to upload in chunks.
Pat Ferate2b140222015-03-03 18:05:11 -08001169 fd = BytesIO(b'0123456789')
Pat Ferateed9affd2015-03-03 16:03:15 -08001170 media_upload = MediaIoBaseUpload(
1171 fd=fd, mimetype='text/plain', chunksize=5, resumable=True)
1172
1173 request = zoo.animals().insert(media_body=media_upload, body=None)
1174
1175 # The single chunk fails, pull the content sent out of the exception.
1176 http = HttpMockSequence([
1177 ({'status': '200',
1178 'location': 'http://upload.example.com'}, ''),
1179 ({'status': '400'}, 'echo_request_body'),
1180 ])
1181
Joe Gregorio5c120db2012-08-23 09:13:55 -04001182 try:
Pat Ferateed9affd2015-03-03 16:03:15 -08001183 body = request.execute(http=http)
1184 except HttpError as e:
Pat Ferate2b140222015-03-03 18:05:11 -08001185 self.assertEqual(b'01234', e.content)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001186
Joe Gregorio910b9b12012-06-12 09:36:30 -04001187 def test_resumable_media_handle_uploads_of_unknown_size(self):
1188 http = HttpMockSequence([
1189 ({'status': '200',
1190 'location': 'http://upload.example.com'}, ''),
1191 ({'status': '200'}, 'echo_request_headers_as_json'),
1192 ])
1193
1194 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001195 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001196
Joe Gregorio910b9b12012-06-12 09:36:30 -04001197 # Create an upload that doesn't know the full size of the media.
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001198 class IoBaseUnknownLength(MediaUpload):
1199 def chunksize(self):
1200 return 10
1201
1202 def mimetype(self):
1203 return 'image/png'
1204
1205 def size(self):
1206 return None
1207
1208 def resumable(self):
1209 return True
1210
1211 def getbytes(self, begin, length):
1212 return '0123456789'
1213
1214 upload = IoBaseUnknownLength()
Joe Gregorio910b9b12012-06-12 09:36:30 -04001215
1216 request = zoo.animals().insert(media_body=upload, body=None)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001217 status, body = request.next_chunk(http=http)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001218 self.assertEqual(body, {
1219 'Content-Range': 'bytes 0-9/*',
1220 'Content-Length': '10',
1221 })
Joe Gregorio44454e42012-06-15 08:38:53 -04001222
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001223 def test_resumable_media_no_streaming_on_unsupported_platforms(self):
1224 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1225 zoo = build('zoo', 'v1', http=self.http)
1226
1227 class IoBaseHasStream(MediaUpload):
1228 def chunksize(self):
1229 return 10
1230
1231 def mimetype(self):
1232 return 'image/png'
1233
1234 def size(self):
1235 return None
1236
1237 def resumable(self):
1238 return True
1239
1240 def getbytes(self, begin, length):
1241 return '0123456789'
1242
1243 def has_stream(self):
1244 return True
1245
1246 def stream(self):
1247 raise NotImplementedError()
1248
1249 upload = IoBaseHasStream()
1250
1251 orig_version = sys.version_info
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001252
1253 sys.version_info = (2, 6, 5, 'final', 0)
1254
1255 request = zoo.animals().insert(media_body=upload, body=None)
1256
1257 # This should raise an exception because stream() will be called.
1258 http = HttpMockSequence([
1259 ({'status': '200',
1260 'location': 'http://upload.example.com'}, ''),
1261 ({'status': '200'}, 'echo_request_headers_as_json'),
1262 ])
1263
1264 self.assertRaises(NotImplementedError, request.next_chunk, http=http)
1265
1266 sys.version_info = orig_version
1267
Joe Gregorio44454e42012-06-15 08:38:53 -04001268 def test_resumable_media_handle_uploads_of_unknown_size_eof(self):
1269 http = HttpMockSequence([
1270 ({'status': '200',
1271 'location': 'http://upload.example.com'}, ''),
1272 ({'status': '200'}, 'echo_request_headers_as_json'),
1273 ])
1274
1275 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001276 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio44454e42012-06-15 08:38:53 -04001277
Pat Ferate2b140222015-03-03 18:05:11 -08001278 fd = BytesIO(b'data goes here')
Joe Gregorio44454e42012-06-15 08:38:53 -04001279
1280 # Create an upload that doesn't know the full size of the media.
1281 upload = MediaIoBaseUpload(
Joe Gregorio4a2c29f2012-07-12 12:52:47 -04001282 fd=fd, mimetype='image/png', chunksize=15, resumable=True)
Joe Gregorio44454e42012-06-15 08:38:53 -04001283
1284 request = zoo.animals().insert(media_body=upload, body=None)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001285 status, body = request.next_chunk(http=http)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001286 self.assertEqual(body, {
1287 'Content-Range': 'bytes 0-13/14',
1288 'Content-Length': '14',
1289 })
Joe Gregorio910b9b12012-06-12 09:36:30 -04001290
1291 def test_resumable_media_handle_resume_of_upload_of_unknown_size(self):
1292 http = HttpMockSequence([
1293 ({'status': '200',
1294 'location': 'http://upload.example.com'}, ''),
1295 ({'status': '400'}, ''),
1296 ])
1297
1298 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001299 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001300
1301 # Create an upload that doesn't know the full size of the media.
Pat Ferate2b140222015-03-03 18:05:11 -08001302 fd = BytesIO(b'data goes here')
Joe Gregorio910b9b12012-06-12 09:36:30 -04001303
1304 upload = MediaIoBaseUpload(
Joe Gregorio4a2c29f2012-07-12 12:52:47 -04001305 fd=fd, mimetype='image/png', chunksize=500, resumable=True)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001306
1307 request = zoo.animals().insert(media_body=upload, body=None)
1308
1309 # Put it in an error state.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001310 self.assertRaises(HttpError, request.next_chunk, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001311
1312 http = HttpMockSequence([
1313 ({'status': '400',
1314 'range': '0-5'}, 'echo_request_headers_as_json'),
1315 ])
1316 try:
1317 # Should resume the upload by first querying the status of the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001318 request.next_chunk(http=http)
INADA Naokic1505df2014-08-20 15:19:53 +09001319 except HttpError as e:
Joe Gregorio910b9b12012-06-12 09:36:30 -04001320 expected = {
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001321 'Content-Range': 'bytes */14',
Joe Gregorio910b9b12012-06-12 09:36:30 -04001322 'content-length': '0'
1323 }
INADA Naoki09157612015-03-25 01:51:03 +09001324 self.assertEqual(expected, json.loads(e.content.decode('utf-8')),
Joe Gregorio910b9b12012-06-12 09:36:30 -04001325 'Should send an empty body when requesting the current upload status.')
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001326
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001327 def test_pickle(self):
1328 sorted_resource_keys = ['_baseUrl',
1329 '_developerKey',
1330 '_dynamic_attrs',
1331 '_http',
1332 '_model',
1333 '_requestBuilder',
1334 '_resourceDesc',
1335 '_rootDesc',
1336 '_schema',
1337 'animals',
1338 'global_',
1339 'load',
1340 'loadNoTemplate',
1341 'my',
Pepper Lebeck-Jobe860836f2015-06-12 20:42:23 -04001342 'new_batch_http_request',
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001343 'query',
1344 'scopedAnimals']
1345
1346 http = HttpMock(datafile('zoo.json'), {'status': '200'})
1347 zoo = build('zoo', 'v1', http=http)
1348 self.assertEqual(sorted(zoo.__dict__.keys()), sorted_resource_keys)
1349
1350 pickled_zoo = pickle.dumps(zoo)
1351 new_zoo = pickle.loads(pickled_zoo)
1352 self.assertEqual(sorted(new_zoo.__dict__.keys()), sorted_resource_keys)
1353 self.assertTrue(hasattr(new_zoo, 'animals'))
1354 self.assertTrue(callable(new_zoo.animals))
1355 self.assertTrue(hasattr(new_zoo, 'global_'))
1356 self.assertTrue(callable(new_zoo.global_))
1357 self.assertTrue(hasattr(new_zoo, 'load'))
1358 self.assertTrue(callable(new_zoo.load))
1359 self.assertTrue(hasattr(new_zoo, 'loadNoTemplate'))
1360 self.assertTrue(callable(new_zoo.loadNoTemplate))
1361 self.assertTrue(hasattr(new_zoo, 'my'))
1362 self.assertTrue(callable(new_zoo.my))
1363 self.assertTrue(hasattr(new_zoo, 'query'))
1364 self.assertTrue(callable(new_zoo.query))
1365 self.assertTrue(hasattr(new_zoo, 'scopedAnimals'))
1366 self.assertTrue(callable(new_zoo.scopedAnimals))
1367
Joe Gregorio003b6e42013-02-13 15:42:19 -05001368 self.assertEqual(sorted(zoo._dynamic_attrs), sorted(new_zoo._dynamic_attrs))
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001369 self.assertEqual(zoo._baseUrl, new_zoo._baseUrl)
1370 self.assertEqual(zoo._developerKey, new_zoo._developerKey)
1371 self.assertEqual(zoo._requestBuilder, new_zoo._requestBuilder)
1372 self.assertEqual(zoo._resourceDesc, new_zoo._resourceDesc)
1373 self.assertEqual(zoo._rootDesc, new_zoo._rootDesc)
1374 # _http, _model and _schema won't be equal since we will get new
1375 # instances upon un-pickling
1376
1377 def _dummy_zoo_request(self):
1378 with open(os.path.join(DATA_DIR, 'zoo.json'), 'rU') as fh:
1379 zoo_contents = fh.read()
1380
1381 zoo_uri = uritemplate.expand(DISCOVERY_URI,
1382 {'api': 'zoo', 'apiVersion': 'v1'})
1383 if 'REMOTE_ADDR' in os.environ:
Joe Gregorio79daca02013-03-29 16:25:52 -04001384 zoo_uri = util._add_query_parameter(zoo_uri, 'userIp',
1385 os.environ['REMOTE_ADDR'])
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001386
Igor Maravić22435292017-01-19 22:28:22 +01001387 http = build_http()
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001388 original_request = http.request
1389 def wrapped_request(uri, method='GET', *args, **kwargs):
1390 if uri == zoo_uri:
1391 return httplib2.Response({'status': '200'}), zoo_contents
1392 return original_request(uri, method=method, *args, **kwargs)
1393 http.request = wrapped_request
1394 return http
1395
1396 def _dummy_token(self):
1397 access_token = 'foo'
1398 client_id = 'some_client_id'
1399 client_secret = 'cOuDdkfjxxnv+'
1400 refresh_token = '1/0/a.df219fjls0'
1401 token_expiry = datetime.datetime.utcnow()
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001402 user_agent = 'refresh_checker/1.0'
1403 return OAuth2Credentials(
1404 access_token, client_id, client_secret,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -08001405 refresh_token, token_expiry, GOOGLE_TOKEN_URI,
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001406 user_agent)
1407
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001408 def test_pickle_with_credentials(self):
1409 credentials = self._dummy_token()
1410 http = self._dummy_zoo_request()
1411 http = credentials.authorize(http)
1412 self.assertTrue(hasattr(http.request, 'credentials'))
1413
1414 zoo = build('zoo', 'v1', http=http)
1415 pickled_zoo = pickle.dumps(zoo)
1416 new_zoo = pickle.loads(pickled_zoo)
1417 self.assertEqual(sorted(zoo.__dict__.keys()),
1418 sorted(new_zoo.__dict__.keys()))
1419 new_http = new_zoo._http
1420 self.assertFalse(hasattr(new_http.request, 'credentials'))
1421
andrewnestera4a44cf2017-03-31 16:09:31 +03001422 def test_resumable_media_upload_no_content(self):
1423 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1424 zoo = build('zoo', 'v1', http=self.http)
1425
1426 media_upload = MediaFileUpload(datafile('empty'), resumable=True)
1427 request = zoo.animals().insert(media_body=media_upload, body=None)
1428
1429 self.assertEquals(media_upload, request.resumable)
1430 self.assertEquals(request.body, None)
1431 self.assertEquals(request.resumable_uri, None)
1432
1433 http = HttpMockSequence([
1434 ({'status': '200',
1435 'location': 'http://upload.example.com'}, ''),
1436 ({'status': '308',
1437 'location': 'http://upload.example.com/2',
1438 'range': '0-0'}, ''),
1439 ])
1440
1441 status, body = request.next_chunk(http=http)
1442 self.assertEquals(None, body)
1443 self.assertTrue(isinstance(status, MediaUploadProgress))
1444 self.assertEquals(0, status.progress())
1445
Joe Gregorio708388c2012-06-15 13:43:04 -04001446
Joe Gregorioc5c5a372010-09-22 11:42:32 -04001447class Next(unittest.TestCase):
Joe Gregorio00cf1d92010-09-27 09:22:03 -04001448
Joe Gregorio3c676f92011-07-25 10:38:14 -04001449 def test_next_successful_none_on_no_next_page_token(self):
1450 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001451 tasks = build('tasks', 'v1', http=self.http)
Joe Gregorio3c676f92011-07-25 10:38:14 -04001452 request = tasks.tasklists().list()
1453 self.assertEqual(None, tasks.tasklists().list_next(request, {}))
1454
Son Dinh2a9a2132015-07-23 16:30:56 +00001455 def test_next_successful_none_on_empty_page_token(self):
1456 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
1457 tasks = build('tasks', 'v1', http=self.http)
1458 request = tasks.tasklists().list()
1459 next_request = tasks.tasklists().list_next(
1460 request, {'nextPageToken': ''})
1461 self.assertEqual(None, next_request)
1462
Joe Gregorio3c676f92011-07-25 10:38:14 -04001463 def test_next_successful_with_next_page_token(self):
1464 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001465 tasks = build('tasks', 'v1', http=self.http)
Joe Gregorio3c676f92011-07-25 10:38:14 -04001466 request = tasks.tasklists().list()
Joe Gregorioa98733f2011-09-16 10:12:28 -04001467 next_request = tasks.tasklists().list_next(
1468 request, {'nextPageToken': '123abc'})
Pat Ferated5b61bd2015-03-03 16:04:11 -08001469 parsed = list(urlparse(next_request.uri))
Joe Gregorio3c676f92011-07-25 10:38:14 -04001470 q = parse_qs(parsed[4])
1471 self.assertEqual(q['pageToken'][0], '123abc')
1472
Thomas Coffee20af04d2017-02-10 15:24:44 -08001473 def test_next_successful_with_next_page_token_alternate_name(self):
1474 self.http = HttpMock(datafile('bigquery.json'), {'status': '200'})
1475 bigquery = build('bigquery', 'v2', http=self.http)
1476 request = bigquery.tabledata().list(datasetId='', projectId='', tableId='')
1477 next_request = bigquery.tabledata().list_next(
1478 request, {'pageToken': '123abc'})
1479 parsed = list(urlparse(next_request.uri))
1480 q = parse_qs(parsed[4])
1481 self.assertEqual(q['pageToken'][0], '123abc')
1482
1483 def test_next_successful_with_next_page_token_in_body(self):
1484 self.http = HttpMock(datafile('logging.json'), {'status': '200'})
1485 logging = build('logging', 'v2', http=self.http)
1486 request = logging.entries().list(body={})
1487 next_request = logging.entries().list_next(
1488 request, {'nextPageToken': '123abc'})
1489 body = JsonModel().deserialize(next_request.body)
1490 self.assertEqual(body['pageToken'], '123abc')
1491
Joe Gregorio555f33c2011-08-19 14:56:07 -04001492 def test_next_with_method_with_no_properties(self):
1493 self.http = HttpMock(datafile('latitude.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001494 service = build('latitude', 'v1', http=self.http)
Thomas Coffee20af04d2017-02-10 15:24:44 -08001495 service.currentLocation().get()
1496
1497 def test_next_nonexistent_with_no_next_page_token(self):
1498 self.http = HttpMock(datafile('drive.json'), {'status': '200'})
1499 drive = build('drive', 'v3', http=self.http)
1500 drive.changes().watch(body={})
1501 self.assertFalse(callable(getattr(drive.changes(), 'watch_next', None)))
1502
1503 def test_next_successful_with_next_page_token_required(self):
1504 self.http = HttpMock(datafile('drive.json'), {'status': '200'})
1505 drive = build('drive', 'v3', http=self.http)
1506 request = drive.changes().list(pageToken='startPageToken')
1507 next_request = drive.changes().list_next(
1508 request, {'nextPageToken': '123abc'})
1509 parsed = list(urlparse(next_request.uri))
1510 q = parse_qs(parsed[4])
1511 self.assertEqual(q['pageToken'][0], '123abc')
Joe Gregorio00cf1d92010-09-27 09:22:03 -04001512
Joe Gregorioa98733f2011-09-16 10:12:28 -04001513
Joe Gregorio708388c2012-06-15 13:43:04 -04001514class MediaGet(unittest.TestCase):
1515
1516 def test_get_media(self):
1517 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001518 zoo = build('zoo', 'v1', http=http)
Joe Gregorio708388c2012-06-15 13:43:04 -04001519 request = zoo.animals().get_media(name='Lion')
1520
Pat Ferated5b61bd2015-03-03 16:04:11 -08001521 parsed = urlparse(request.uri)
Joe Gregorio708388c2012-06-15 13:43:04 -04001522 q = parse_qs(parsed[4])
1523 self.assertEqual(q['alt'], ['media'])
1524 self.assertEqual(request.headers['accept'], '*/*')
1525
1526 http = HttpMockSequence([
1527 ({'status': '200'}, 'standing in for media'),
1528 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001529 response = request.execute(http=http)
INADA Naoki09157612015-03-25 01:51:03 +09001530 self.assertEqual(b'standing in for media', response)
Joe Gregorio708388c2012-06-15 13:43:04 -04001531
1532
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001533if __name__ == '__main__':
1534 unittest.main()