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