blob: 941cfb0991961ffb51afb53ada60ac6a00dac7a6 [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/",
452 credentials=self.MOCK_CREDENTIALS)
453 # 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 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400490 zoo = build('zoo', 'v1', http=http, developerKey='foo',
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
Ethan Bao12b7cd32016-03-14 14:25:10 -0700509 def test_discovery_loading_from_v2_discovery_uri(self):
510 http = HttpMockSequence([
511 ({'status': '404'}, 'Not found'),
512 ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()),
513 ])
514 zoo = build('zoo', 'v1', http=http, cache_discovery=False)
515 self.assertTrue(hasattr(zoo, 'animals'))
Joe Gregorioa98733f2011-09-16 10:12:28 -0400516
Takashi Matsuo30125122015-08-19 11:42:32 -0700517class DiscoveryFromAppEngineCache(unittest.TestCase):
518 def test_appengine_memcache(self):
519 # Hack module import
520 self.orig_import = __import__
521 self.mocked_api = mock.MagicMock()
522
eesheeshc6425a02016-02-12 15:07:06 +0000523 def import_mock(name, *args, **kwargs):
Takashi Matsuo30125122015-08-19 11:42:32 -0700524 if name == 'google.appengine.api':
525 return self.mocked_api
eesheeshc6425a02016-02-12 15:07:06 +0000526 return self.orig_import(name, *args, **kwargs)
Takashi Matsuo30125122015-08-19 11:42:32 -0700527
528 import_fullname = '__builtin__.__import__'
529 if sys.version_info[0] >= 3:
530 import_fullname = 'builtins.__import__'
531
532 with mock.patch(import_fullname, side_effect=import_mock):
533 namespace = 'google-api-client'
534 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
535
536 self.mocked_api.memcache.get.return_value = None
537
538 plus = build('plus', 'v1', http=self.http)
539
540 # memcache.get is called once
541 url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
542 self.mocked_api.memcache.get.assert_called_once_with(url,
543 namespace=namespace)
544
545 # memcache.set is called once
546 with open(datafile('plus.json')) as f:
547 content = f.read()
548 self.mocked_api.memcache.set.assert_called_once_with(
549 url, content, time=DISCOVERY_DOC_MAX_AGE, namespace=namespace)
550
551 # Returns the cached content this time.
552 self.mocked_api.memcache.get.return_value = content
553
554 # Make sure the contents are returned from the cache.
555 # (Otherwise it should through an error)
556 self.http = HttpMock(None, {'status': '200'})
557
558 plus = build('plus', 'v1', http=self.http)
559
560 # memcache.get is called twice
561 self.mocked_api.memcache.get.assert_has_calls(
562 [mock.call(url, namespace=namespace),
563 mock.call(url, namespace=namespace)])
564
565 # memcahce.set is called just once
566 self.mocked_api.memcache.set.assert_called_once_with(
567 url, content, time=DISCOVERY_DOC_MAX_AGE,namespace=namespace)
568
569
570class DictCache(Cache):
571 def __init__(self):
572 self.d = {}
573 def get(self, url):
574 return self.d.get(url, None)
575 def set(self, url, content):
576 self.d[url] = content
577 def contains(self, url):
578 return url in self.d
579
580
581class DiscoveryFromFileCache(unittest.TestCase):
582 def test_file_based_cache(self):
583 cache = mock.Mock(wraps=DictCache())
Jon Wayne Parrott36d4e1b2016-10-17 13:31:33 -0700584 with mock.patch('googleapiclient.discovery_cache.autodetect',
585 return_value=cache):
Takashi Matsuo30125122015-08-19 11:42:32 -0700586 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
587
588 plus = build('plus', 'v1', http=self.http)
589
590 # cache.get is called once
591 url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
592 cache.get.assert_called_once_with(url)
593
594 # cache.set is called once
595 with open(datafile('plus.json')) as f:
596 content = f.read()
597 cache.set.assert_called_once_with(url, content)
598
599 # Make sure there is a cache entry for the plus v1 discovery doc.
600 self.assertTrue(cache.contains(url))
601
602 # Make sure the contents are returned from the cache.
603 # (Otherwise it should through an error)
604 self.http = HttpMock(None, {'status': '200'})
605
606 plus = build('plus', 'v1', http=self.http)
607
608 # cache.get is called twice
609 cache.get.assert_has_calls([mock.call(url), mock.call(url)])
610
611 # cahce.set is called just once
612 cache.set.assert_called_once_with(url, content)
613
614
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400615class Discovery(unittest.TestCase):
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400616
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400617 def test_method_error_checking(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -0500618 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400619 plus = build('plus', 'v1', http=self.http)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400620
621 # Missing required parameters
622 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500623 plus.activities().list()
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400624 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900625 except TypeError as e:
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400626 self.assertTrue('Missing' in str(e))
627
Joe Gregorio2467afa2012-06-20 12:21:25 -0400628 # Missing required parameters even if supplied as None.
629 try:
630 plus.activities().list(collection=None, userId=None)
631 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900632 except TypeError as e:
Joe Gregorio2467afa2012-06-20 12:21:25 -0400633 self.assertTrue('Missing' in str(e))
634
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400635 # Parameter doesn't match regex
636 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500637 plus.activities().list(collection='not_a_collection_name', userId='me')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400638 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900639 except TypeError as e:
Joe Gregorioca876e42011-02-22 19:39:42 -0500640 self.assertTrue('not an allowed value' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400641
642 # Unexpected parameter
643 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500644 plus.activities().list(flubber=12)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400645 self.fail()
INADA Naokic1505df2014-08-20 15:19:53 +0900646 except TypeError as e:
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400647 self.assertTrue('unexpected' in str(e))
648
Joe Gregoriobee86832011-02-22 10:00:19 -0500649 def _check_query_types(self, request):
Pat Ferated5b61bd2015-03-03 16:04:11 -0800650 parsed = urlparse(request.uri)
Joe Gregoriobee86832011-02-22 10:00:19 -0500651 q = parse_qs(parsed[4])
652 self.assertEqual(q['q'], ['foo'])
653 self.assertEqual(q['i'], ['1'])
654 self.assertEqual(q['n'], ['1.0'])
655 self.assertEqual(q['b'], ['false'])
656 self.assertEqual(q['a'], ['[1, 2, 3]'])
657 self.assertEqual(q['o'], ['{\'a\': 1}'])
658 self.assertEqual(q['e'], ['bar'])
659
660 def test_type_coercion(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400661 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400662 zoo = build('zoo', 'v1', http=http)
Joe Gregoriobee86832011-02-22 10:00:19 -0500663
Joe Gregorioa98733f2011-09-16 10:12:28 -0400664 request = zoo.query(
665 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 -0500666 self._check_query_types(request)
Joe Gregorioa98733f2011-09-16 10:12:28 -0400667 request = zoo.query(
668 q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar')
Joe Gregoriobee86832011-02-22 10:00:19 -0500669 self._check_query_types(request)
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500670
Joe Gregorioa98733f2011-09-16 10:12:28 -0400671 request = zoo.query(
Craig Citro1e742822012-03-01 12:59:22 -0800672 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 -0500673
674 request = zoo.query(
Craig Citro1e742822012-03-01 12:59:22 -0800675 q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar',
676 er=['one', 'three'], rr=['foo', 'bar'])
Joe Gregoriobee86832011-02-22 10:00:19 -0500677 self._check_query_types(request)
678
Craig Citro1e742822012-03-01 12:59:22 -0800679 # Five is right out.
Joe Gregorio20c26e52012-03-02 15:58:31 -0500680 self.assertRaises(TypeError, zoo.query, er=['one', 'five'])
Craig Citro1e742822012-03-01 12:59:22 -0800681
Joe Gregorio13217952011-02-22 15:37:38 -0500682 def test_optional_stack_query_parameters(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400683 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400684 zoo = build('zoo', 'v1', http=http)
Joe Gregoriof4153422011-03-18 22:45:18 -0400685 request = zoo.query(trace='html', fields='description')
Joe Gregorio13217952011-02-22 15:37:38 -0500686
Pat Ferated5b61bd2015-03-03 16:04:11 -0800687 parsed = urlparse(request.uri)
Joe Gregorioca876e42011-02-22 19:39:42 -0500688 q = parse_qs(parsed[4])
689 self.assertEqual(q['trace'], ['html'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400690 self.assertEqual(q['fields'], ['description'])
691
Joe Gregorio2467afa2012-06-20 12:21:25 -0400692 def test_string_params_value_of_none_get_dropped(self):
Joe Gregorio4b4002f2012-06-14 15:41:01 -0400693 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400694 zoo = build('zoo', 'v1', http=http)
Joe Gregorio2467afa2012-06-20 12:21:25 -0400695 request = zoo.query(trace=None, fields='description')
696
Pat Ferated5b61bd2015-03-03 16:04:11 -0800697 parsed = urlparse(request.uri)
Joe Gregorio2467afa2012-06-20 12:21:25 -0400698 q = parse_qs(parsed[4])
699 self.assertFalse('trace' in q)
Joe Gregorio4b4002f2012-06-14 15:41:01 -0400700
Joe Gregorioe08a1662011-12-07 09:48:22 -0500701 def test_model_added_query_parameters(self):
702 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400703 zoo = build('zoo', 'v1', http=http)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500704 request = zoo.animals().get(name='Lion')
705
Pat Ferated5b61bd2015-03-03 16:04:11 -0800706 parsed = urlparse(request.uri)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500707 q = parse_qs(parsed[4])
708 self.assertEqual(q['alt'], ['json'])
709 self.assertEqual(request.headers['accept'], 'application/json')
710
711 def test_fallback_to_raw_model(self):
712 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400713 zoo = build('zoo', 'v1', http=http)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500714 request = zoo.animals().getmedia(name='Lion')
715
Pat Ferated5b61bd2015-03-03 16:04:11 -0800716 parsed = urlparse(request.uri)
Joe Gregorioe08a1662011-12-07 09:48:22 -0500717 q = parse_qs(parsed[4])
718 self.assertTrue('alt' not in q)
719 self.assertEqual(request.headers['accept'], '*/*')
720
Joe Gregoriof4153422011-03-18 22:45:18 -0400721 def test_patch(self):
722 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400723 zoo = build('zoo', 'v1', http=http)
Joe Gregoriof4153422011-03-18 22:45:18 -0400724 request = zoo.animals().patch(name='lion', body='{"description": "foo"}')
725
726 self.assertEqual(request.method, 'PATCH')
727
Pepper Lebeck-Jobe860836f2015-06-12 20:42:23 -0400728 def test_batch_request_from_discovery(self):
729 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
730 # zoo defines a batchPath
731 zoo = build('zoo', 'v1', http=self.http)
732 batch_request = zoo.new_batch_http_request()
733 self.assertEqual(batch_request._batch_uri,
734 "https://www.googleapis.com/batchZoo")
735
736 def test_batch_request_from_default(self):
737 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
738 # plus does not define a batchPath
739 plus = build('plus', 'v1', http=self.http)
740 batch_request = plus.new_batch_http_request()
741 self.assertEqual(batch_request._batch_uri,
742 "https://www.googleapis.com/batch")
743
Joe Gregoriof4153422011-03-18 22:45:18 -0400744 def test_tunnel_patch(self):
745 http = HttpMockSequence([
Joe Gregorio79daca02013-03-29 16:25:52 -0400746 ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()),
Joe Gregoriof4153422011-03-18 22:45:18 -0400747 ({'status': '200'}, 'echo_request_headers_as_json'),
748 ])
749 http = tunnel_patch(http)
Takashi Matsuo30125122015-08-19 11:42:32 -0700750 zoo = build('zoo', 'v1', http=http, cache_discovery=False)
Joe Gregorioa98733f2011-09-16 10:12:28 -0400751 resp = zoo.animals().patch(
752 name='lion', body='{"description": "foo"}').execute()
Joe Gregoriof4153422011-03-18 22:45:18 -0400753
754 self.assertTrue('x-http-method-override' in resp)
Joe Gregorioca876e42011-02-22 19:39:42 -0500755
Joe Gregorio7b70f432011-11-09 10:18:51 -0500756 def test_plus_resources(self):
757 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400758 plus = build('plus', 'v1', http=self.http)
Joe Gregorio7b70f432011-11-09 10:18:51 -0500759 self.assertTrue(getattr(plus, 'activities'))
760 self.assertTrue(getattr(plus, 'people'))
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400761
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800762 def test_oauth2client_credentials(self):
763 credentials = mock.Mock(spec=GoogleCredentials)
764 credentials.create_scoped_required.return_value = False
Orest Bolohane92c9002014-05-30 11:15:43 -0700765
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800766 discovery = open(datafile('plus.json')).read()
767 service = build_from_document(discovery, credentials=credentials)
768 self.assertEqual(service._http, credentials.authorize.return_value)
Orest Bolohane92c9002014-05-30 11:15:43 -0700769
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800770 def test_google_auth_credentials(self):
771 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
772 discovery = open(datafile('plus.json')).read()
773 service = build_from_document(discovery, credentials=credentials)
774
775 self.assertIsInstance(service._http, google_auth_httplib2.AuthorizedHttp)
776 self.assertEqual(service._http.credentials, credentials)
777
778 def test_no_scopes_no_credentials(self):
779 # Zoo doesn't have scopes
780 discovery = open(datafile('zoo.json')).read()
781 service = build_from_document(discovery)
782 # Should be an ordinary httplib2.Http instance and not AuthorizedHttp.
783 self.assertIsInstance(service._http, httplib2.Http)
Orest Bolohane92c9002014-05-30 11:15:43 -0700784
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400785 def test_full_featured(self):
786 # Zoo should exercise all discovery facets
787 # and should also have no future.json file.
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500788 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400789 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400790 self.assertTrue(getattr(zoo, 'animals'))
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500791
Joe Gregoriof4153422011-03-18 22:45:18 -0400792 request = zoo.animals().list(name='bat', projection="full")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800793 parsed = urlparse(request.uri)
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400794 q = parse_qs(parsed[4])
795 self.assertEqual(q['name'], ['bat'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400796 self.assertEqual(q['projection'], ['full'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400797
Joe Gregorio3fada332011-01-07 17:07:45 -0500798 def test_nested_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500799 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400800 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio3fada332011-01-07 17:07:45 -0500801 self.assertTrue(getattr(zoo, 'animals'))
802 request = zoo.my().favorites().list(max_results="5")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800803 parsed = urlparse(request.uri)
Joe Gregorio3fada332011-01-07 17:07:45 -0500804 q = parse_qs(parsed[4])
805 self.assertEqual(q['max-results'], ['5'])
806
Pat Feratec6050872015-03-03 18:24:59 -0800807 @unittest.skipIf(six.PY3, 'print is not a reserved name in Python 3')
Joe Gregoriod92897c2011-07-07 11:44:56 -0400808 def test_methods_with_reserved_names(self):
809 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400810 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod92897c2011-07-07 11:44:56 -0400811 self.assertTrue(getattr(zoo, 'animals'))
812 request = zoo.global_().print_().assert_(max_results="5")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800813 parsed = urlparse(request.uri)
Joe Gregorioa2838152012-07-16 11:52:17 -0400814 self.assertEqual(parsed[2], '/zoo/v1/global/print/assert')
Joe Gregoriod92897c2011-07-07 11:44:56 -0400815
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500816 def test_top_level_functions(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500817 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400818 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500819 self.assertTrue(getattr(zoo, 'query'))
820 request = zoo.query(q="foo")
Pat Ferated5b61bd2015-03-03 16:04:11 -0800821 parsed = urlparse(request.uri)
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500822 q = parse_qs(parsed[4])
823 self.assertEqual(q['q'], ['foo'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400824
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400825 def test_simple_media_uploads(self):
826 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400827 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400828 doc = getattr(zoo.animals().insert, '__doc__')
829 self.assertTrue('media_body' in doc)
830
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400831 def test_simple_media_upload_no_max_size_provided(self):
832 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400833 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400834 request = zoo.animals().crossbreed(media_body=datafile('small.png'))
835 self.assertEquals('image/png', request.headers['content-type'])
Pat Ferate2b140222015-03-03 18:05:11 -0800836 self.assertEquals(b'PNG', request.body[1:4])
Joe Gregorio84d3c1f2011-07-25 10:39:45 -0400837
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400838 def test_simple_media_raise_correct_exceptions(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
842 try:
843 zoo.animals().insert(media_body=datafile('smiley.png'))
844 self.fail("should throw exception if media is too large.")
845 except MediaUploadSizeError:
846 pass
847
848 try:
849 zoo.animals().insert(media_body=datafile('small.jpg'))
850 self.fail("should throw exception if mimetype is unacceptable.")
851 except UnacceptableMimeTypeError:
852 pass
853
854 def test_simple_media_good_upload(self):
855 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400856 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400857
858 request = zoo.animals().insert(media_body=datafile('small.png'))
859 self.assertEquals('image/png', request.headers['content-type'])
Pat Ferate2b140222015-03-03 18:05:11 -0800860 self.assertEquals(b'PNG', request.body[1:4])
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500861 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -0400862 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -0500863 request.uri)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400864
Brian J. Watson38051ac2016-10-25 07:53:08 -0700865 def test_simple_media_unknown_mimetype(self):
866 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
867 zoo = build('zoo', 'v1', http=self.http)
868
869 try:
870 zoo.animals().insert(media_body=datafile('small-png'))
871 self.fail("should throw exception if mimetype is unknown.")
872 except UnknownFileType:
873 pass
874
875 request = zoo.animals().insert(media_body=datafile('small-png'),
876 media_mime_type='image/png')
877 self.assertEquals('image/png', request.headers['content-type'])
878 self.assertEquals(b'PNG', request.body[1:4])
879 assertUrisEqual(self,
880 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json',
881 request.uri)
882
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400883 def test_multipart_media_raise_correct_exceptions(self):
884 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400885 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400886
887 try:
888 zoo.animals().insert(media_body=datafile('smiley.png'), body={})
889 self.fail("should throw exception if media is too large.")
890 except MediaUploadSizeError:
891 pass
892
893 try:
894 zoo.animals().insert(media_body=datafile('small.jpg'), body={})
895 self.fail("should throw exception if mimetype is unacceptable.")
896 except UnacceptableMimeTypeError:
897 pass
898
899 def test_multipart_media_good_upload(self):
900 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400901 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400902
903 request = zoo.animals().insert(media_body=datafile('small.png'), body={})
Joe Gregorioa98733f2011-09-16 10:12:28 -0400904 self.assertTrue(request.headers['content-type'].startswith(
905 'multipart/related'))
Phil Ruffwind26178fc2015-10-13 19:00:33 -0400906 with open(datafile('small.png'), 'rb') as f:
907 contents = f.read()
908 boundary = re.match(b'--=+([^=]+)', request.body).group(1)
909 self.assertEqual(
910 request.body.rstrip(b"\n"), # Python 2.6 does not add a trailing \n
911 b'--===============' + boundary + b'==\n' +
912 b'Content-Type: application/json\n' +
913 b'MIME-Version: 1.0\n\n' +
914 b'{"data": {}}\n' +
915 b'--===============' + boundary + b'==\n' +
916 b'Content-Type: image/png\n' +
917 b'MIME-Version: 1.0\n' +
918 b'Content-Transfer-Encoding: binary\n\n' +
919 contents +
920 b'\n--===============' + boundary + b'==--')
Joe Gregoriof1ba7f12012-11-16 15:10:47 -0500921 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -0400922 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=multipart&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -0500923 request.uri)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400924
925 def test_media_capable_method_without_media(self):
926 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400927 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400928
929 request = zoo.animals().insert(body={})
930 self.assertTrue(request.headers['content-type'], 'application/json')
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400931
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500932 def test_resumable_multipart_media_good_upload(self):
933 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400934 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500935
936 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
937 request = zoo.animals().insert(media_body=media_upload, body={})
938 self.assertTrue(request.headers['content-type'].startswith(
Joe Gregorio945be3e2012-01-27 17:01:06 -0500939 'application/json'))
940 self.assertEquals('{"data": {}}', request.body)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500941 self.assertEquals(media_upload, request.resumable)
942
943 self.assertEquals('image/png', request.resumable.mimetype())
944
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500945 self.assertNotEquals(request.body, None)
946 self.assertEquals(request.resumable_uri, None)
947
948 http = HttpMockSequence([
949 ({'status': '200',
950 'location': 'http://upload.example.com'}, ''),
951 ({'status': '308',
Matt Carroll94a53942016-12-20 13:56:43 -0800952 'location': 'http://upload.example.com/2'}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500953 ({'status': '308',
954 'location': 'http://upload.example.com/3',
Matt Carroll94a53942016-12-20 13:56:43 -0800955 'range': '0-12'}, ''),
956 ({'status': '308',
957 'location': 'http://upload.example.com/4',
Joe Gregorio945be3e2012-01-27 17:01:06 -0500958 'range': '0-%d' % (media_upload.size() - 2)}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500959 ({'status': '200'}, '{"foo": "bar"}'),
960 ])
961
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400962 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500963 self.assertEquals(None, body)
964 self.assertTrue(isinstance(status, MediaUploadProgress))
Matt Carroll94a53942016-12-20 13:56:43 -0800965 self.assertEquals(0, status.resumable_progress)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500966
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500967 # Two requests should have been made and the resumable_uri should have been
968 # updated for each one.
969 self.assertEquals(request.resumable_uri, 'http://upload.example.com/2')
Matt Carroll94a53942016-12-20 13:56:43 -0800970 self.assertEquals(media_upload, request.resumable)
971 self.assertEquals(0, request.resumable_progress)
972
973 # This next chuck call should upload the first chunk
974 status, body = request.next_chunk(http=http)
975 self.assertEquals(request.resumable_uri, 'http://upload.example.com/3')
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500976 self.assertEquals(media_upload, request.resumable)
977 self.assertEquals(13, request.resumable_progress)
978
Matt Carroll94a53942016-12-20 13:56:43 -0800979 # This call will upload the next chunk
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400980 status, body = request.next_chunk(http=http)
Matt Carroll94a53942016-12-20 13:56:43 -0800981 self.assertEquals(request.resumable_uri, 'http://upload.example.com/4')
Joe Gregorio945be3e2012-01-27 17:01:06 -0500982 self.assertEquals(media_upload.size()-1, request.resumable_progress)
983 self.assertEquals('{"data": {}}', request.body)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500984
985 # Final call to next_chunk should complete the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400986 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500987 self.assertEquals(body, {"foo": "bar"})
988 self.assertEquals(status, None)
989
990
991 def test_resumable_media_good_upload(self):
992 """Not a multipart upload."""
993 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400994 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500995
996 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
997 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500998 self.assertEquals(media_upload, request.resumable)
999
1000 self.assertEquals('image/png', request.resumable.mimetype())
1001
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001002 self.assertEquals(request.body, None)
1003 self.assertEquals(request.resumable_uri, None)
1004
1005 http = HttpMockSequence([
1006 ({'status': '200',
1007 'location': 'http://upload.example.com'}, ''),
1008 ({'status': '308',
1009 'location': 'http://upload.example.com/2',
1010 'range': '0-12'}, ''),
1011 ({'status': '308',
1012 'location': 'http://upload.example.com/3',
Joe Gregorio945be3e2012-01-27 17:01:06 -05001013 'range': '0-%d' % (media_upload.size() - 2)}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001014 ({'status': '200'}, '{"foo": "bar"}'),
1015 ])
1016
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001017 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001018 self.assertEquals(None, body)
1019 self.assertTrue(isinstance(status, MediaUploadProgress))
1020 self.assertEquals(13, status.resumable_progress)
1021
1022 # Two requests should have been made and the resumable_uri should have been
1023 # updated for each one.
1024 self.assertEquals(request.resumable_uri, 'http://upload.example.com/2')
1025
1026 self.assertEquals(media_upload, request.resumable)
1027 self.assertEquals(13, request.resumable_progress)
1028
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001029 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001030 self.assertEquals(request.resumable_uri, 'http://upload.example.com/3')
Joe Gregorio945be3e2012-01-27 17:01:06 -05001031 self.assertEquals(media_upload.size()-1, request.resumable_progress)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001032 self.assertEquals(request.body, None)
1033
1034 # Final call to next_chunk should complete the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001035 status, body = request.next_chunk(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001036 self.assertEquals(body, {"foo": "bar"})
1037 self.assertEquals(status, None)
1038
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001039 def test_resumable_media_good_upload_from_execute(self):
1040 """Not a multipart upload."""
1041 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001042 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001043
1044 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1045 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregoriof1ba7f12012-11-16 15:10:47 -05001046 assertUrisEqual(self,
Joe Gregorioa2838152012-07-16 11:52:17 -04001047 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=resumable&alt=json',
Joe Gregoriode860442012-03-02 15:55:52 -05001048 request.uri)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001049
1050 http = HttpMockSequence([
1051 ({'status': '200',
1052 'location': 'http://upload.example.com'}, ''),
1053 ({'status': '308',
1054 'location': 'http://upload.example.com/2',
1055 'range': '0-12'}, ''),
1056 ({'status': '308',
1057 'location': 'http://upload.example.com/3',
Joe Gregorio945be3e2012-01-27 17:01:06 -05001058 'range': '0-%d' % media_upload.size()}, ''),
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001059 ({'status': '200'}, '{"foo": "bar"}'),
1060 ])
1061
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001062 body = request.execute(http=http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001063 self.assertEquals(body, {"foo": "bar"})
1064
1065 def test_resumable_media_fail_unknown_response_code_first_request(self):
1066 """Not a multipart upload."""
1067 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001068 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001069
1070 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1071 request = zoo.animals().insert(media_body=media_upload, body=None)
1072
1073 http = HttpMockSequence([
1074 ({'status': '400',
1075 'location': 'http://upload.example.com'}, ''),
1076 ])
1077
Joe Gregoriobaf04802013-03-01 12:27:06 -05001078 try:
1079 request.execute(http=http)
1080 self.fail('Should have raised ResumableUploadError.')
INADA Naokic1505df2014-08-20 15:19:53 +09001081 except ResumableUploadError as e:
Joe Gregoriobaf04802013-03-01 12:27:06 -05001082 self.assertEqual(400, e.resp.status)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001083
1084 def test_resumable_media_fail_unknown_response_code_subsequent_request(self):
1085 """Not a multipart upload."""
1086 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001087 zoo = build('zoo', 'v1', http=self.http)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001088
1089 media_upload = MediaFileUpload(datafile('small.png'), resumable=True)
1090 request = zoo.animals().insert(media_body=media_upload, body=None)
1091
1092 http = HttpMockSequence([
1093 ({'status': '200',
1094 'location': 'http://upload.example.com'}, ''),
1095 ({'status': '400'}, ''),
1096 ])
1097
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001098 self.assertRaises(HttpError, request.execute, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001099 self.assertTrue(request._in_error_state)
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001100
Joe Gregorio910b9b12012-06-12 09:36:30 -04001101 http = HttpMockSequence([
1102 ({'status': '308',
1103 'range': '0-5'}, ''),
1104 ({'status': '308',
1105 'range': '0-6'}, ''),
1106 ])
1107
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001108 status, body = request.next_chunk(http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001109 self.assertEquals(status.resumable_progress, 7,
1110 'Should have first checked length and then tried to PUT more.')
1111 self.assertFalse(request._in_error_state)
1112
1113 # Put it back in an error state.
1114 http = HttpMockSequence([
1115 ({'status': '400'}, ''),
1116 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001117 self.assertRaises(HttpError, request.execute, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001118 self.assertTrue(request._in_error_state)
1119
1120 # Pretend the last request that 400'd actually succeeded.
1121 http = HttpMockSequence([
1122 ({'status': '200'}, '{"foo": "bar"}'),
1123 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001124 status, body = request.next_chunk(http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001125 self.assertEqual(body, {'foo': 'bar'})
1126
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001127 def test_media_io_base_stream_unlimited_chunksize_resume(self):
1128 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1129 zoo = build('zoo', 'v1', http=self.http)
1130
Pat Ferateed9affd2015-03-03 16:03:15 -08001131 # Set up a seekable stream and try to upload in single chunk.
Pat Ferate2b140222015-03-03 18:05:11 -08001132 fd = BytesIO(b'01234"56789"')
Pat Ferateed9affd2015-03-03 16:03:15 -08001133 media_upload = MediaIoBaseUpload(
1134 fd=fd, mimetype='text/plain', chunksize=-1, resumable=True)
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001135
Pat Ferateed9affd2015-03-03 16:03:15 -08001136 request = zoo.animals().insert(media_body=media_upload, body=None)
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001137
Pat Ferateed9affd2015-03-03 16:03:15 -08001138 # The single chunk fails, restart at the right point.
1139 http = HttpMockSequence([
1140 ({'status': '200',
1141 'location': 'http://upload.example.com'}, ''),
1142 ({'status': '308',
1143 'location': 'http://upload.example.com/2',
1144 'range': '0-4'}, ''),
1145 ({'status': '200'}, 'echo_request_body'),
1146 ])
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001147
Pat Ferateed9affd2015-03-03 16:03:15 -08001148 body = request.execute(http=http)
1149 self.assertEqual('56789', body)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001150
1151 def test_media_io_base_stream_chunksize_resume(self):
1152 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1153 zoo = build('zoo', 'v1', http=self.http)
1154
Pat Ferateed9affd2015-03-03 16:03:15 -08001155 # Set up a seekable stream and try to upload in chunks.
Pat Ferate2b140222015-03-03 18:05:11 -08001156 fd = BytesIO(b'0123456789')
Pat Ferateed9affd2015-03-03 16:03:15 -08001157 media_upload = MediaIoBaseUpload(
1158 fd=fd, mimetype='text/plain', chunksize=5, resumable=True)
1159
1160 request = zoo.animals().insert(media_body=media_upload, body=None)
1161
1162 # The single chunk fails, pull the content sent out of the exception.
1163 http = HttpMockSequence([
1164 ({'status': '200',
1165 'location': 'http://upload.example.com'}, ''),
1166 ({'status': '400'}, 'echo_request_body'),
1167 ])
1168
Joe Gregorio5c120db2012-08-23 09:13:55 -04001169 try:
Pat Ferateed9affd2015-03-03 16:03:15 -08001170 body = request.execute(http=http)
1171 except HttpError as e:
Pat Ferate2b140222015-03-03 18:05:11 -08001172 self.assertEqual(b'01234', e.content)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001173
Joe Gregorio910b9b12012-06-12 09:36:30 -04001174 def test_resumable_media_handle_uploads_of_unknown_size(self):
1175 http = HttpMockSequence([
1176 ({'status': '200',
1177 'location': 'http://upload.example.com'}, ''),
1178 ({'status': '200'}, 'echo_request_headers_as_json'),
1179 ])
1180
1181 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001182 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001183
Joe Gregorio910b9b12012-06-12 09:36:30 -04001184 # Create an upload that doesn't know the full size of the media.
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001185 class IoBaseUnknownLength(MediaUpload):
1186 def chunksize(self):
1187 return 10
1188
1189 def mimetype(self):
1190 return 'image/png'
1191
1192 def size(self):
1193 return None
1194
1195 def resumable(self):
1196 return True
1197
1198 def getbytes(self, begin, length):
1199 return '0123456789'
1200
1201 upload = IoBaseUnknownLength()
Joe Gregorio910b9b12012-06-12 09:36:30 -04001202
1203 request = zoo.animals().insert(media_body=upload, body=None)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001204 status, body = request.next_chunk(http=http)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001205 self.assertEqual(body, {
1206 'Content-Range': 'bytes 0-9/*',
1207 'Content-Length': '10',
1208 })
Joe Gregorio44454e42012-06-15 08:38:53 -04001209
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001210 def test_resumable_media_no_streaming_on_unsupported_platforms(self):
1211 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1212 zoo = build('zoo', 'v1', http=self.http)
1213
1214 class IoBaseHasStream(MediaUpload):
1215 def chunksize(self):
1216 return 10
1217
1218 def mimetype(self):
1219 return 'image/png'
1220
1221 def size(self):
1222 return None
1223
1224 def resumable(self):
1225 return True
1226
1227 def getbytes(self, begin, length):
1228 return '0123456789'
1229
1230 def has_stream(self):
1231 return True
1232
1233 def stream(self):
1234 raise NotImplementedError()
1235
1236 upload = IoBaseHasStream()
1237
1238 orig_version = sys.version_info
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001239
1240 sys.version_info = (2, 6, 5, 'final', 0)
1241
1242 request = zoo.animals().insert(media_body=upload, body=None)
1243
1244 # This should raise an exception because stream() will be called.
1245 http = HttpMockSequence([
1246 ({'status': '200',
1247 'location': 'http://upload.example.com'}, ''),
1248 ({'status': '200'}, 'echo_request_headers_as_json'),
1249 ])
1250
1251 self.assertRaises(NotImplementedError, request.next_chunk, http=http)
1252
1253 sys.version_info = orig_version
1254
Joe Gregorio44454e42012-06-15 08:38:53 -04001255 def test_resumable_media_handle_uploads_of_unknown_size_eof(self):
1256 http = HttpMockSequence([
1257 ({'status': '200',
1258 'location': 'http://upload.example.com'}, ''),
1259 ({'status': '200'}, 'echo_request_headers_as_json'),
1260 ])
1261
1262 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001263 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio44454e42012-06-15 08:38:53 -04001264
Pat Ferate2b140222015-03-03 18:05:11 -08001265 fd = BytesIO(b'data goes here')
Joe Gregorio44454e42012-06-15 08:38:53 -04001266
1267 # Create an upload that doesn't know the full size of the media.
1268 upload = MediaIoBaseUpload(
Joe Gregorio4a2c29f2012-07-12 12:52:47 -04001269 fd=fd, mimetype='image/png', chunksize=15, resumable=True)
Joe Gregorio44454e42012-06-15 08:38:53 -04001270
1271 request = zoo.animals().insert(media_body=upload, body=None)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001272 status, body = request.next_chunk(http=http)
Joe Gregorio5c120db2012-08-23 09:13:55 -04001273 self.assertEqual(body, {
1274 'Content-Range': 'bytes 0-13/14',
1275 'Content-Length': '14',
1276 })
Joe Gregorio910b9b12012-06-12 09:36:30 -04001277
1278 def test_resumable_media_handle_resume_of_upload_of_unknown_size(self):
1279 http = HttpMockSequence([
1280 ({'status': '200',
1281 'location': 'http://upload.example.com'}, ''),
1282 ({'status': '400'}, ''),
1283 ])
1284
1285 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001286 zoo = build('zoo', 'v1', http=self.http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001287
1288 # Create an upload that doesn't know the full size of the media.
Pat Ferate2b140222015-03-03 18:05:11 -08001289 fd = BytesIO(b'data goes here')
Joe Gregorio910b9b12012-06-12 09:36:30 -04001290
1291 upload = MediaIoBaseUpload(
Joe Gregorio4a2c29f2012-07-12 12:52:47 -04001292 fd=fd, mimetype='image/png', chunksize=500, resumable=True)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001293
1294 request = zoo.animals().insert(media_body=upload, body=None)
1295
1296 # Put it in an error state.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001297 self.assertRaises(HttpError, request.next_chunk, http=http)
Joe Gregorio910b9b12012-06-12 09:36:30 -04001298
1299 http = HttpMockSequence([
1300 ({'status': '400',
1301 'range': '0-5'}, 'echo_request_headers_as_json'),
1302 ])
1303 try:
1304 # Should resume the upload by first querying the status of the upload.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001305 request.next_chunk(http=http)
INADA Naokic1505df2014-08-20 15:19:53 +09001306 except HttpError as e:
Joe Gregorio910b9b12012-06-12 09:36:30 -04001307 expected = {
Joe Gregorioc80ac9d2012-08-21 14:09:09 -04001308 'Content-Range': 'bytes */14',
Joe Gregorio910b9b12012-06-12 09:36:30 -04001309 'content-length': '0'
1310 }
INADA Naoki09157612015-03-25 01:51:03 +09001311 self.assertEqual(expected, json.loads(e.content.decode('utf-8')),
Joe Gregorio910b9b12012-06-12 09:36:30 -04001312 'Should send an empty body when requesting the current upload status.')
Joe Gregoriod0bd3882011-11-22 09:49:47 -05001313
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001314 def test_pickle(self):
1315 sorted_resource_keys = ['_baseUrl',
1316 '_developerKey',
1317 '_dynamic_attrs',
1318 '_http',
1319 '_model',
1320 '_requestBuilder',
1321 '_resourceDesc',
1322 '_rootDesc',
1323 '_schema',
1324 'animals',
1325 'global_',
1326 'load',
1327 'loadNoTemplate',
1328 'my',
Pepper Lebeck-Jobe860836f2015-06-12 20:42:23 -04001329 'new_batch_http_request',
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001330 'query',
1331 'scopedAnimals']
1332
1333 http = HttpMock(datafile('zoo.json'), {'status': '200'})
1334 zoo = build('zoo', 'v1', http=http)
1335 self.assertEqual(sorted(zoo.__dict__.keys()), sorted_resource_keys)
1336
1337 pickled_zoo = pickle.dumps(zoo)
1338 new_zoo = pickle.loads(pickled_zoo)
1339 self.assertEqual(sorted(new_zoo.__dict__.keys()), sorted_resource_keys)
1340 self.assertTrue(hasattr(new_zoo, 'animals'))
1341 self.assertTrue(callable(new_zoo.animals))
1342 self.assertTrue(hasattr(new_zoo, 'global_'))
1343 self.assertTrue(callable(new_zoo.global_))
1344 self.assertTrue(hasattr(new_zoo, 'load'))
1345 self.assertTrue(callable(new_zoo.load))
1346 self.assertTrue(hasattr(new_zoo, 'loadNoTemplate'))
1347 self.assertTrue(callable(new_zoo.loadNoTemplate))
1348 self.assertTrue(hasattr(new_zoo, 'my'))
1349 self.assertTrue(callable(new_zoo.my))
1350 self.assertTrue(hasattr(new_zoo, 'query'))
1351 self.assertTrue(callable(new_zoo.query))
1352 self.assertTrue(hasattr(new_zoo, 'scopedAnimals'))
1353 self.assertTrue(callable(new_zoo.scopedAnimals))
1354
Joe Gregorio003b6e42013-02-13 15:42:19 -05001355 self.assertEqual(sorted(zoo._dynamic_attrs), sorted(new_zoo._dynamic_attrs))
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001356 self.assertEqual(zoo._baseUrl, new_zoo._baseUrl)
1357 self.assertEqual(zoo._developerKey, new_zoo._developerKey)
1358 self.assertEqual(zoo._requestBuilder, new_zoo._requestBuilder)
1359 self.assertEqual(zoo._resourceDesc, new_zoo._resourceDesc)
1360 self.assertEqual(zoo._rootDesc, new_zoo._rootDesc)
1361 # _http, _model and _schema won't be equal since we will get new
1362 # instances upon un-pickling
1363
1364 def _dummy_zoo_request(self):
1365 with open(os.path.join(DATA_DIR, 'zoo.json'), 'rU') as fh:
1366 zoo_contents = fh.read()
1367
1368 zoo_uri = uritemplate.expand(DISCOVERY_URI,
1369 {'api': 'zoo', 'apiVersion': 'v1'})
1370 if 'REMOTE_ADDR' in os.environ:
Joe Gregorio79daca02013-03-29 16:25:52 -04001371 zoo_uri = util._add_query_parameter(zoo_uri, 'userIp',
1372 os.environ['REMOTE_ADDR'])
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001373
Igor Maravić22435292017-01-19 22:28:22 +01001374 http = build_http()
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001375 original_request = http.request
1376 def wrapped_request(uri, method='GET', *args, **kwargs):
1377 if uri == zoo_uri:
1378 return httplib2.Response({'status': '200'}), zoo_contents
1379 return original_request(uri, method=method, *args, **kwargs)
1380 http.request = wrapped_request
1381 return http
1382
1383 def _dummy_token(self):
1384 access_token = 'foo'
1385 client_id = 'some_client_id'
1386 client_secret = 'cOuDdkfjxxnv+'
1387 refresh_token = '1/0/a.df219fjls0'
1388 token_expiry = datetime.datetime.utcnow()
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001389 user_agent = 'refresh_checker/1.0'
1390 return OAuth2Credentials(
1391 access_token, client_id, client_secret,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -08001392 refresh_token, token_expiry, GOOGLE_TOKEN_URI,
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001393 user_agent)
1394
Joe Gregoriodc106fc2012-11-20 14:30:14 -05001395 def test_pickle_with_credentials(self):
1396 credentials = self._dummy_token()
1397 http = self._dummy_zoo_request()
1398 http = credentials.authorize(http)
1399 self.assertTrue(hasattr(http.request, 'credentials'))
1400
1401 zoo = build('zoo', 'v1', http=http)
1402 pickled_zoo = pickle.dumps(zoo)
1403 new_zoo = pickle.loads(pickled_zoo)
1404 self.assertEqual(sorted(zoo.__dict__.keys()),
1405 sorted(new_zoo.__dict__.keys()))
1406 new_http = new_zoo._http
1407 self.assertFalse(hasattr(new_http.request, 'credentials'))
1408
andrewnestera4a44cf2017-03-31 16:09:31 +03001409 def test_resumable_media_upload_no_content(self):
1410 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
1411 zoo = build('zoo', 'v1', http=self.http)
1412
1413 media_upload = MediaFileUpload(datafile('empty'), resumable=True)
1414 request = zoo.animals().insert(media_body=media_upload, body=None)
1415
1416 self.assertEquals(media_upload, request.resumable)
1417 self.assertEquals(request.body, None)
1418 self.assertEquals(request.resumable_uri, None)
1419
1420 http = HttpMockSequence([
1421 ({'status': '200',
1422 'location': 'http://upload.example.com'}, ''),
1423 ({'status': '308',
1424 'location': 'http://upload.example.com/2',
1425 'range': '0-0'}, ''),
1426 ])
1427
1428 status, body = request.next_chunk(http=http)
1429 self.assertEquals(None, body)
1430 self.assertTrue(isinstance(status, MediaUploadProgress))
1431 self.assertEquals(0, status.progress())
1432
Joe Gregorio708388c2012-06-15 13:43:04 -04001433
Joe Gregorioc5c5a372010-09-22 11:42:32 -04001434class Next(unittest.TestCase):
Joe Gregorio00cf1d92010-09-27 09:22:03 -04001435
Joe Gregorio3c676f92011-07-25 10:38:14 -04001436 def test_next_successful_none_on_no_next_page_token(self):
1437 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001438 tasks = build('tasks', 'v1', http=self.http)
Joe Gregorio3c676f92011-07-25 10:38:14 -04001439 request = tasks.tasklists().list()
1440 self.assertEqual(None, tasks.tasklists().list_next(request, {}))
1441
Son Dinh2a9a2132015-07-23 16:30:56 +00001442 def test_next_successful_none_on_empty_page_token(self):
1443 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
1444 tasks = build('tasks', 'v1', http=self.http)
1445 request = tasks.tasklists().list()
1446 next_request = tasks.tasklists().list_next(
1447 request, {'nextPageToken': ''})
1448 self.assertEqual(None, next_request)
1449
Joe Gregorio3c676f92011-07-25 10:38:14 -04001450 def test_next_successful_with_next_page_token(self):
1451 self.http = HttpMock(datafile('tasks.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001452 tasks = build('tasks', 'v1', http=self.http)
Joe Gregorio3c676f92011-07-25 10:38:14 -04001453 request = tasks.tasklists().list()
Joe Gregorioa98733f2011-09-16 10:12:28 -04001454 next_request = tasks.tasklists().list_next(
1455 request, {'nextPageToken': '123abc'})
Pat Ferated5b61bd2015-03-03 16:04:11 -08001456 parsed = list(urlparse(next_request.uri))
Joe Gregorio3c676f92011-07-25 10:38:14 -04001457 q = parse_qs(parsed[4])
1458 self.assertEqual(q['pageToken'][0], '123abc')
1459
Thomas Coffee20af04d2017-02-10 15:24:44 -08001460 def test_next_successful_with_next_page_token_alternate_name(self):
1461 self.http = HttpMock(datafile('bigquery.json'), {'status': '200'})
1462 bigquery = build('bigquery', 'v2', http=self.http)
1463 request = bigquery.tabledata().list(datasetId='', projectId='', tableId='')
1464 next_request = bigquery.tabledata().list_next(
1465 request, {'pageToken': '123abc'})
1466 parsed = list(urlparse(next_request.uri))
1467 q = parse_qs(parsed[4])
1468 self.assertEqual(q['pageToken'][0], '123abc')
1469
1470 def test_next_successful_with_next_page_token_in_body(self):
1471 self.http = HttpMock(datafile('logging.json'), {'status': '200'})
1472 logging = build('logging', 'v2', http=self.http)
1473 request = logging.entries().list(body={})
1474 next_request = logging.entries().list_next(
1475 request, {'nextPageToken': '123abc'})
1476 body = JsonModel().deserialize(next_request.body)
1477 self.assertEqual(body['pageToken'], '123abc')
1478
Joe Gregorio555f33c2011-08-19 14:56:07 -04001479 def test_next_with_method_with_no_properties(self):
1480 self.http = HttpMock(datafile('latitude.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001481 service = build('latitude', 'v1', http=self.http)
Thomas Coffee20af04d2017-02-10 15:24:44 -08001482 service.currentLocation().get()
1483
1484 def test_next_nonexistent_with_no_next_page_token(self):
1485 self.http = HttpMock(datafile('drive.json'), {'status': '200'})
1486 drive = build('drive', 'v3', http=self.http)
1487 drive.changes().watch(body={})
1488 self.assertFalse(callable(getattr(drive.changes(), 'watch_next', None)))
1489
1490 def test_next_successful_with_next_page_token_required(self):
1491 self.http = HttpMock(datafile('drive.json'), {'status': '200'})
1492 drive = build('drive', 'v3', http=self.http)
1493 request = drive.changes().list(pageToken='startPageToken')
1494 next_request = drive.changes().list_next(
1495 request, {'nextPageToken': '123abc'})
1496 parsed = list(urlparse(next_request.uri))
1497 q = parse_qs(parsed[4])
1498 self.assertEqual(q['pageToken'][0], '123abc')
Joe Gregorio00cf1d92010-09-27 09:22:03 -04001499
Joe Gregorioa98733f2011-09-16 10:12:28 -04001500
Joe Gregorio708388c2012-06-15 13:43:04 -04001501class MediaGet(unittest.TestCase):
1502
1503 def test_get_media(self):
1504 http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001505 zoo = build('zoo', 'v1', http=http)
Joe Gregorio708388c2012-06-15 13:43:04 -04001506 request = zoo.animals().get_media(name='Lion')
1507
Pat Ferated5b61bd2015-03-03 16:04:11 -08001508 parsed = urlparse(request.uri)
Joe Gregorio708388c2012-06-15 13:43:04 -04001509 q = parse_qs(parsed[4])
1510 self.assertEqual(q['alt'], ['media'])
1511 self.assertEqual(request.headers['accept'], '*/*')
1512
1513 http = HttpMockSequence([
1514 ({'status': '200'}, 'standing in for media'),
1515 ])
Joe Gregorio68a8cfe2012-08-03 16:17:40 -04001516 response = request.execute(http=http)
INADA Naoki09157612015-03-25 01:51:03 +09001517 self.assertEqual(b'standing in for media', response)
Joe Gregorio708388c2012-06-15 13:43:04 -04001518
1519
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001520if __name__ == '__main__':
1521 unittest.main()