Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 3 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 4 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 5 | # |
| 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 18 | |
| 19 | """Discovery document tests |
| 20 | |
| 21 | Unit tests for objects created from discovery documents. |
| 22 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 23 | from __future__ import absolute_import |
| 24 | import six |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 25 | |
| 26 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 27 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 28 | from six import BytesIO, StringIO |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 29 | from six.moves.urllib.parse import urlparse, parse_qs |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 30 | |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 31 | import copy |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 32 | import datetime |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 33 | import httplib2 |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 34 | import itertools |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 35 | import json |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 36 | import os |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 37 | import pickle |
Phil Ruffwind | 26178fc | 2015-10-13 19:00:33 -0400 | [diff] [blame] | 38 | import re |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 39 | import sys |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 40 | import unittest2 as unittest |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 41 | |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 42 | import mock |
| 43 | |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 44 | import google.auth.credentials |
| 45 | import google_auth_httplib2 |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 46 | from googleapiclient.discovery import _fix_up_media_upload |
| 47 | from googleapiclient.discovery import _fix_up_method_description |
| 48 | from googleapiclient.discovery import _fix_up_parameters |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 49 | from googleapiclient.discovery import _urljoin |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 50 | from googleapiclient.discovery import build |
| 51 | from googleapiclient.discovery import build_from_document |
| 52 | from googleapiclient.discovery import DISCOVERY_URI |
| 53 | from googleapiclient.discovery import key2param |
| 54 | from googleapiclient.discovery import MEDIA_BODY_PARAMETER_DEFAULT_VALUE |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 55 | from googleapiclient.discovery import MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 56 | from googleapiclient.discovery import ResourceMethodParameters |
| 57 | from googleapiclient.discovery import STACK_QUERY_PARAMETERS |
| 58 | from googleapiclient.discovery import STACK_QUERY_PARAMETER_DEFAULT_VALUE |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 59 | from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE |
| 60 | from googleapiclient.discovery_cache.base import Cache |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 61 | from googleapiclient.errors import HttpError |
| 62 | from googleapiclient.errors import InvalidJsonError |
| 63 | from googleapiclient.errors import MediaUploadSizeError |
| 64 | from googleapiclient.errors import ResumableUploadError |
| 65 | from googleapiclient.errors import UnacceptableMimeTypeError |
Takashi Matsuo | 3772f9d | 2015-09-04 12:25:55 -0700 | [diff] [blame] | 66 | from googleapiclient.errors import UnknownApiNameOrVersion |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 67 | from googleapiclient.errors import UnknownFileType |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 68 | from googleapiclient.http import build_http |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 69 | from googleapiclient.http import BatchHttpRequest |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 70 | from googleapiclient.http import HttpMock |
| 71 | from googleapiclient.http import HttpMockSequence |
| 72 | from googleapiclient.http import MediaFileUpload |
| 73 | from googleapiclient.http import MediaIoBaseUpload |
| 74 | from googleapiclient.http import MediaUpload |
| 75 | from googleapiclient.http import MediaUploadProgress |
| 76 | from googleapiclient.http import tunnel_patch |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 77 | from googleapiclient.model import JsonModel |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 78 | from googleapiclient.schema import Schemas |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 79 | from oauth2client import GOOGLE_TOKEN_URI |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 80 | from oauth2client.client import OAuth2Credentials, GoogleCredentials |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 81 | |
Helen Koike | de13e3b | 2018-04-26 16:05:16 -0300 | [diff] [blame] | 82 | from googleapiclient import _helpers as util |
Jon Wayne Parrott | 36d4e1b | 2016-10-17 13:31:33 -0700 | [diff] [blame] | 83 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 84 | import uritemplate |
| 85 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 86 | |
| 87 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 88 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 89 | |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 90 | def assertUrisEqual(testcase, expected, actual): |
| 91 | """Test that URIs are the same, up to reordering of query parameters.""" |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 92 | expected = urlparse(expected) |
| 93 | actual = urlparse(actual) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 94 | 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 Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 101 | for name in list(expected_query.keys()): |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 102 | testcase.assertEqual(expected_query[name], actual_query[name]) |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 103 | for name in list(actual_query.keys()): |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 104 | testcase.assertEqual(expected_query[name], actual_query[name]) |
| 105 | |
| 106 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 107 | def datafile(filename): |
| 108 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 109 | |
| 110 | |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 111 | class SetupHttplib2(unittest.TestCase): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 112 | |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 113 | def test_retries(self): |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 114 | # Merely loading googleapiclient.discovery should set the RETRIES to 1. |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 115 | self.assertEqual(1, httplib2.RETRIES) |
| 116 | |
| 117 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 118 | class Utilities(unittest.TestCase): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 119 | |
| 120 | def setUp(self): |
| 121 | with open(datafile('zoo.json'), 'r') as fh: |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 122 | self.zoo_root_desc = json.loads(fh.read()) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 123 | self.zoo_get_method_desc = self.zoo_root_desc['methods']['query'] |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 124 | 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-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 126 | self.zoo_schema = Schemas(self.zoo_root_desc) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 127 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 128 | def test_key2param(self): |
| 129 | self.assertEqual('max_results', key2param('max-results')) |
| 130 | self.assertEqual('x007_bond', key2param('007-bond')) |
| 131 | |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 132 | def _base_fix_up_parameters_test( |
| 133 | self, method_desc, http_method, root_desc, schema): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 134 | 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-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 139 | parameters = _fix_up_parameters(method_desc_copy, root_desc, http_method, |
| 140 | schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 141 | |
| 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 Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 148 | for param_name, value in six.iteritems(root_desc.get('parameters', {})): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 149 | self.assertEqual(value, parameters[param_name]) |
| 150 | |
| 151 | return parameters |
| 152 | |
| 153 | def test_fix_up_parameters_get(self): |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 154 | parameters = self._base_fix_up_parameters_test( |
| 155 | self.zoo_get_method_desc, 'GET', self.zoo_root_desc, self.zoo_schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 156 | # Since http_method is 'GET' |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 157 | self.assertFalse('body' in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 158 | |
| 159 | def test_fix_up_parameters_insert(self): |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 160 | parameters = self._base_fix_up_parameters_test( |
| 161 | self.zoo_insert_method_desc, 'POST', self.zoo_root_desc, self.zoo_schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 162 | body = { |
| 163 | 'description': 'The request body.', |
| 164 | 'type': 'object', |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 165 | '$ref': 'Animal', |
| 166 | } |
| 167 | self.assertEqual(parameters['body'], body) |
| 168 | |
| 169 | def test_fix_up_parameters_check_body(self): |
| 170 | dummy_root_desc = {} |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 171 | dummy_schema = { |
| 172 | 'Request': { |
| 173 | 'properties': { |
| 174 | "description": "Required. Dummy parameter.", |
| 175 | "type": "string" |
| 176 | } |
| 177 | } |
| 178 | } |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 179 | no_payload_http_method = 'DELETE' |
| 180 | with_payload_http_method = 'PUT' |
| 181 | |
| 182 | invalid_method_desc = {'response': 'Who cares'} |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 183 | valid_method_desc = { |
| 184 | 'request': { |
| 185 | 'key1': 'value1', |
| 186 | 'key2': 'value2', |
| 187 | '$ref': 'Request' |
| 188 | } |
| 189 | } |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 190 | |
| 191 | parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 192 | no_payload_http_method, dummy_schema) |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 193 | self.assertFalse('body' in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 194 | |
| 195 | parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 196 | no_payload_http_method, dummy_schema) |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 197 | self.assertFalse('body' in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 198 | |
| 199 | parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 200 | with_payload_http_method, dummy_schema) |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 201 | self.assertFalse('body' in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 202 | |
| 203 | parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 204 | with_payload_http_method, dummy_schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 205 | body = { |
| 206 | 'description': 'The request body.', |
| 207 | 'type': 'object', |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 208 | '$ref': 'Request', |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 209 | 'key1': 'value1', |
| 210 | 'key2': 'value2', |
| 211 | } |
| 212 | self.assertEqual(parameters['body'], body) |
| 213 | |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 214 | def test_fix_up_parameters_optional_body(self): |
| 215 | # Request with no parameters |
| 216 | dummy_schema = {'Request': {'properties': {}}} |
| 217 | method_desc = {'request': {'$ref': 'Request'}} |
| 218 | |
| 219 | parameters = _fix_up_parameters(method_desc, {}, 'POST', dummy_schema) |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 220 | |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 221 | def _base_fix_up_method_description_test( |
| 222 | self, method_desc, initial_parameters, final_parameters, |
| 223 | final_accept, final_max_size, final_media_path_url): |
| 224 | fake_root_desc = {'rootUrl': 'http://root/', |
| 225 | 'servicePath': 'fake/'} |
| 226 | fake_path_url = 'fake-path/' |
| 227 | |
| 228 | accept, max_size, media_path_url = _fix_up_media_upload( |
| 229 | method_desc, fake_root_desc, fake_path_url, initial_parameters) |
| 230 | self.assertEqual(accept, final_accept) |
| 231 | self.assertEqual(max_size, final_max_size) |
| 232 | self.assertEqual(media_path_url, final_media_path_url) |
| 233 | self.assertEqual(initial_parameters, final_parameters) |
| 234 | |
| 235 | def test_fix_up_media_upload_no_initial_invalid(self): |
| 236 | invalid_method_desc = {'response': 'Who cares'} |
| 237 | self._base_fix_up_method_description_test(invalid_method_desc, {}, {}, |
| 238 | [], 0, None) |
| 239 | |
| 240 | def test_fix_up_media_upload_no_initial_valid_minimal(self): |
| 241 | valid_method_desc = {'mediaUpload': {'accept': []}} |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 242 | final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 243 | 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE} |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 244 | self._base_fix_up_method_description_test( |
| 245 | valid_method_desc, {}, final_parameters, [], 0, |
| 246 | 'http://root/upload/fake/fake-path/') |
| 247 | |
| 248 | def test_fix_up_media_upload_no_initial_valid_full(self): |
| 249 | valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}} |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 250 | final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 251 | 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE} |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 252 | ten_gb = 10 * 2**30 |
| 253 | self._base_fix_up_method_description_test( |
| 254 | valid_method_desc, {}, final_parameters, ['*/*'], |
| 255 | ten_gb, 'http://root/upload/fake/fake-path/') |
| 256 | |
| 257 | def test_fix_up_media_upload_with_initial_invalid(self): |
| 258 | invalid_method_desc = {'response': 'Who cares'} |
| 259 | initial_parameters = {'body': {}} |
| 260 | self._base_fix_up_method_description_test( |
| 261 | invalid_method_desc, initial_parameters, |
| 262 | initial_parameters, [], 0, None) |
| 263 | |
| 264 | def test_fix_up_media_upload_with_initial_valid_minimal(self): |
| 265 | valid_method_desc = {'mediaUpload': {'accept': []}} |
| 266 | initial_parameters = {'body': {}} |
Bu Sun Kim | b854ff1 | 2019-07-16 17:46:08 -0700 | [diff] [blame^] | 267 | final_parameters = {'body': {}, |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 268 | 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 269 | 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE} |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 270 | self._base_fix_up_method_description_test( |
| 271 | valid_method_desc, initial_parameters, final_parameters, [], 0, |
| 272 | 'http://root/upload/fake/fake-path/') |
| 273 | |
| 274 | def test_fix_up_media_upload_with_initial_valid_full(self): |
| 275 | valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}} |
| 276 | initial_parameters = {'body': {}} |
Bu Sun Kim | b854ff1 | 2019-07-16 17:46:08 -0700 | [diff] [blame^] | 277 | final_parameters = {'body': {}, |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 278 | 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 279 | 'media_mime_type': MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE} |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 280 | ten_gb = 10 * 2**30 |
| 281 | self._base_fix_up_method_description_test( |
| 282 | valid_method_desc, initial_parameters, final_parameters, ['*/*'], |
| 283 | ten_gb, 'http://root/upload/fake/fake-path/') |
| 284 | |
| 285 | def test_fix_up_method_description_get(self): |
| 286 | result = _fix_up_method_description(self.zoo_get_method_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 287 | self.zoo_root_desc, self.zoo_schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 288 | path_url = 'query' |
| 289 | http_method = 'GET' |
| 290 | method_id = 'bigquery.query' |
| 291 | accept = [] |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 292 | max_size = 0 |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 293 | media_path_url = None |
| 294 | self.assertEqual(result, (path_url, http_method, method_id, accept, |
| 295 | max_size, media_path_url)) |
| 296 | |
| 297 | def test_fix_up_method_description_insert(self): |
| 298 | result = _fix_up_method_description(self.zoo_insert_method_desc, |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 299 | self.zoo_root_desc, self.zoo_schema) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 300 | path_url = 'animals' |
| 301 | http_method = 'POST' |
| 302 | method_id = 'zoo.animals.insert' |
| 303 | accept = ['image/png'] |
INADA Naoki | 0bceb33 | 2014-08-20 15:27:52 +0900 | [diff] [blame] | 304 | max_size = 1024 |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 305 | media_path_url = 'https://www.googleapis.com/upload/zoo/v1/animals' |
| 306 | self.assertEqual(result, (path_url, http_method, method_id, accept, |
| 307 | max_size, media_path_url)) |
| 308 | |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 309 | def test_urljoin(self): |
| 310 | # We want to exhaustively test various URL combinations. |
| 311 | simple_bases = ['https://www.googleapis.com', 'https://www.googleapis.com/'] |
| 312 | long_urls = ['foo/v1/bar:custom?alt=json', '/foo/v1/bar:custom?alt=json'] |
| 313 | |
| 314 | long_bases = [ |
| 315 | 'https://www.googleapis.com/foo/v1', |
| 316 | 'https://www.googleapis.com/foo/v1/', |
| 317 | ] |
| 318 | simple_urls = ['bar:custom?alt=json', '/bar:custom?alt=json'] |
| 319 | |
| 320 | final_url = 'https://www.googleapis.com/foo/v1/bar:custom?alt=json' |
| 321 | for base, url in itertools.product(simple_bases, long_urls): |
| 322 | self.assertEqual(final_url, _urljoin(base, url)) |
| 323 | for base, url in itertools.product(long_bases, simple_urls): |
| 324 | self.assertEqual(final_url, _urljoin(base, url)) |
| 325 | |
| 326 | |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 327 | def test_ResourceMethodParameters_zoo_get(self): |
| 328 | parameters = ResourceMethodParameters(self.zoo_get_method_desc) |
| 329 | |
| 330 | param_types = {'a': 'any', |
| 331 | 'b': 'boolean', |
| 332 | 'e': 'string', |
| 333 | 'er': 'string', |
| 334 | 'i': 'integer', |
| 335 | 'n': 'number', |
| 336 | 'o': 'object', |
| 337 | 'q': 'string', |
| 338 | 'rr': 'string'} |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 339 | keys = list(param_types.keys()) |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 340 | self.assertEqual(parameters.argmap, dict((key, key) for key in keys)) |
| 341 | self.assertEqual(parameters.required_params, []) |
| 342 | self.assertEqual(sorted(parameters.repeated_params), ['er', 'rr']) |
| 343 | self.assertEqual(parameters.pattern_params, {'rr': '[a-z]+'}) |
| 344 | self.assertEqual(sorted(parameters.query_params), |
| 345 | ['a', 'b', 'e', 'er', 'i', 'n', 'o', 'q', 'rr']) |
| 346 | self.assertEqual(parameters.path_params, set()) |
| 347 | self.assertEqual(parameters.param_types, param_types) |
| 348 | enum_params = {'e': ['foo', 'bar'], |
| 349 | 'er': ['one', 'two', 'three']} |
| 350 | self.assertEqual(parameters.enum_params, enum_params) |
| 351 | |
| 352 | def test_ResourceMethodParameters_zoo_animals_patch(self): |
| 353 | method_desc = self.zoo_animals_resource['methods']['patch'] |
| 354 | parameters = ResourceMethodParameters(method_desc) |
| 355 | |
| 356 | param_types = {'name': 'string'} |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 357 | keys = list(param_types.keys()) |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 358 | self.assertEqual(parameters.argmap, dict((key, key) for key in keys)) |
| 359 | self.assertEqual(parameters.required_params, ['name']) |
| 360 | self.assertEqual(parameters.repeated_params, []) |
| 361 | self.assertEqual(parameters.pattern_params, {}) |
| 362 | self.assertEqual(parameters.query_params, []) |
| 363 | self.assertEqual(parameters.path_params, set(['name'])) |
| 364 | self.assertEqual(parameters.param_types, param_types) |
| 365 | self.assertEqual(parameters.enum_params, {}) |
| 366 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 367 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 368 | class DiscoveryErrors(unittest.TestCase): |
| 369 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 370 | def test_tests_should_be_run_with_strict_positional_enforcement(self): |
| 371 | try: |
| 372 | plus = build('plus', 'v1', None) |
| 373 | self.fail("should have raised a TypeError exception over missing http=.") |
| 374 | except TypeError: |
| 375 | pass |
| 376 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 377 | def test_failed_to_parse_discovery_json(self): |
| 378 | self.http = HttpMock(datafile('malformed.json'), {'status': '200'}) |
| 379 | try: |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 380 | plus = build('plus', 'v1', http=self.http, cache_discovery=False) |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 381 | self.fail("should have raised an exception over malformed JSON.") |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 382 | except InvalidJsonError: |
| 383 | pass |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 384 | |
Takashi Matsuo | 3772f9d | 2015-09-04 12:25:55 -0700 | [diff] [blame] | 385 | def test_unknown_api_name_or_version(self): |
| 386 | http = HttpMockSequence([ |
| 387 | ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()), |
Ethan Bao | 12b7cd3 | 2016-03-14 14:25:10 -0700 | [diff] [blame] | 388 | ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()), |
Takashi Matsuo | 3772f9d | 2015-09-04 12:25:55 -0700 | [diff] [blame] | 389 | ]) |
| 390 | with self.assertRaises(UnknownApiNameOrVersion): |
| 391 | plus = build('plus', 'v1', http=http, cache_discovery=False) |
| 392 | |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 393 | def test_credentials_and_http_mutually_exclusive(self): |
| 394 | http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 395 | with self.assertRaises(ValueError): |
| 396 | build( |
| 397 | 'plus', 'v1', http=http, credentials=mock.sentinel.credentials) |
| 398 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 399 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 400 | class DiscoveryFromDocument(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 401 | MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 402 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 403 | def test_can_build_from_local_document(self): |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 404 | discovery = open(datafile('plus.json')).read() |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 405 | plus = build_from_document( |
| 406 | discovery, base="https://www.googleapis.com/", |
| 407 | credentials=self.MOCK_CREDENTIALS) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 408 | self.assertTrue(plus is not None) |
Joe Gregorio | 4772f3d | 2012-12-10 10:22:37 -0500 | [diff] [blame] | 409 | self.assertTrue(hasattr(plus, 'activities')) |
| 410 | |
| 411 | def test_can_build_from_local_deserialized_document(self): |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 412 | discovery = open(datafile('plus.json')).read() |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 413 | discovery = json.loads(discovery) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 414 | plus = build_from_document( |
| 415 | discovery, base="https://www.googleapis.com/", |
| 416 | credentials=self.MOCK_CREDENTIALS) |
Joe Gregorio | 4772f3d | 2012-12-10 10:22:37 -0500 | [diff] [blame] | 417 | self.assertTrue(plus is not None) |
| 418 | self.assertTrue(hasattr(plus, 'activities')) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 419 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 420 | def test_building_with_base_remembers_base(self): |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 421 | discovery = open(datafile('plus.json')).read() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 422 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 423 | base = "https://www.example.com/" |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 424 | plus = build_from_document( |
| 425 | discovery, base=base, credentials=self.MOCK_CREDENTIALS) |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 426 | self.assertEquals("https://www.googleapis.com/plus/v1/", plus._baseUrl) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 427 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 428 | def test_building_with_optional_http_with_authorization(self): |
Jonathan Wayne Parrott | a6e6fbd | 2015-07-16 15:33:57 -0700 | [diff] [blame] | 429 | discovery = open(datafile('plus.json')).read() |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 430 | plus = build_from_document( |
| 431 | discovery, base="https://www.googleapis.com/", |
| 432 | credentials=self.MOCK_CREDENTIALS) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 433 | |
| 434 | # plus service requires Authorization, hence we expect to see AuthorizedHttp object here |
| 435 | self.assertIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp) |
| 436 | self.assertIsInstance(plus._http.http, httplib2.Http) |
| 437 | self.assertIsInstance(plus._http.http.timeout, int) |
| 438 | self.assertGreater(plus._http.http.timeout, 0) |
| 439 | |
| 440 | def test_building_with_optional_http_with_no_authorization(self): |
| 441 | discovery = open(datafile('plus.json')).read() |
| 442 | # Cleanup auth field, so we would use plain http client |
| 443 | discovery = json.loads(discovery) |
| 444 | discovery['auth'] = {} |
| 445 | discovery = json.dumps(discovery) |
| 446 | |
| 447 | plus = build_from_document( |
| 448 | discovery, base="https://www.googleapis.com/", |
Arunpn | 9d779cc | 2018-11-30 10:25:01 -0800 | [diff] [blame] | 449 | credentials=None) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 450 | # plus service requires Authorization |
| 451 | self.assertIsInstance(plus._http, httplib2.Http) |
| 452 | self.assertIsInstance(plus._http.timeout, int) |
| 453 | self.assertGreater(plus._http.timeout, 0) |
Jonathan Wayne Parrott | a6e6fbd | 2015-07-16 15:33:57 -0700 | [diff] [blame] | 454 | |
| 455 | def test_building_with_explicit_http(self): |
| 456 | http = HttpMock() |
| 457 | discovery = open(datafile('plus.json')).read() |
| 458 | plus = build_from_document( |
| 459 | discovery, base="https://www.googleapis.com/", http=http) |
| 460 | self.assertEquals(plus._http, http) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 461 | |
Jon Wayne Parrott | 068eb35 | 2017-02-08 10:13:06 -0800 | [diff] [blame] | 462 | def test_building_with_developer_key_skips_adc(self): |
| 463 | discovery = open(datafile('plus.json')).read() |
| 464 | plus = build_from_document( |
| 465 | discovery, base="https://www.googleapis.com/", developerKey='123') |
| 466 | self.assertIsInstance(plus._http, httplib2.Http) |
| 467 | # It should not be an AuthorizedHttp, because that would indicate that |
| 468 | # application default credentials were used. |
| 469 | self.assertNotIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp) |
| 470 | |
| 471 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 472 | class DiscoveryFromHttp(unittest.TestCase): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 473 | def setUp(self): |
Joe Beda | fb463cb | 2011-09-19 17:39:49 -0700 | [diff] [blame] | 474 | self.old_environ = os.environ.copy() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 475 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 476 | def tearDown(self): |
| 477 | os.environ = self.old_environ |
| 478 | |
| 479 | def test_userip_is_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 480 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 481 | # out of the raised exception. |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 482 | os.environ['REMOTE_ADDR'] = '10.0.0.1' |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 483 | try: |
| 484 | http = HttpMockSequence([ |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 485 | ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()), |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 486 | ]) |
Arunpn | 9d779cc | 2018-11-30 10:25:01 -0800 | [diff] [blame] | 487 | zoo = build('zoo', 'v1', http=http, developerKey=None, |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 488 | discoveryServiceUrl='http://example.com') |
| 489 | self.fail('Should have raised an exception.') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 490 | except HttpError as e: |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 491 | self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1') |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 492 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 493 | def test_userip_missing_is_not_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 494 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 495 | # out of the raised exception. |
| 496 | try: |
| 497 | http = HttpMockSequence([ |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 498 | ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()), |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 499 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 500 | zoo = build('zoo', 'v1', http=http, developerKey=None, |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 501 | discoveryServiceUrl='http://example.com') |
| 502 | self.fail('Should have raised an exception.') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 503 | except HttpError as e: |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 504 | self.assertEqual(e.uri, 'http://example.com') |
| 505 | |
Arunpn | 9d779cc | 2018-11-30 10:25:01 -0800 | [diff] [blame] | 506 | def test_key_is_added_to_discovery_uri(self): |
| 507 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 508 | # out of the raised exception. |
| 509 | try: |
| 510 | http = HttpMockSequence([ |
| 511 | ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()), |
| 512 | ]) |
| 513 | zoo = build('zoo', 'v1', http=http, developerKey='foo', |
| 514 | discoveryServiceUrl='http://example.com') |
| 515 | self.fail('Should have raised an exception.') |
| 516 | except HttpError as e: |
| 517 | self.assertEqual(e.uri, 'http://example.com?key=foo') |
| 518 | |
Ethan Bao | 12b7cd3 | 2016-03-14 14:25:10 -0700 | [diff] [blame] | 519 | def test_discovery_loading_from_v2_discovery_uri(self): |
| 520 | http = HttpMockSequence([ |
| 521 | ({'status': '404'}, 'Not found'), |
| 522 | ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()), |
| 523 | ]) |
| 524 | zoo = build('zoo', 'v1', http=http, cache_discovery=False) |
| 525 | self.assertTrue(hasattr(zoo, 'animals')) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 526 | |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 527 | class DiscoveryFromAppEngineCache(unittest.TestCase): |
| 528 | def test_appengine_memcache(self): |
| 529 | # Hack module import |
| 530 | self.orig_import = __import__ |
| 531 | self.mocked_api = mock.MagicMock() |
| 532 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 533 | def import_mock(name, *args, **kwargs): |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 534 | if name == 'google.appengine.api': |
| 535 | return self.mocked_api |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 536 | return self.orig_import(name, *args, **kwargs) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 537 | |
| 538 | import_fullname = '__builtin__.__import__' |
| 539 | if sys.version_info[0] >= 3: |
| 540 | import_fullname = 'builtins.__import__' |
| 541 | |
| 542 | with mock.patch(import_fullname, side_effect=import_mock): |
| 543 | namespace = 'google-api-client' |
| 544 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 545 | |
| 546 | self.mocked_api.memcache.get.return_value = None |
| 547 | |
| 548 | plus = build('plus', 'v1', http=self.http) |
| 549 | |
| 550 | # memcache.get is called once |
| 551 | url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest' |
| 552 | self.mocked_api.memcache.get.assert_called_once_with(url, |
| 553 | namespace=namespace) |
| 554 | |
| 555 | # memcache.set is called once |
| 556 | with open(datafile('plus.json')) as f: |
| 557 | content = f.read() |
| 558 | self.mocked_api.memcache.set.assert_called_once_with( |
| 559 | url, content, time=DISCOVERY_DOC_MAX_AGE, namespace=namespace) |
| 560 | |
| 561 | # Returns the cached content this time. |
| 562 | self.mocked_api.memcache.get.return_value = content |
| 563 | |
| 564 | # Make sure the contents are returned from the cache. |
| 565 | # (Otherwise it should through an error) |
| 566 | self.http = HttpMock(None, {'status': '200'}) |
| 567 | |
| 568 | plus = build('plus', 'v1', http=self.http) |
| 569 | |
| 570 | # memcache.get is called twice |
| 571 | self.mocked_api.memcache.get.assert_has_calls( |
| 572 | [mock.call(url, namespace=namespace), |
| 573 | mock.call(url, namespace=namespace)]) |
| 574 | |
| 575 | # memcahce.set is called just once |
| 576 | self.mocked_api.memcache.set.assert_called_once_with( |
| 577 | url, content, time=DISCOVERY_DOC_MAX_AGE,namespace=namespace) |
| 578 | |
| 579 | |
| 580 | class DictCache(Cache): |
| 581 | def __init__(self): |
| 582 | self.d = {} |
| 583 | def get(self, url): |
| 584 | return self.d.get(url, None) |
| 585 | def set(self, url, content): |
| 586 | self.d[url] = content |
| 587 | def contains(self, url): |
| 588 | return url in self.d |
| 589 | |
| 590 | |
| 591 | class DiscoveryFromFileCache(unittest.TestCase): |
| 592 | def test_file_based_cache(self): |
| 593 | cache = mock.Mock(wraps=DictCache()) |
Jon Wayne Parrott | 36d4e1b | 2016-10-17 13:31:33 -0700 | [diff] [blame] | 594 | with mock.patch('googleapiclient.discovery_cache.autodetect', |
| 595 | return_value=cache): |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 596 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 597 | |
| 598 | plus = build('plus', 'v1', http=self.http) |
| 599 | |
| 600 | # cache.get is called once |
| 601 | url = 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest' |
| 602 | cache.get.assert_called_once_with(url) |
| 603 | |
| 604 | # cache.set is called once |
| 605 | with open(datafile('plus.json')) as f: |
| 606 | content = f.read() |
| 607 | cache.set.assert_called_once_with(url, content) |
| 608 | |
| 609 | # Make sure there is a cache entry for the plus v1 discovery doc. |
| 610 | self.assertTrue(cache.contains(url)) |
| 611 | |
| 612 | # Make sure the contents are returned from the cache. |
| 613 | # (Otherwise it should through an error) |
| 614 | self.http = HttpMock(None, {'status': '200'}) |
| 615 | |
| 616 | plus = build('plus', 'v1', http=self.http) |
| 617 | |
| 618 | # cache.get is called twice |
| 619 | cache.get.assert_has_calls([mock.call(url), mock.call(url)]) |
| 620 | |
| 621 | # cahce.set is called just once |
| 622 | cache.set.assert_called_once_with(url, content) |
| 623 | |
| 624 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 625 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 626 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 627 | def test_method_error_checking(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 628 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 629 | plus = build('plus', 'v1', http=self.http) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 630 | |
| 631 | # Missing required parameters |
| 632 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 633 | plus.activities().list() |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 634 | self.fail() |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 635 | except TypeError as e: |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 636 | self.assertTrue('Missing' in str(e)) |
| 637 | |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 638 | # Missing required parameters even if supplied as None. |
| 639 | try: |
| 640 | plus.activities().list(collection=None, userId=None) |
| 641 | self.fail() |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 642 | except TypeError as e: |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 643 | self.assertTrue('Missing' in str(e)) |
| 644 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 645 | # Parameter doesn't match regex |
| 646 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 647 | plus.activities().list(collection='not_a_collection_name', userId='me') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 648 | self.fail() |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 649 | except TypeError as e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 650 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 651 | |
| 652 | # Unexpected parameter |
| 653 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 654 | plus.activities().list(flubber=12) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 655 | self.fail() |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 656 | except TypeError as e: |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 657 | self.assertTrue('unexpected' in str(e)) |
| 658 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 659 | def _check_query_types(self, request): |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 660 | parsed = urlparse(request.uri) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 661 | q = parse_qs(parsed[4]) |
| 662 | self.assertEqual(q['q'], ['foo']) |
| 663 | self.assertEqual(q['i'], ['1']) |
| 664 | self.assertEqual(q['n'], ['1.0']) |
| 665 | self.assertEqual(q['b'], ['false']) |
| 666 | self.assertEqual(q['a'], ['[1, 2, 3]']) |
| 667 | self.assertEqual(q['o'], ['{\'a\': 1}']) |
| 668 | self.assertEqual(q['e'], ['bar']) |
| 669 | |
| 670 | def test_type_coercion(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 671 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 672 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 673 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 674 | request = zoo.query( |
| 675 | q="foo", i=1.0, n=1.0, b=0, a=[1,2,3], o={'a':1}, e='bar') |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 676 | self._check_query_types(request) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 677 | request = zoo.query( |
| 678 | q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar') |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 679 | self._check_query_types(request) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 680 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 681 | request = zoo.query( |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 682 | q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar', er='two') |
Joe Gregorio | 6804c7a | 2011-11-18 14:30:32 -0500 | [diff] [blame] | 683 | |
| 684 | request = zoo.query( |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 685 | q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar', |
| 686 | er=['one', 'three'], rr=['foo', 'bar']) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 687 | self._check_query_types(request) |
| 688 | |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 689 | # Five is right out. |
Joe Gregorio | 20c26e5 | 2012-03-02 15:58:31 -0500 | [diff] [blame] | 690 | self.assertRaises(TypeError, zoo.query, er=['one', 'five']) |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 691 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 692 | def test_optional_stack_query_parameters(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 693 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 694 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 695 | request = zoo.query(trace='html', fields='description') |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 696 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 697 | parsed = urlparse(request.uri) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 698 | q = parse_qs(parsed[4]) |
| 699 | self.assertEqual(q['trace'], ['html']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 700 | self.assertEqual(q['fields'], ['description']) |
| 701 | |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 702 | def test_string_params_value_of_none_get_dropped(self): |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 703 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 704 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 705 | request = zoo.query(trace=None, fields='description') |
| 706 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 707 | parsed = urlparse(request.uri) |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 708 | q = parse_qs(parsed[4]) |
| 709 | self.assertFalse('trace' in q) |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 710 | |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 711 | def test_model_added_query_parameters(self): |
| 712 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 713 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 714 | request = zoo.animals().get(name='Lion') |
| 715 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 716 | parsed = urlparse(request.uri) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 717 | q = parse_qs(parsed[4]) |
| 718 | self.assertEqual(q['alt'], ['json']) |
| 719 | self.assertEqual(request.headers['accept'], 'application/json') |
| 720 | |
| 721 | def test_fallback_to_raw_model(self): |
| 722 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 723 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 724 | request = zoo.animals().getmedia(name='Lion') |
| 725 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 726 | parsed = urlparse(request.uri) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 727 | q = parse_qs(parsed[4]) |
| 728 | self.assertTrue('alt' not in q) |
| 729 | self.assertEqual(request.headers['accept'], '*/*') |
| 730 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 731 | def test_patch(self): |
| 732 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 733 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 734 | request = zoo.animals().patch(name='lion', body='{"description": "foo"}') |
| 735 | |
| 736 | self.assertEqual(request.method, 'PATCH') |
| 737 | |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 738 | def test_batch_request_from_discovery(self): |
| 739 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 740 | # zoo defines a batchPath |
| 741 | zoo = build('zoo', 'v1', http=self.http) |
| 742 | batch_request = zoo.new_batch_http_request() |
| 743 | self.assertEqual(batch_request._batch_uri, |
| 744 | "https://www.googleapis.com/batchZoo") |
| 745 | |
| 746 | def test_batch_request_from_default(self): |
| 747 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 748 | # plus does not define a batchPath |
| 749 | plus = build('plus', 'v1', http=self.http) |
| 750 | batch_request = plus.new_batch_http_request() |
| 751 | self.assertEqual(batch_request._batch_uri, |
| 752 | "https://www.googleapis.com/batch") |
| 753 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 754 | def test_tunnel_patch(self): |
| 755 | http = HttpMockSequence([ |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 756 | ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()), |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 757 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 758 | ]) |
| 759 | http = tunnel_patch(http) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 760 | zoo = build('zoo', 'v1', http=http, cache_discovery=False) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 761 | resp = zoo.animals().patch( |
| 762 | name='lion', body='{"description": "foo"}').execute() |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 763 | |
| 764 | self.assertTrue('x-http-method-override' in resp) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 765 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 766 | def test_plus_resources(self): |
| 767 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 768 | plus = build('plus', 'v1', http=self.http) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 769 | self.assertTrue(getattr(plus, 'activities')) |
| 770 | self.assertTrue(getattr(plus, 'people')) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 771 | |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 772 | def test_oauth2client_credentials(self): |
| 773 | credentials = mock.Mock(spec=GoogleCredentials) |
| 774 | credentials.create_scoped_required.return_value = False |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 775 | |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 776 | discovery = open(datafile('plus.json')).read() |
| 777 | service = build_from_document(discovery, credentials=credentials) |
| 778 | self.assertEqual(service._http, credentials.authorize.return_value) |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 779 | |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 780 | def test_google_auth_credentials(self): |
| 781 | credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 782 | discovery = open(datafile('plus.json')).read() |
| 783 | service = build_from_document(discovery, credentials=credentials) |
| 784 | |
| 785 | self.assertIsInstance(service._http, google_auth_httplib2.AuthorizedHttp) |
| 786 | self.assertEqual(service._http.credentials, credentials) |
| 787 | |
| 788 | def test_no_scopes_no_credentials(self): |
| 789 | # Zoo doesn't have scopes |
| 790 | discovery = open(datafile('zoo.json')).read() |
| 791 | service = build_from_document(discovery) |
| 792 | # Should be an ordinary httplib2.Http instance and not AuthorizedHttp. |
| 793 | self.assertIsInstance(service._http, httplib2.Http) |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 794 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 795 | def test_full_featured(self): |
| 796 | # Zoo should exercise all discovery facets |
| 797 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 798 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 799 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 800 | self.assertTrue(getattr(zoo, 'animals')) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 801 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 802 | request = zoo.animals().list(name='bat', projection="full") |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 803 | parsed = urlparse(request.uri) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 804 | q = parse_qs(parsed[4]) |
| 805 | self.assertEqual(q['name'], ['bat']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 806 | self.assertEqual(q['projection'], ['full']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 807 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 808 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 809 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 810 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 811 | self.assertTrue(getattr(zoo, 'animals')) |
| 812 | request = zoo.my().favorites().list(max_results="5") |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 813 | parsed = urlparse(request.uri) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 814 | q = parse_qs(parsed[4]) |
| 815 | self.assertEqual(q['max-results'], ['5']) |
| 816 | |
Pat Ferate | c605087 | 2015-03-03 18:24:59 -0800 | [diff] [blame] | 817 | @unittest.skipIf(six.PY3, 'print is not a reserved name in Python 3') |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 818 | def test_methods_with_reserved_names(self): |
| 819 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 820 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 821 | self.assertTrue(getattr(zoo, 'animals')) |
| 822 | request = zoo.global_().print_().assert_(max_results="5") |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 823 | parsed = urlparse(request.uri) |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 824 | self.assertEqual(parsed[2], '/zoo/v1/global/print/assert') |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 825 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 826 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 827 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 828 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 829 | self.assertTrue(getattr(zoo, 'query')) |
| 830 | request = zoo.query(q="foo") |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 831 | parsed = urlparse(request.uri) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 832 | q = parse_qs(parsed[4]) |
| 833 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 834 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 835 | def test_simple_media_uploads(self): |
| 836 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 837 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 838 | doc = getattr(zoo.animals().insert, '__doc__') |
| 839 | self.assertTrue('media_body' in doc) |
| 840 | |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 841 | def test_simple_media_upload_no_max_size_provided(self): |
| 842 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 843 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 844 | request = zoo.animals().crossbreed(media_body=datafile('small.png')) |
| 845 | self.assertEquals('image/png', request.headers['content-type']) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 846 | self.assertEquals(b'PNG', request.body[1:4]) |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 847 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 848 | def test_simple_media_raise_correct_exceptions(self): |
| 849 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 850 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 851 | |
| 852 | try: |
| 853 | zoo.animals().insert(media_body=datafile('smiley.png')) |
| 854 | self.fail("should throw exception if media is too large.") |
| 855 | except MediaUploadSizeError: |
| 856 | pass |
| 857 | |
| 858 | try: |
| 859 | zoo.animals().insert(media_body=datafile('small.jpg')) |
| 860 | self.fail("should throw exception if mimetype is unacceptable.") |
| 861 | except UnacceptableMimeTypeError: |
| 862 | pass |
| 863 | |
| 864 | def test_simple_media_good_upload(self): |
| 865 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 866 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 867 | |
| 868 | request = zoo.animals().insert(media_body=datafile('small.png')) |
| 869 | self.assertEquals('image/png', request.headers['content-type']) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 870 | self.assertEquals(b'PNG', request.body[1:4]) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 871 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 872 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 873 | request.uri) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 874 | |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 875 | def test_simple_media_unknown_mimetype(self): |
| 876 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 877 | zoo = build('zoo', 'v1', http=self.http) |
| 878 | |
| 879 | try: |
| 880 | zoo.animals().insert(media_body=datafile('small-png')) |
| 881 | self.fail("should throw exception if mimetype is unknown.") |
| 882 | except UnknownFileType: |
| 883 | pass |
| 884 | |
| 885 | request = zoo.animals().insert(media_body=datafile('small-png'), |
| 886 | media_mime_type='image/png') |
| 887 | self.assertEquals('image/png', request.headers['content-type']) |
| 888 | self.assertEquals(b'PNG', request.body[1:4]) |
| 889 | assertUrisEqual(self, |
| 890 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json', |
| 891 | request.uri) |
| 892 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 893 | def test_multipart_media_raise_correct_exceptions(self): |
| 894 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 895 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 896 | |
| 897 | try: |
| 898 | zoo.animals().insert(media_body=datafile('smiley.png'), body={}) |
| 899 | self.fail("should throw exception if media is too large.") |
| 900 | except MediaUploadSizeError: |
| 901 | pass |
| 902 | |
| 903 | try: |
| 904 | zoo.animals().insert(media_body=datafile('small.jpg'), body={}) |
| 905 | self.fail("should throw exception if mimetype is unacceptable.") |
| 906 | except UnacceptableMimeTypeError: |
| 907 | pass |
| 908 | |
| 909 | def test_multipart_media_good_upload(self): |
| 910 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 911 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 912 | |
| 913 | request = zoo.animals().insert(media_body=datafile('small.png'), body={}) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 914 | self.assertTrue(request.headers['content-type'].startswith( |
| 915 | 'multipart/related')) |
Phil Ruffwind | 26178fc | 2015-10-13 19:00:33 -0400 | [diff] [blame] | 916 | with open(datafile('small.png'), 'rb') as f: |
| 917 | contents = f.read() |
| 918 | boundary = re.match(b'--=+([^=]+)', request.body).group(1) |
| 919 | self.assertEqual( |
| 920 | request.body.rstrip(b"\n"), # Python 2.6 does not add a trailing \n |
| 921 | b'--===============' + boundary + b'==\n' + |
| 922 | b'Content-Type: application/json\n' + |
| 923 | b'MIME-Version: 1.0\n\n' + |
| 924 | b'{"data": {}}\n' + |
| 925 | b'--===============' + boundary + b'==\n' + |
| 926 | b'Content-Type: image/png\n' + |
| 927 | b'MIME-Version: 1.0\n' + |
| 928 | b'Content-Transfer-Encoding: binary\n\n' + |
| 929 | contents + |
| 930 | b'\n--===============' + boundary + b'==--') |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 931 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 932 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=multipart&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 933 | request.uri) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 934 | |
| 935 | def test_media_capable_method_without_media(self): |
| 936 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 937 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 938 | |
| 939 | request = zoo.animals().insert(body={}) |
| 940 | self.assertTrue(request.headers['content-type'], 'application/json') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 941 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 942 | def test_resumable_multipart_media_good_upload(self): |
| 943 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 944 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 945 | |
| 946 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 947 | request = zoo.animals().insert(media_body=media_upload, body={}) |
| 948 | self.assertTrue(request.headers['content-type'].startswith( |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 949 | 'application/json')) |
| 950 | self.assertEquals('{"data": {}}', request.body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 951 | self.assertEquals(media_upload, request.resumable) |
| 952 | |
| 953 | self.assertEquals('image/png', request.resumable.mimetype()) |
| 954 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 955 | self.assertNotEquals(request.body, None) |
| 956 | self.assertEquals(request.resumable_uri, None) |
| 957 | |
| 958 | http = HttpMockSequence([ |
| 959 | ({'status': '200', |
| 960 | 'location': 'http://upload.example.com'}, ''), |
| 961 | ({'status': '308', |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 962 | 'location': 'http://upload.example.com/2'}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 963 | ({'status': '308', |
| 964 | 'location': 'http://upload.example.com/3', |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 965 | 'range': '0-12'}, ''), |
| 966 | ({'status': '308', |
| 967 | 'location': 'http://upload.example.com/4', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 968 | 'range': '0-%d' % (media_upload.size() - 2)}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 969 | ({'status': '200'}, '{"foo": "bar"}'), |
| 970 | ]) |
| 971 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 972 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 973 | self.assertEquals(None, body) |
| 974 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 975 | self.assertEquals(0, status.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 976 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 977 | # Two requests should have been made and the resumable_uri should have been |
| 978 | # updated for each one. |
| 979 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/2') |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 980 | self.assertEquals(media_upload, request.resumable) |
| 981 | self.assertEquals(0, request.resumable_progress) |
| 982 | |
| 983 | # This next chuck call should upload the first chunk |
| 984 | status, body = request.next_chunk(http=http) |
| 985 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/3') |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 986 | self.assertEquals(media_upload, request.resumable) |
| 987 | self.assertEquals(13, request.resumable_progress) |
| 988 | |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 989 | # This call will upload the next chunk |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 990 | status, body = request.next_chunk(http=http) |
Matt Carroll | 94a5394 | 2016-12-20 13:56:43 -0800 | [diff] [blame] | 991 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/4') |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 992 | self.assertEquals(media_upload.size()-1, request.resumable_progress) |
| 993 | self.assertEquals('{"data": {}}', request.body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 994 | |
| 995 | # Final call to next_chunk should complete the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 996 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 997 | self.assertEquals(body, {"foo": "bar"}) |
| 998 | self.assertEquals(status, None) |
| 999 | |
| 1000 | |
| 1001 | def test_resumable_media_good_upload(self): |
| 1002 | """Not a multipart upload.""" |
| 1003 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1004 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1005 | |
| 1006 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 1007 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1008 | self.assertEquals(media_upload, request.resumable) |
| 1009 | |
| 1010 | self.assertEquals('image/png', request.resumable.mimetype()) |
| 1011 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1012 | self.assertEquals(request.body, None) |
| 1013 | self.assertEquals(request.resumable_uri, None) |
| 1014 | |
| 1015 | http = HttpMockSequence([ |
| 1016 | ({'status': '200', |
| 1017 | 'location': 'http://upload.example.com'}, ''), |
| 1018 | ({'status': '308', |
| 1019 | 'location': 'http://upload.example.com/2', |
| 1020 | 'range': '0-12'}, ''), |
| 1021 | ({'status': '308', |
| 1022 | 'location': 'http://upload.example.com/3', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 1023 | 'range': '0-%d' % (media_upload.size() - 2)}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1024 | ({'status': '200'}, '{"foo": "bar"}'), |
| 1025 | ]) |
| 1026 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1027 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1028 | self.assertEquals(None, body) |
| 1029 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
| 1030 | self.assertEquals(13, status.resumable_progress) |
| 1031 | |
| 1032 | # Two requests should have been made and the resumable_uri should have been |
| 1033 | # updated for each one. |
| 1034 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/2') |
| 1035 | |
| 1036 | self.assertEquals(media_upload, request.resumable) |
| 1037 | self.assertEquals(13, request.resumable_progress) |
| 1038 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1039 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1040 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/3') |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 1041 | self.assertEquals(media_upload.size()-1, request.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1042 | self.assertEquals(request.body, None) |
| 1043 | |
| 1044 | # Final call to next_chunk should complete the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1045 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1046 | self.assertEquals(body, {"foo": "bar"}) |
| 1047 | self.assertEquals(status, None) |
| 1048 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1049 | def test_resumable_media_good_upload_from_execute(self): |
| 1050 | """Not a multipart upload.""" |
| 1051 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1052 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1053 | |
| 1054 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 1055 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 1056 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 1057 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=resumable&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 1058 | request.uri) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1059 | |
| 1060 | http = HttpMockSequence([ |
| 1061 | ({'status': '200', |
| 1062 | 'location': 'http://upload.example.com'}, ''), |
| 1063 | ({'status': '308', |
| 1064 | 'location': 'http://upload.example.com/2', |
| 1065 | 'range': '0-12'}, ''), |
| 1066 | ({'status': '308', |
| 1067 | 'location': 'http://upload.example.com/3', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 1068 | 'range': '0-%d' % media_upload.size()}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1069 | ({'status': '200'}, '{"foo": "bar"}'), |
| 1070 | ]) |
| 1071 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1072 | body = request.execute(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1073 | self.assertEquals(body, {"foo": "bar"}) |
| 1074 | |
| 1075 | def test_resumable_media_fail_unknown_response_code_first_request(self): |
| 1076 | """Not a multipart upload.""" |
| 1077 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1078 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1079 | |
| 1080 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 1081 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 1082 | |
| 1083 | http = HttpMockSequence([ |
| 1084 | ({'status': '400', |
| 1085 | 'location': 'http://upload.example.com'}, ''), |
| 1086 | ]) |
| 1087 | |
Joe Gregorio | baf0480 | 2013-03-01 12:27:06 -0500 | [diff] [blame] | 1088 | try: |
| 1089 | request.execute(http=http) |
| 1090 | self.fail('Should have raised ResumableUploadError.') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 1091 | except ResumableUploadError as e: |
Joe Gregorio | baf0480 | 2013-03-01 12:27:06 -0500 | [diff] [blame] | 1092 | self.assertEqual(400, e.resp.status) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1093 | |
| 1094 | def test_resumable_media_fail_unknown_response_code_subsequent_request(self): |
| 1095 | """Not a multipart upload.""" |
| 1096 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1097 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1098 | |
| 1099 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 1100 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 1101 | |
| 1102 | http = HttpMockSequence([ |
| 1103 | ({'status': '200', |
| 1104 | 'location': 'http://upload.example.com'}, ''), |
| 1105 | ({'status': '400'}, ''), |
| 1106 | ]) |
| 1107 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1108 | self.assertRaises(HttpError, request.execute, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1109 | self.assertTrue(request._in_error_state) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1110 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1111 | http = HttpMockSequence([ |
| 1112 | ({'status': '308', |
| 1113 | 'range': '0-5'}, ''), |
| 1114 | ({'status': '308', |
| 1115 | 'range': '0-6'}, ''), |
| 1116 | ]) |
| 1117 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1118 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1119 | self.assertEquals(status.resumable_progress, 7, |
| 1120 | 'Should have first checked length and then tried to PUT more.') |
| 1121 | self.assertFalse(request._in_error_state) |
| 1122 | |
| 1123 | # Put it back in an error state. |
| 1124 | http = HttpMockSequence([ |
| 1125 | ({'status': '400'}, ''), |
| 1126 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1127 | self.assertRaises(HttpError, request.execute, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1128 | self.assertTrue(request._in_error_state) |
| 1129 | |
| 1130 | # Pretend the last request that 400'd actually succeeded. |
| 1131 | http = HttpMockSequence([ |
| 1132 | ({'status': '200'}, '{"foo": "bar"}'), |
| 1133 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1134 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1135 | self.assertEqual(body, {'foo': 'bar'}) |
| 1136 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1137 | def test_media_io_base_stream_unlimited_chunksize_resume(self): |
| 1138 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1139 | zoo = build('zoo', 'v1', http=self.http) |
| 1140 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1141 | # Set up a seekable stream and try to upload in single chunk. |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1142 | fd = BytesIO(b'01234"56789"') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1143 | media_upload = MediaIoBaseUpload( |
| 1144 | fd=fd, mimetype='text/plain', chunksize=-1, resumable=True) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1145 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1146 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1147 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1148 | # The single chunk fails, restart at the right point. |
| 1149 | http = HttpMockSequence([ |
| 1150 | ({'status': '200', |
| 1151 | 'location': 'http://upload.example.com'}, ''), |
| 1152 | ({'status': '308', |
| 1153 | 'location': 'http://upload.example.com/2', |
| 1154 | 'range': '0-4'}, ''), |
| 1155 | ({'status': '200'}, 'echo_request_body'), |
| 1156 | ]) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1157 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1158 | body = request.execute(http=http) |
| 1159 | self.assertEqual('56789', body) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1160 | |
| 1161 | def test_media_io_base_stream_chunksize_resume(self): |
| 1162 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1163 | zoo = build('zoo', 'v1', http=self.http) |
| 1164 | |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1165 | # Set up a seekable stream and try to upload in chunks. |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1166 | fd = BytesIO(b'0123456789') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1167 | media_upload = MediaIoBaseUpload( |
| 1168 | fd=fd, mimetype='text/plain', chunksize=5, resumable=True) |
| 1169 | |
| 1170 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 1171 | |
| 1172 | # The single chunk fails, pull the content sent out of the exception. |
| 1173 | http = HttpMockSequence([ |
| 1174 | ({'status': '200', |
| 1175 | 'location': 'http://upload.example.com'}, ''), |
| 1176 | ({'status': '400'}, 'echo_request_body'), |
| 1177 | ]) |
| 1178 | |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1179 | try: |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1180 | body = request.execute(http=http) |
| 1181 | except HttpError as e: |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1182 | self.assertEqual(b'01234', e.content) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1183 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1184 | def test_resumable_media_handle_uploads_of_unknown_size(self): |
| 1185 | http = HttpMockSequence([ |
| 1186 | ({'status': '200', |
| 1187 | 'location': 'http://upload.example.com'}, ''), |
| 1188 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 1189 | ]) |
| 1190 | |
| 1191 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1192 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1193 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1194 | # Create an upload that doesn't know the full size of the media. |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1195 | class IoBaseUnknownLength(MediaUpload): |
| 1196 | def chunksize(self): |
| 1197 | return 10 |
| 1198 | |
| 1199 | def mimetype(self): |
| 1200 | return 'image/png' |
| 1201 | |
| 1202 | def size(self): |
| 1203 | return None |
| 1204 | |
| 1205 | def resumable(self): |
| 1206 | return True |
| 1207 | |
| 1208 | def getbytes(self, begin, length): |
| 1209 | return '0123456789' |
| 1210 | |
| 1211 | upload = IoBaseUnknownLength() |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1212 | |
| 1213 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1214 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1215 | self.assertEqual(body, { |
| 1216 | 'Content-Range': 'bytes 0-9/*', |
| 1217 | 'Content-Length': '10', |
| 1218 | }) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1219 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1220 | def test_resumable_media_no_streaming_on_unsupported_platforms(self): |
| 1221 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1222 | zoo = build('zoo', 'v1', http=self.http) |
| 1223 | |
| 1224 | class IoBaseHasStream(MediaUpload): |
| 1225 | def chunksize(self): |
| 1226 | return 10 |
| 1227 | |
| 1228 | def mimetype(self): |
| 1229 | return 'image/png' |
| 1230 | |
| 1231 | def size(self): |
| 1232 | return None |
| 1233 | |
| 1234 | def resumable(self): |
| 1235 | return True |
| 1236 | |
| 1237 | def getbytes(self, begin, length): |
| 1238 | return '0123456789' |
| 1239 | |
| 1240 | def has_stream(self): |
| 1241 | return True |
| 1242 | |
| 1243 | def stream(self): |
| 1244 | raise NotImplementedError() |
| 1245 | |
| 1246 | upload = IoBaseHasStream() |
| 1247 | |
| 1248 | orig_version = sys.version_info |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1249 | |
| 1250 | sys.version_info = (2, 6, 5, 'final', 0) |
| 1251 | |
| 1252 | request = zoo.animals().insert(media_body=upload, body=None) |
| 1253 | |
| 1254 | # This should raise an exception because stream() will be called. |
| 1255 | http = HttpMockSequence([ |
| 1256 | ({'status': '200', |
| 1257 | 'location': 'http://upload.example.com'}, ''), |
| 1258 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 1259 | ]) |
| 1260 | |
| 1261 | self.assertRaises(NotImplementedError, request.next_chunk, http=http) |
| 1262 | |
| 1263 | sys.version_info = orig_version |
| 1264 | |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1265 | def test_resumable_media_handle_uploads_of_unknown_size_eof(self): |
| 1266 | http = HttpMockSequence([ |
| 1267 | ({'status': '200', |
| 1268 | 'location': 'http://upload.example.com'}, ''), |
| 1269 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 1270 | ]) |
| 1271 | |
| 1272 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1273 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1274 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1275 | fd = BytesIO(b'data goes here') |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1276 | |
| 1277 | # Create an upload that doesn't know the full size of the media. |
| 1278 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 1279 | fd=fd, mimetype='image/png', chunksize=15, resumable=True) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1280 | |
| 1281 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1282 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1283 | self.assertEqual(body, { |
| 1284 | 'Content-Range': 'bytes 0-13/14', |
| 1285 | 'Content-Length': '14', |
| 1286 | }) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1287 | |
| 1288 | def test_resumable_media_handle_resume_of_upload_of_unknown_size(self): |
| 1289 | http = HttpMockSequence([ |
| 1290 | ({'status': '200', |
| 1291 | 'location': 'http://upload.example.com'}, ''), |
| 1292 | ({'status': '400'}, ''), |
| 1293 | ]) |
| 1294 | |
| 1295 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1296 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1297 | |
| 1298 | # Create an upload that doesn't know the full size of the media. |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1299 | fd = BytesIO(b'data goes here') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1300 | |
| 1301 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 1302 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1303 | |
| 1304 | request = zoo.animals().insert(media_body=upload, body=None) |
| 1305 | |
| 1306 | # Put it in an error state. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1307 | self.assertRaises(HttpError, request.next_chunk, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1308 | |
| 1309 | http = HttpMockSequence([ |
| 1310 | ({'status': '400', |
| 1311 | 'range': '0-5'}, 'echo_request_headers_as_json'), |
| 1312 | ]) |
| 1313 | try: |
| 1314 | # Should resume the upload by first querying the status of the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1315 | request.next_chunk(http=http) |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 1316 | except HttpError as e: |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1317 | expected = { |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1318 | 'Content-Range': 'bytes */14', |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1319 | 'content-length': '0' |
| 1320 | } |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 1321 | self.assertEqual(expected, json.loads(e.content.decode('utf-8')), |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1322 | 'Should send an empty body when requesting the current upload status.') |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1323 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1324 | def test_pickle(self): |
| 1325 | sorted_resource_keys = ['_baseUrl', |
| 1326 | '_developerKey', |
| 1327 | '_dynamic_attrs', |
| 1328 | '_http', |
| 1329 | '_model', |
| 1330 | '_requestBuilder', |
| 1331 | '_resourceDesc', |
| 1332 | '_rootDesc', |
| 1333 | '_schema', |
| 1334 | 'animals', |
| 1335 | 'global_', |
| 1336 | 'load', |
| 1337 | 'loadNoTemplate', |
| 1338 | 'my', |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 1339 | 'new_batch_http_request', |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1340 | 'query', |
| 1341 | 'scopedAnimals'] |
| 1342 | |
| 1343 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1344 | zoo = build('zoo', 'v1', http=http) |
| 1345 | self.assertEqual(sorted(zoo.__dict__.keys()), sorted_resource_keys) |
| 1346 | |
| 1347 | pickled_zoo = pickle.dumps(zoo) |
| 1348 | new_zoo = pickle.loads(pickled_zoo) |
| 1349 | self.assertEqual(sorted(new_zoo.__dict__.keys()), sorted_resource_keys) |
| 1350 | self.assertTrue(hasattr(new_zoo, 'animals')) |
| 1351 | self.assertTrue(callable(new_zoo.animals)) |
| 1352 | self.assertTrue(hasattr(new_zoo, 'global_')) |
| 1353 | self.assertTrue(callable(new_zoo.global_)) |
| 1354 | self.assertTrue(hasattr(new_zoo, 'load')) |
| 1355 | self.assertTrue(callable(new_zoo.load)) |
| 1356 | self.assertTrue(hasattr(new_zoo, 'loadNoTemplate')) |
| 1357 | self.assertTrue(callable(new_zoo.loadNoTemplate)) |
| 1358 | self.assertTrue(hasattr(new_zoo, 'my')) |
| 1359 | self.assertTrue(callable(new_zoo.my)) |
| 1360 | self.assertTrue(hasattr(new_zoo, 'query')) |
| 1361 | self.assertTrue(callable(new_zoo.query)) |
| 1362 | self.assertTrue(hasattr(new_zoo, 'scopedAnimals')) |
| 1363 | self.assertTrue(callable(new_zoo.scopedAnimals)) |
| 1364 | |
Joe Gregorio | 003b6e4 | 2013-02-13 15:42:19 -0500 | [diff] [blame] | 1365 | self.assertEqual(sorted(zoo._dynamic_attrs), sorted(new_zoo._dynamic_attrs)) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1366 | self.assertEqual(zoo._baseUrl, new_zoo._baseUrl) |
| 1367 | self.assertEqual(zoo._developerKey, new_zoo._developerKey) |
| 1368 | self.assertEqual(zoo._requestBuilder, new_zoo._requestBuilder) |
| 1369 | self.assertEqual(zoo._resourceDesc, new_zoo._resourceDesc) |
| 1370 | self.assertEqual(zoo._rootDesc, new_zoo._rootDesc) |
| 1371 | # _http, _model and _schema won't be equal since we will get new |
| 1372 | # instances upon un-pickling |
| 1373 | |
| 1374 | def _dummy_zoo_request(self): |
| 1375 | with open(os.path.join(DATA_DIR, 'zoo.json'), 'rU') as fh: |
| 1376 | zoo_contents = fh.read() |
| 1377 | |
| 1378 | zoo_uri = uritemplate.expand(DISCOVERY_URI, |
| 1379 | {'api': 'zoo', 'apiVersion': 'v1'}) |
| 1380 | if 'REMOTE_ADDR' in os.environ: |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 1381 | zoo_uri = util._add_query_parameter(zoo_uri, 'userIp', |
| 1382 | os.environ['REMOTE_ADDR']) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1383 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 1384 | http = build_http() |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1385 | original_request = http.request |
| 1386 | def wrapped_request(uri, method='GET', *args, **kwargs): |
| 1387 | if uri == zoo_uri: |
| 1388 | return httplib2.Response({'status': '200'}), zoo_contents |
| 1389 | return original_request(uri, method=method, *args, **kwargs) |
| 1390 | http.request = wrapped_request |
| 1391 | return http |
| 1392 | |
| 1393 | def _dummy_token(self): |
| 1394 | access_token = 'foo' |
| 1395 | client_id = 'some_client_id' |
| 1396 | client_secret = 'cOuDdkfjxxnv+' |
| 1397 | refresh_token = '1/0/a.df219fjls0' |
| 1398 | token_expiry = datetime.datetime.utcnow() |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1399 | user_agent = 'refresh_checker/1.0' |
| 1400 | return OAuth2Credentials( |
| 1401 | access_token, client_id, client_secret, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1402 | refresh_token, token_expiry, GOOGLE_TOKEN_URI, |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1403 | user_agent) |
| 1404 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1405 | def test_pickle_with_credentials(self): |
| 1406 | credentials = self._dummy_token() |
| 1407 | http = self._dummy_zoo_request() |
| 1408 | http = credentials.authorize(http) |
| 1409 | self.assertTrue(hasattr(http.request, 'credentials')) |
| 1410 | |
| 1411 | zoo = build('zoo', 'v1', http=http) |
| 1412 | pickled_zoo = pickle.dumps(zoo) |
| 1413 | new_zoo = pickle.loads(pickled_zoo) |
| 1414 | self.assertEqual(sorted(zoo.__dict__.keys()), |
| 1415 | sorted(new_zoo.__dict__.keys())) |
| 1416 | new_http = new_zoo._http |
| 1417 | self.assertFalse(hasattr(new_http.request, 'credentials')) |
| 1418 | |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 1419 | def test_resumable_media_upload_no_content(self): |
| 1420 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1421 | zoo = build('zoo', 'v1', http=self.http) |
| 1422 | |
| 1423 | media_upload = MediaFileUpload(datafile('empty'), resumable=True) |
| 1424 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 1425 | |
| 1426 | self.assertEquals(media_upload, request.resumable) |
| 1427 | self.assertEquals(request.body, None) |
| 1428 | self.assertEquals(request.resumable_uri, None) |
| 1429 | |
| 1430 | http = HttpMockSequence([ |
| 1431 | ({'status': '200', |
| 1432 | 'location': 'http://upload.example.com'}, ''), |
| 1433 | ({'status': '308', |
| 1434 | 'location': 'http://upload.example.com/2', |
| 1435 | 'range': '0-0'}, ''), |
| 1436 | ]) |
| 1437 | |
| 1438 | status, body = request.next_chunk(http=http) |
| 1439 | self.assertEquals(None, body) |
| 1440 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
| 1441 | self.assertEquals(0, status.progress()) |
| 1442 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1443 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 1444 | class Next(unittest.TestCase): |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 1445 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1446 | def test_next_successful_none_on_no_next_page_token(self): |
| 1447 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1448 | tasks = build('tasks', 'v1', http=self.http) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1449 | request = tasks.tasklists().list() |
| 1450 | self.assertEqual(None, tasks.tasklists().list_next(request, {})) |
| 1451 | |
Son Dinh | 2a9a213 | 2015-07-23 16:30:56 +0000 | [diff] [blame] | 1452 | def test_next_successful_none_on_empty_page_token(self): |
| 1453 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
| 1454 | tasks = build('tasks', 'v1', http=self.http) |
| 1455 | request = tasks.tasklists().list() |
| 1456 | next_request = tasks.tasklists().list_next( |
| 1457 | request, {'nextPageToken': ''}) |
| 1458 | self.assertEqual(None, next_request) |
| 1459 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1460 | def test_next_successful_with_next_page_token(self): |
| 1461 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1462 | tasks = build('tasks', 'v1', http=self.http) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1463 | request = tasks.tasklists().list() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 1464 | next_request = tasks.tasklists().list_next( |
| 1465 | request, {'nextPageToken': '123abc'}) |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 1466 | parsed = list(urlparse(next_request.uri)) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1467 | q = parse_qs(parsed[4]) |
| 1468 | self.assertEqual(q['pageToken'][0], '123abc') |
| 1469 | |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 1470 | def test_next_successful_with_next_page_token_alternate_name(self): |
| 1471 | self.http = HttpMock(datafile('bigquery.json'), {'status': '200'}) |
| 1472 | bigquery = build('bigquery', 'v2', http=self.http) |
| 1473 | request = bigquery.tabledata().list(datasetId='', projectId='', tableId='') |
| 1474 | next_request = bigquery.tabledata().list_next( |
| 1475 | request, {'pageToken': '123abc'}) |
| 1476 | parsed = list(urlparse(next_request.uri)) |
| 1477 | q = parse_qs(parsed[4]) |
| 1478 | self.assertEqual(q['pageToken'][0], '123abc') |
| 1479 | |
| 1480 | def test_next_successful_with_next_page_token_in_body(self): |
| 1481 | self.http = HttpMock(datafile('logging.json'), {'status': '200'}) |
| 1482 | logging = build('logging', 'v2', http=self.http) |
| 1483 | request = logging.entries().list(body={}) |
| 1484 | next_request = logging.entries().list_next( |
| 1485 | request, {'nextPageToken': '123abc'}) |
| 1486 | body = JsonModel().deserialize(next_request.body) |
| 1487 | self.assertEqual(body['pageToken'], '123abc') |
| 1488 | |
Joe Gregorio | 555f33c | 2011-08-19 14:56:07 -0400 | [diff] [blame] | 1489 | def test_next_with_method_with_no_properties(self): |
| 1490 | self.http = HttpMock(datafile('latitude.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1491 | service = build('latitude', 'v1', http=self.http) |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 1492 | service.currentLocation().get() |
| 1493 | |
| 1494 | def test_next_nonexistent_with_no_next_page_token(self): |
| 1495 | self.http = HttpMock(datafile('drive.json'), {'status': '200'}) |
| 1496 | drive = build('drive', 'v3', http=self.http) |
| 1497 | drive.changes().watch(body={}) |
| 1498 | self.assertFalse(callable(getattr(drive.changes(), 'watch_next', None))) |
| 1499 | |
| 1500 | def test_next_successful_with_next_page_token_required(self): |
| 1501 | self.http = HttpMock(datafile('drive.json'), {'status': '200'}) |
| 1502 | drive = build('drive', 'v3', http=self.http) |
| 1503 | request = drive.changes().list(pageToken='startPageToken') |
| 1504 | next_request = drive.changes().list_next( |
| 1505 | request, {'nextPageToken': '123abc'}) |
| 1506 | parsed = list(urlparse(next_request.uri)) |
| 1507 | q = parse_qs(parsed[4]) |
| 1508 | self.assertEqual(q['pageToken'][0], '123abc') |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 1509 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 1510 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1511 | class MediaGet(unittest.TestCase): |
| 1512 | |
| 1513 | def test_get_media(self): |
| 1514 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1515 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1516 | request = zoo.animals().get_media(name='Lion') |
| 1517 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 1518 | parsed = urlparse(request.uri) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1519 | q = parse_qs(parsed[4]) |
| 1520 | self.assertEqual(q['alt'], ['media']) |
| 1521 | self.assertEqual(request.headers['accept'], '*/*') |
| 1522 | |
| 1523 | http = HttpMockSequence([ |
| 1524 | ({'status': '200'}, 'standing in for media'), |
| 1525 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1526 | response = request.execute(http=http) |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 1527 | self.assertEqual(b'standing in for media', response) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1528 | |
| 1529 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1530 | if __name__ == '__main__': |
| 1531 | unittest.main() |