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 |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 24 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 25 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 26 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 27 | from collections import defaultdict |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 28 | import copy |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 29 | import datetime |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 30 | import httplib2 |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 31 | import io |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 32 | import itertools |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 33 | import json |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 34 | import os |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 35 | import pickle |
Phil Ruffwind | 26178fc | 2015-10-13 19:00:33 -0400 | [diff] [blame] | 36 | import re |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 37 | import sys |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 38 | import unittest2 as unittest |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 39 | import urllib |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 40 | |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 41 | from parameterized import parameterized |
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 |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 45 | from google.auth.transport import mtls |
| 46 | from google.auth.exceptions import MutualTLSChannelError |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 47 | import google_auth_httplib2 |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 48 | import google.api_core.exceptions |
| 49 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 50 | from googleapiclient.discovery import _fix_up_media_upload |
| 51 | from googleapiclient.discovery import _fix_up_method_description |
| 52 | from googleapiclient.discovery import _fix_up_parameters |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 53 | from googleapiclient.discovery import _urljoin |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 54 | from googleapiclient.discovery import build |
| 55 | from googleapiclient.discovery import build_from_document |
| 56 | from googleapiclient.discovery import DISCOVERY_URI |
| 57 | from googleapiclient.discovery import key2param |
| 58 | from googleapiclient.discovery import MEDIA_BODY_PARAMETER_DEFAULT_VALUE |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 59 | from googleapiclient.discovery import MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 60 | from googleapiclient.discovery import ResourceMethodParameters |
| 61 | from googleapiclient.discovery import STACK_QUERY_PARAMETERS |
| 62 | from googleapiclient.discovery import STACK_QUERY_PARAMETER_DEFAULT_VALUE |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 63 | from googleapiclient.discovery import V1_DISCOVERY_URI |
| 64 | from googleapiclient.discovery import V2_DISCOVERY_URI |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 65 | from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE |
| 66 | from googleapiclient.discovery_cache.base import Cache |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 67 | from googleapiclient.errors import HttpError |
| 68 | from googleapiclient.errors import InvalidJsonError |
| 69 | from googleapiclient.errors import MediaUploadSizeError |
| 70 | from googleapiclient.errors import ResumableUploadError |
| 71 | from googleapiclient.errors import UnacceptableMimeTypeError |
Takashi Matsuo | 3772f9d | 2015-09-04 12:25:55 -0700 | [diff] [blame] | 72 | from googleapiclient.errors import UnknownApiNameOrVersion |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 73 | from googleapiclient.errors import UnknownFileType |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 74 | from googleapiclient.http import build_http |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 75 | from googleapiclient.http import BatchHttpRequest |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 76 | from googleapiclient.http import HttpMock |
| 77 | from googleapiclient.http import HttpMockSequence |
| 78 | from googleapiclient.http import MediaFileUpload |
| 79 | from googleapiclient.http import MediaIoBaseUpload |
| 80 | from googleapiclient.http import MediaUpload |
| 81 | from googleapiclient.http import MediaUploadProgress |
| 82 | from googleapiclient.http import tunnel_patch |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 83 | from googleapiclient.model import JsonModel |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 84 | from googleapiclient.schema import Schemas |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 85 | from oauth2client import GOOGLE_TOKEN_URI |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 86 | from oauth2client.client import OAuth2Credentials, GoogleCredentials |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 87 | |
Bu Sun Kim | a9583f7 | 2021-03-15 09:12:02 -0600 | [diff] [blame] | 88 | |
Helen Koike | de13e3b | 2018-04-26 16:05:16 -0300 | [diff] [blame] | 89 | from googleapiclient import _helpers as util |
Jon Wayne Parrott | 36d4e1b | 2016-10-17 13:31:33 -0700 | [diff] [blame] | 90 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 91 | import uritemplate |
| 92 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 93 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 94 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 95 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 96 | |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 97 | def assertUrisEqual(testcase, expected, actual): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 98 | """Test that URIs are the same, up to reordering of query parameters.""" |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 99 | expected = urllib.parse.urlparse(expected) |
| 100 | actual = urllib.parse.urlparse(actual) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 101 | testcase.assertEqual(expected.scheme, actual.scheme) |
| 102 | testcase.assertEqual(expected.netloc, actual.netloc) |
| 103 | testcase.assertEqual(expected.path, actual.path) |
| 104 | testcase.assertEqual(expected.params, actual.params) |
| 105 | testcase.assertEqual(expected.fragment, actual.fragment) |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 106 | expected_query = urllib.parse.parse_qs(expected.query) |
| 107 | actual_query = urllib.parse.parse_qs(actual.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 108 | for name in list(expected_query.keys()): |
| 109 | testcase.assertEqual(expected_query[name], actual_query[name]) |
| 110 | for name in list(actual_query.keys()): |
| 111 | testcase.assertEqual(expected_query[name], actual_query[name]) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 112 | |
| 113 | |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 114 | def assert_discovery_uri(testcase, actual, service_name, version, discovery): |
| 115 | """Assert that discovery URI used was the one that was expected |
| 116 | for a given service and version.""" |
| 117 | params = {"api": service_name, "apiVersion": version} |
| 118 | expanded_requested_uri = uritemplate.expand(discovery, params) |
| 119 | assertUrisEqual(testcase, expanded_requested_uri, actual) |
| 120 | |
| 121 | |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 122 | def validate_discovery_requests(testcase, http_mock, service_name, version, discovery): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 123 | """Validates that there have > 0 calls to Http Discovery |
| 124 | and that LAST discovery URI used was the one that was expected |
| 125 | for a given service and version.""" |
| 126 | testcase.assertTrue(len(http_mock.request_sequence) > 0) |
| 127 | if len(http_mock.request_sequence) > 0: |
| 128 | actual_uri = http_mock.request_sequence[-1][0] |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 129 | assert_discovery_uri(testcase, actual_uri, service_name, version, discovery) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 130 | |
| 131 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 132 | def datafile(filename): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 133 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 134 | |
| 135 | |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 136 | def read_datafile(filename, mode="r"): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 137 | with open(datafile(filename), mode=mode) as f: |
| 138 | return f.read() |
| 139 | |
| 140 | |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 141 | class SetupHttplib2(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 142 | def test_retries(self): |
| 143 | # Merely loading googleapiclient.discovery should set the RETRIES to 1. |
| 144 | self.assertEqual(1, httplib2.RETRIES) |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 145 | |
| 146 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 147 | class Utilities(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 148 | def setUp(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 149 | self.zoo_root_desc = json.loads(read_datafile("zoo.json", "r")) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 150 | self.zoo_get_method_desc = self.zoo_root_desc["methods"]["query"] |
| 151 | self.zoo_animals_resource = self.zoo_root_desc["resources"]["animals"] |
| 152 | self.zoo_insert_method_desc = self.zoo_animals_resource["methods"]["insert"] |
| 153 | self.zoo_schema = Schemas(self.zoo_root_desc) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 154 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 155 | def test_key2param(self): |
| 156 | self.assertEqual("max_results", key2param("max-results")) |
| 157 | self.assertEqual("x007_bond", key2param("007-bond")) |
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 _base_fix_up_parameters_test(self, method_desc, http_method, root_desc, schema): |
| 160 | self.assertEqual(method_desc["httpMethod"], http_method) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 161 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 162 | method_desc_copy = copy.deepcopy(method_desc) |
| 163 | self.assertEqual(method_desc, method_desc_copy) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 164 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 165 | parameters = _fix_up_parameters( |
| 166 | method_desc_copy, root_desc, http_method, schema |
| 167 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 168 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 169 | self.assertNotEqual(method_desc, method_desc_copy) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 170 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 171 | for param_name in STACK_QUERY_PARAMETERS: |
| 172 | self.assertEqual( |
| 173 | STACK_QUERY_PARAMETER_DEFAULT_VALUE, parameters[param_name] |
| 174 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 175 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 176 | for param_name, value in root_desc.get("parameters", {}).items(): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 177 | self.assertEqual(value, parameters[param_name]) |
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 | return parameters |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 180 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 181 | def test_fix_up_parameters_get(self): |
| 182 | parameters = self._base_fix_up_parameters_test( |
| 183 | self.zoo_get_method_desc, "GET", self.zoo_root_desc, self.zoo_schema |
| 184 | ) |
| 185 | # Since http_method is 'GET' |
| 186 | self.assertFalse("body" in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 187 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 188 | def test_fix_up_parameters_insert(self): |
| 189 | parameters = self._base_fix_up_parameters_test( |
| 190 | self.zoo_insert_method_desc, "POST", self.zoo_root_desc, self.zoo_schema |
| 191 | ) |
| 192 | body = {"description": "The request body.", "type": "object", "$ref": "Animal"} |
| 193 | self.assertEqual(parameters["body"], body) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 194 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 195 | def test_fix_up_parameters_check_body(self): |
| 196 | dummy_root_desc = {} |
| 197 | dummy_schema = { |
| 198 | "Request": { |
| 199 | "properties": { |
| 200 | "description": "Required. Dummy parameter.", |
| 201 | "type": "string", |
| 202 | } |
| 203 | } |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 204 | } |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 205 | no_payload_http_method = "DELETE" |
| 206 | with_payload_http_method = "PUT" |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 207 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 208 | invalid_method_desc = {"response": "Who cares"} |
| 209 | valid_method_desc = { |
| 210 | "request": {"key1": "value1", "key2": "value2", "$ref": "Request"} |
| 211 | } |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 212 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 213 | parameters = _fix_up_parameters( |
| 214 | invalid_method_desc, dummy_root_desc, no_payload_http_method, dummy_schema |
| 215 | ) |
| 216 | self.assertFalse("body" in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 217 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 218 | parameters = _fix_up_parameters( |
| 219 | valid_method_desc, dummy_root_desc, no_payload_http_method, dummy_schema |
| 220 | ) |
| 221 | self.assertFalse("body" in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 222 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 223 | parameters = _fix_up_parameters( |
| 224 | invalid_method_desc, dummy_root_desc, with_payload_http_method, dummy_schema |
| 225 | ) |
| 226 | self.assertFalse("body" in parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 227 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 228 | parameters = _fix_up_parameters( |
| 229 | valid_method_desc, dummy_root_desc, with_payload_http_method, dummy_schema |
| 230 | ) |
| 231 | body = { |
| 232 | "description": "The request body.", |
| 233 | "type": "object", |
| 234 | "$ref": "Request", |
| 235 | "key1": "value1", |
| 236 | "key2": "value2", |
| 237 | } |
| 238 | self.assertEqual(parameters["body"], body) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 239 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 240 | def test_fix_up_parameters_optional_body(self): |
| 241 | # Request with no parameters |
| 242 | dummy_schema = {"Request": {"properties": {}}} |
| 243 | method_desc = {"request": {"$ref": "Request"}} |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 244 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 245 | parameters = _fix_up_parameters(method_desc, {}, "POST", dummy_schema) |
Jean-Loup Roussel-Clouet | 0c0c897 | 2018-04-28 05:42:43 +0900 | [diff] [blame] | 246 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 247 | def _base_fix_up_method_description_test( |
| 248 | self, |
| 249 | method_desc, |
| 250 | initial_parameters, |
| 251 | final_parameters, |
| 252 | final_accept, |
| 253 | final_max_size, |
| 254 | final_media_path_url, |
| 255 | ): |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 256 | fake_root_desc = { |
| 257 | "rootUrl": "http://root/", |
| 258 | "servicePath": "fake/", |
| 259 | "mtlsRootUrl": "http://root/", |
| 260 | } |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 261 | fake_path_url = "fake-path/" |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 262 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 263 | accept, max_size, media_path_url = _fix_up_media_upload( |
| 264 | method_desc, fake_root_desc, fake_path_url, initial_parameters |
| 265 | ) |
| 266 | self.assertEqual(accept, final_accept) |
| 267 | self.assertEqual(max_size, final_max_size) |
| 268 | self.assertEqual(media_path_url, final_media_path_url) |
| 269 | self.assertEqual(initial_parameters, final_parameters) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 270 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 271 | def test_fix_up_media_upload_no_initial_invalid(self): |
| 272 | invalid_method_desc = {"response": "Who cares"} |
| 273 | self._base_fix_up_method_description_test( |
| 274 | invalid_method_desc, {}, {}, [], 0, None |
| 275 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 276 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 277 | def test_fix_up_media_upload_no_initial_valid_minimal(self): |
| 278 | valid_method_desc = {"mediaUpload": {"accept": []}} |
| 279 | final_parameters = { |
| 280 | "media_body": MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 281 | "media_mime_type": MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE, |
| 282 | } |
| 283 | self._base_fix_up_method_description_test( |
| 284 | valid_method_desc, |
| 285 | {}, |
| 286 | final_parameters, |
| 287 | [], |
| 288 | 0, |
| 289 | "http://root/upload/fake/fake-path/", |
| 290 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 291 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 292 | def test_fix_up_media_upload_no_initial_valid_full(self): |
| 293 | valid_method_desc = {"mediaUpload": {"accept": ["*/*"], "maxSize": "10GB"}} |
| 294 | final_parameters = { |
| 295 | "media_body": MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 296 | "media_mime_type": MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE, |
| 297 | } |
| 298 | ten_gb = 10 * 2 ** 30 |
| 299 | self._base_fix_up_method_description_test( |
| 300 | valid_method_desc, |
| 301 | {}, |
| 302 | final_parameters, |
| 303 | ["*/*"], |
| 304 | ten_gb, |
| 305 | "http://root/upload/fake/fake-path/", |
| 306 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 307 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 308 | def test_fix_up_media_upload_with_initial_invalid(self): |
| 309 | invalid_method_desc = {"response": "Who cares"} |
| 310 | initial_parameters = {"body": {}} |
| 311 | self._base_fix_up_method_description_test( |
| 312 | invalid_method_desc, initial_parameters, initial_parameters, [], 0, None |
| 313 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 314 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 315 | def test_fix_up_media_upload_with_initial_valid_minimal(self): |
| 316 | valid_method_desc = {"mediaUpload": {"accept": []}} |
| 317 | initial_parameters = {"body": {}} |
| 318 | final_parameters = { |
| 319 | "body": {}, |
| 320 | "media_body": MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 321 | "media_mime_type": MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE, |
| 322 | } |
| 323 | self._base_fix_up_method_description_test( |
| 324 | valid_method_desc, |
| 325 | initial_parameters, |
| 326 | final_parameters, |
| 327 | [], |
| 328 | 0, |
| 329 | "http://root/upload/fake/fake-path/", |
| 330 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 331 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 332 | def test_fix_up_media_upload_with_initial_valid_full(self): |
| 333 | valid_method_desc = {"mediaUpload": {"accept": ["*/*"], "maxSize": "10GB"}} |
| 334 | initial_parameters = {"body": {}} |
| 335 | final_parameters = { |
| 336 | "body": {}, |
| 337 | "media_body": MEDIA_BODY_PARAMETER_DEFAULT_VALUE, |
| 338 | "media_mime_type": MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE, |
| 339 | } |
| 340 | ten_gb = 10 * 2 ** 30 |
| 341 | self._base_fix_up_method_description_test( |
| 342 | valid_method_desc, |
| 343 | initial_parameters, |
| 344 | final_parameters, |
| 345 | ["*/*"], |
| 346 | ten_gb, |
| 347 | "http://root/upload/fake/fake-path/", |
| 348 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 349 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 350 | def test_fix_up_method_description_get(self): |
| 351 | result = _fix_up_method_description( |
| 352 | self.zoo_get_method_desc, self.zoo_root_desc, self.zoo_schema |
| 353 | ) |
| 354 | path_url = "query" |
| 355 | http_method = "GET" |
| 356 | method_id = "bigquery.query" |
| 357 | accept = [] |
| 358 | max_size = 0 |
| 359 | media_path_url = None |
| 360 | self.assertEqual( |
| 361 | result, (path_url, http_method, method_id, accept, max_size, media_path_url) |
| 362 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 363 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 364 | def test_fix_up_method_description_insert(self): |
| 365 | result = _fix_up_method_description( |
| 366 | self.zoo_insert_method_desc, self.zoo_root_desc, self.zoo_schema |
| 367 | ) |
| 368 | path_url = "animals" |
| 369 | http_method = "POST" |
| 370 | method_id = "zoo.animals.insert" |
| 371 | accept = ["image/png"] |
| 372 | max_size = 1024 |
| 373 | media_path_url = "https://www.googleapis.com/upload/zoo/v1/animals" |
| 374 | self.assertEqual( |
| 375 | result, (path_url, http_method, method_id, accept, max_size, media_path_url) |
| 376 | ) |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame] | 377 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 378 | def test_urljoin(self): |
| 379 | # We want to exhaustively test various URL combinations. |
| 380 | simple_bases = ["https://www.googleapis.com", "https://www.googleapis.com/"] |
| 381 | 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] | 382 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 383 | long_bases = [ |
| 384 | "https://www.googleapis.com/foo/v1", |
| 385 | "https://www.googleapis.com/foo/v1/", |
| 386 | ] |
| 387 | simple_urls = ["bar:custom?alt=json", "/bar:custom?alt=json"] |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 388 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 389 | final_url = "https://www.googleapis.com/foo/v1/bar:custom?alt=json" |
| 390 | for base, url in itertools.product(simple_bases, long_urls): |
| 391 | self.assertEqual(final_url, _urljoin(base, url)) |
| 392 | for base, url in itertools.product(long_bases, simple_urls): |
| 393 | self.assertEqual(final_url, _urljoin(base, url)) |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 394 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 395 | def test_ResourceMethodParameters_zoo_get(self): |
| 396 | parameters = ResourceMethodParameters(self.zoo_get_method_desc) |
Craig Citro | 7ee535d | 2015-02-23 10:11:14 -0800 | [diff] [blame] | 397 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 398 | param_types = { |
| 399 | "a": "any", |
| 400 | "b": "boolean", |
| 401 | "e": "string", |
| 402 | "er": "string", |
| 403 | "i": "integer", |
| 404 | "n": "number", |
| 405 | "o": "object", |
| 406 | "q": "string", |
| 407 | "rr": "string", |
| 408 | } |
| 409 | keys = list(param_types.keys()) |
| 410 | self.assertEqual(parameters.argmap, dict((key, key) for key in keys)) |
| 411 | self.assertEqual(parameters.required_params, []) |
| 412 | self.assertEqual(sorted(parameters.repeated_params), ["er", "rr"]) |
| 413 | self.assertEqual(parameters.pattern_params, {"rr": "[a-z]+"}) |
| 414 | self.assertEqual( |
| 415 | sorted(parameters.query_params), |
| 416 | ["a", "b", "e", "er", "i", "n", "o", "q", "rr"], |
| 417 | ) |
| 418 | self.assertEqual(parameters.path_params, set()) |
| 419 | self.assertEqual(parameters.param_types, param_types) |
| 420 | enum_params = {"e": ["foo", "bar"], "er": ["one", "two", "three"]} |
| 421 | self.assertEqual(parameters.enum_params, enum_params) |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 422 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 423 | def test_ResourceMethodParameters_zoo_animals_patch(self): |
| 424 | method_desc = self.zoo_animals_resource["methods"]["patch"] |
| 425 | parameters = ResourceMethodParameters(method_desc) |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 426 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 427 | param_types = {"name": "string"} |
| 428 | keys = list(param_types.keys()) |
| 429 | self.assertEqual(parameters.argmap, dict((key, key) for key in keys)) |
| 430 | self.assertEqual(parameters.required_params, ["name"]) |
| 431 | self.assertEqual(parameters.repeated_params, []) |
| 432 | self.assertEqual(parameters.pattern_params, {}) |
| 433 | self.assertEqual(parameters.query_params, []) |
| 434 | self.assertEqual(parameters.path_params, set(["name"])) |
| 435 | self.assertEqual(parameters.param_types, param_types) |
| 436 | self.assertEqual(parameters.enum_params, {}) |
Daniel Hermes | 954e124 | 2013-02-28 09:28:37 -0800 | [diff] [blame] | 437 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 438 | |
Bu Sun Kim | 98888da | 2020-09-23 11:10:39 -0600 | [diff] [blame] | 439 | class Discovery(unittest.TestCase): |
| 440 | def test_discovery_http_is_closed(self): |
| 441 | http = HttpMock(datafile("malformed.json"), {"status": "200"}) |
| 442 | service = build("plus", "v1", credentials=mock.sentinel.credentials) |
| 443 | http.close.assert_called_once() |
| 444 | |
| 445 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 446 | class DiscoveryErrors(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 447 | def test_tests_should_be_run_with_strict_positional_enforcement(self): |
| 448 | try: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 449 | plus = build("plus", "v1", None, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 450 | self.fail("should have raised a TypeError exception over missing http=.") |
| 451 | except TypeError: |
| 452 | pass |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 453 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 454 | def test_failed_to_parse_discovery_json(self): |
| 455 | self.http = HttpMock(datafile("malformed.json"), {"status": "200"}) |
| 456 | try: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 457 | plus = build("plus", "v1", http=self.http, cache_discovery=False, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 458 | self.fail("should have raised an exception over malformed JSON.") |
| 459 | except InvalidJsonError: |
| 460 | pass |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 461 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 462 | def test_unknown_api_name_or_version(self): |
| 463 | http = HttpMockSequence( |
| 464 | [ |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 465 | ({"status": "404"}, read_datafile("zoo.json", "rb")), |
| 466 | ({"status": "404"}, read_datafile("zoo.json", "rb")), |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 467 | ] |
| 468 | ) |
| 469 | with self.assertRaises(UnknownApiNameOrVersion): |
| 470 | plus = build("plus", "v1", http=http, cache_discovery=False) |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 471 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 472 | def test_credentials_and_http_mutually_exclusive(self): |
| 473 | http = HttpMock(datafile("plus.json"), {"status": "200"}) |
| 474 | with self.assertRaises(ValueError): |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 475 | build("plus", "v1", http=http, credentials=mock.sentinel.credentials, static_discovery=False) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 476 | |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 477 | def test_credentials_file_and_http_mutually_exclusive(self): |
| 478 | http = HttpMock(datafile("plus.json"), {"status": "200"}) |
| 479 | with self.assertRaises(ValueError): |
| 480 | build( |
| 481 | "plus", |
| 482 | "v1", |
| 483 | http=http, |
| 484 | client_options=google.api_core.client_options.ClientOptions( |
| 485 | credentials_file="credentials.json" |
| 486 | ), |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 487 | static_discovery=False, |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 488 | ) |
| 489 | |
| 490 | def test_credentials_and_credentials_file_mutually_exclusive(self): |
| 491 | with self.assertRaises(google.api_core.exceptions.DuplicateCredentialArgs): |
| 492 | build( |
| 493 | "plus", |
| 494 | "v1", |
| 495 | credentials=mock.sentinel.credentials, |
| 496 | client_options=google.api_core.client_options.ClientOptions( |
| 497 | credentials_file="credentials.json" |
| 498 | ), |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 499 | static_discovery=False, |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 500 | ) |
| 501 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 502 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 503 | class DiscoveryFromDocument(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 504 | MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 505 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 506 | def test_can_build_from_local_document(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 507 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 508 | plus = build_from_document( |
| 509 | discovery, |
| 510 | base="https://www.googleapis.com/", |
| 511 | credentials=self.MOCK_CREDENTIALS, |
| 512 | ) |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 513 | self.assertIsNotNone(plus) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 514 | self.assertTrue(hasattr(plus, "activities")) |
Joe Gregorio | 4772f3d | 2012-12-10 10:22:37 -0500 | [diff] [blame] | 515 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 516 | def test_can_build_from_local_deserialized_document(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 517 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 518 | discovery = json.loads(discovery) |
| 519 | plus = build_from_document( |
| 520 | discovery, |
| 521 | base="https://www.googleapis.com/", |
| 522 | credentials=self.MOCK_CREDENTIALS, |
| 523 | ) |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 524 | self.assertIsNotNone(plus) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 525 | self.assertTrue(hasattr(plus, "activities")) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 526 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 527 | def test_building_with_base_remembers_base(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 528 | discovery = read_datafile("plus.json") |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 529 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 530 | base = "https://www.example.com/" |
| 531 | plus = build_from_document( |
| 532 | discovery, base=base, credentials=self.MOCK_CREDENTIALS |
| 533 | ) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 534 | self.assertEqual("https://www.googleapis.com/plus/v1/", plus._baseUrl) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 535 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 536 | def test_building_with_optional_http_with_authorization(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 537 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 538 | plus = build_from_document( |
| 539 | discovery, |
| 540 | base="https://www.googleapis.com/", |
| 541 | credentials=self.MOCK_CREDENTIALS, |
| 542 | ) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 543 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 544 | # plus service requires Authorization, hence we expect to see AuthorizedHttp object here |
| 545 | self.assertIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp) |
| 546 | self.assertIsInstance(plus._http.http, httplib2.Http) |
| 547 | self.assertIsInstance(plus._http.http.timeout, int) |
| 548 | self.assertGreater(plus._http.http.timeout, 0) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 549 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 550 | def test_building_with_optional_http_with_no_authorization(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 551 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 552 | # Cleanup auth field, so we would use plain http client |
| 553 | discovery = json.loads(discovery) |
| 554 | discovery["auth"] = {} |
| 555 | discovery = json.dumps(discovery) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 556 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 557 | plus = build_from_document( |
| 558 | discovery, base="https://www.googleapis.com/", credentials=None |
| 559 | ) |
| 560 | # plus service requires Authorization |
| 561 | self.assertIsInstance(plus._http, httplib2.Http) |
| 562 | self.assertIsInstance(plus._http.timeout, int) |
| 563 | self.assertGreater(plus._http.timeout, 0) |
Jonathan Wayne Parrott | a6e6fbd | 2015-07-16 15:33:57 -0700 | [diff] [blame] | 564 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 565 | def test_building_with_explicit_http(self): |
| 566 | http = HttpMock() |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 567 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 568 | plus = build_from_document( |
| 569 | discovery, base="https://www.googleapis.com/", http=http |
| 570 | ) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 571 | self.assertEqual(plus._http, http) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 572 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 573 | def test_building_with_developer_key_skips_adc(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 574 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 575 | plus = build_from_document( |
| 576 | discovery, base="https://www.googleapis.com/", developerKey="123" |
| 577 | ) |
| 578 | self.assertIsInstance(plus._http, httplib2.Http) |
| 579 | # It should not be an AuthorizedHttp, because that would indicate that |
| 580 | # application default credentials were used. |
| 581 | self.assertNotIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp) |
Jon Wayne Parrott | 068eb35 | 2017-02-08 10:13:06 -0800 | [diff] [blame] | 582 | |
Bu Sun Kim | 98888da | 2020-09-23 11:10:39 -0600 | [diff] [blame] | 583 | def test_building_with_context_manager(self): |
| 584 | discovery = read_datafile("plus.json") |
| 585 | with mock.patch("httplib2.Http") as http: |
| 586 | with build_from_document(discovery, base="https://www.googleapis.com/", credentials=self.MOCK_CREDENTIALS) as plus: |
| 587 | self.assertIsNotNone(plus) |
| 588 | self.assertTrue(hasattr(plus, "activities")) |
| 589 | plus._http.http.close.assert_called_once() |
| 590 | |
| 591 | def test_resource_close(self): |
| 592 | discovery = read_datafile("plus.json") |
Anthonios Partheniou | 3b4f2e2 | 2021-03-19 11:36:01 -0400 | [diff] [blame] | 593 | |
Bu Sun Kim | a9583f7 | 2021-03-15 09:12:02 -0600 | [diff] [blame] | 594 | with mock.patch("httplib2.Http", autospec=True) as httplib2_http: |
| 595 | http = httplib2_http() |
| 596 | plus = build_from_document( |
| 597 | discovery, |
| 598 | base="https://www.googleapis.com/", |
| 599 | http=http, |
| 600 | ) |
| 601 | plus.close() |
| 602 | http.close.assert_called_once() |
| 603 | |
| 604 | def test_resource_close_authorized_http(self): |
| 605 | discovery = read_datafile("plus.json") |
| 606 | with mock.patch("google_auth_httplib2.AuthorizedHttp", autospec=True): |
Bu Sun Kim | 98888da | 2020-09-23 11:10:39 -0600 | [diff] [blame] | 607 | plus = build_from_document( |
| 608 | discovery, |
| 609 | base="https://www.googleapis.com/", |
| 610 | credentials=self.MOCK_CREDENTIALS, |
| 611 | ) |
| 612 | plus.close() |
Bu Sun Kim | a9583f7 | 2021-03-15 09:12:02 -0600 | [diff] [blame] | 613 | plus._http.close.assert_called_once() |
Bu Sun Kim | 98888da | 2020-09-23 11:10:39 -0600 | [diff] [blame] | 614 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 615 | def test_api_endpoint_override_from_client_options(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 616 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 617 | api_endpoint = "https://foo.googleapis.com/" |
| 618 | options = google.api_core.client_options.ClientOptions( |
| 619 | api_endpoint=api_endpoint |
| 620 | ) |
Bu Sun Kim | 8ed1dcd | 2020-05-14 00:42:22 -0700 | [diff] [blame] | 621 | plus = build_from_document( |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 622 | discovery, client_options=options, credentials=self.MOCK_CREDENTIALS |
Bu Sun Kim | 8ed1dcd | 2020-05-14 00:42:22 -0700 | [diff] [blame] | 623 | ) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 624 | |
| 625 | self.assertEqual(plus._baseUrl, api_endpoint) |
| 626 | |
Pavel Kiselev | 21af37b | 2020-06-18 19:50:03 +0300 | [diff] [blame] | 627 | def test_api_endpoint_override_from_client_options_mapping_object(self): |
| 628 | |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 629 | discovery = read_datafile("plus.json") |
Pavel Kiselev | 21af37b | 2020-06-18 19:50:03 +0300 | [diff] [blame] | 630 | api_endpoint = "https://foo.googleapis.com/" |
| 631 | mapping_object = defaultdict(str) |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 632 | mapping_object["api_endpoint"] = api_endpoint |
Bu Sun Kim | d500e83 | 2021-04-27 16:13:05 -0600 | [diff] [blame] | 633 | plus = build_from_document( |
| 634 | discovery, client_options=mapping_object, credentials=self.MOCK_CREDENTIALS |
| 635 | ) |
Pavel Kiselev | 21af37b | 2020-06-18 19:50:03 +0300 | [diff] [blame] | 636 | |
| 637 | self.assertEqual(plus._baseUrl, api_endpoint) |
| 638 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 639 | def test_api_endpoint_override_from_client_options_dict(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 640 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 641 | api_endpoint = "https://foo.googleapis.com/" |
| 642 | plus = build_from_document( |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 643 | discovery, |
Bu Sun Kim | 8ed1dcd | 2020-05-14 00:42:22 -0700 | [diff] [blame] | 644 | client_options={"api_endpoint": api_endpoint}, |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 645 | credentials=self.MOCK_CREDENTIALS, |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 646 | ) |
| 647 | |
| 648 | self.assertEqual(plus._baseUrl, api_endpoint) |
| 649 | |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 650 | def test_scopes_from_client_options(self): |
| 651 | discovery = read_datafile("plus.json") |
| 652 | |
| 653 | with mock.patch("googleapiclient._auth.default_credentials") as default: |
| 654 | plus = build_from_document( |
| 655 | discovery, client_options={"scopes": ["1", "2"]}, |
| 656 | ) |
| 657 | |
| 658 | default.assert_called_once_with(scopes=["1", "2"], quota_project_id=None) |
| 659 | |
| 660 | def test_quota_project_from_client_options(self): |
| 661 | discovery = read_datafile("plus.json") |
| 662 | |
| 663 | with mock.patch("googleapiclient._auth.default_credentials") as default: |
| 664 | plus = build_from_document( |
| 665 | discovery, |
| 666 | client_options=google.api_core.client_options.ClientOptions( |
| 667 | quota_project_id="my-project" |
| 668 | ), |
| 669 | ) |
| 670 | |
| 671 | default.assert_called_once_with(scopes=None, quota_project_id="my-project") |
| 672 | |
| 673 | def test_credentials_file_from_client_options(self): |
| 674 | discovery = read_datafile("plus.json") |
| 675 | |
| 676 | with mock.patch("googleapiclient._auth.credentials_from_file") as default: |
| 677 | plus = build_from_document( |
| 678 | discovery, |
| 679 | client_options=google.api_core.client_options.ClientOptions( |
| 680 | credentials_file="credentials.json" |
| 681 | ), |
| 682 | ) |
| 683 | |
| 684 | default.assert_called_once_with( |
| 685 | "credentials.json", scopes=None, quota_project_id=None |
| 686 | ) |
| 687 | |
Jon Wayne Parrott | 068eb35 | 2017-02-08 10:13:06 -0800 | [diff] [blame] | 688 | |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 689 | REGULAR_ENDPOINT = "https://www.googleapis.com/plus/v1/" |
| 690 | MTLS_ENDPOINT = "https://www.mtls.googleapis.com/plus/v1/" |
| 691 | |
| 692 | |
| 693 | class DiscoveryFromDocumentMutualTLS(unittest.TestCase): |
| 694 | MOCK_CREDENTIALS = mock.Mock(spec=google.auth.credentials.Credentials) |
| 695 | ADC_CERT_PATH = "adc_cert_path" |
| 696 | ADC_KEY_PATH = "adc_key_path" |
| 697 | ADC_PASSPHRASE = "adc_passphrase" |
| 698 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 699 | def check_http_client_cert(self, resource, has_client_cert="false"): |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 700 | if isinstance(resource._http, google_auth_httplib2.AuthorizedHttp): |
| 701 | certs = list(resource._http.http.certificates.iter("")) |
| 702 | else: |
| 703 | certs = list(resource._http.certificates.iter("")) |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 704 | if has_client_cert == "true": |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 705 | self.assertEqual(len(certs), 1) |
| 706 | self.assertEqual( |
| 707 | certs[0], (self.ADC_KEY_PATH, self.ADC_CERT_PATH, self.ADC_PASSPHRASE) |
| 708 | ) |
| 709 | else: |
| 710 | self.assertEqual(len(certs), 0) |
| 711 | |
| 712 | def client_encrypted_cert_source(self): |
| 713 | return self.ADC_CERT_PATH, self.ADC_KEY_PATH, self.ADC_PASSPHRASE |
| 714 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 715 | @parameterized.expand( |
| 716 | [ |
| 717 | ("never", "true"), |
| 718 | ("auto", "true"), |
| 719 | ("always", "true"), |
| 720 | ("never", "false"), |
| 721 | ("auto", "false"), |
| 722 | ("always", "false"), |
| 723 | ] |
| 724 | ) |
| 725 | def test_mtls_not_trigger_if_http_provided(self, use_mtls_env, use_client_cert): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 726 | discovery = read_datafile("plus.json") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 727 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 728 | with mock.patch.dict( |
| 729 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 730 | ): |
| 731 | with mock.patch.dict( |
| 732 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 733 | ): |
| 734 | plus = build_from_document(discovery, http=httplib2.Http()) |
| 735 | self.assertIsNotNone(plus) |
| 736 | self.assertEqual(plus._baseUrl, REGULAR_ENDPOINT) |
| 737 | self.check_http_client_cert(plus, has_client_cert="false") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 738 | |
| 739 | @parameterized.expand( |
| 740 | [ |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 741 | ("never", "true"), |
| 742 | ("auto", "true"), |
| 743 | ("always", "true"), |
| 744 | ("never", "false"), |
| 745 | ("auto", "false"), |
| 746 | ("always", "false"), |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 747 | ] |
| 748 | ) |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 749 | def test_exception_with_client_cert_source(self, use_mtls_env, use_client_cert): |
| 750 | discovery = read_datafile("plus.json") |
| 751 | with mock.patch.dict( |
| 752 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 753 | ): |
| 754 | with mock.patch.dict( |
| 755 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 756 | ): |
| 757 | with self.assertRaises(MutualTLSChannelError): |
| 758 | build_from_document( |
| 759 | discovery, |
| 760 | credentials=self.MOCK_CREDENTIALS, |
| 761 | client_options={"client_cert_source": mock.Mock()}, |
| 762 | ) |
| 763 | |
| 764 | @parameterized.expand( |
| 765 | [ |
| 766 | ("never", "true", REGULAR_ENDPOINT), |
| 767 | ("auto", "true", MTLS_ENDPOINT), |
| 768 | ("always", "true", MTLS_ENDPOINT), |
| 769 | ("never", "false", REGULAR_ENDPOINT), |
| 770 | ("auto", "false", REGULAR_ENDPOINT), |
| 771 | ("always", "false", MTLS_ENDPOINT), |
| 772 | ] |
| 773 | ) |
| 774 | def test_mtls_with_provided_client_cert( |
| 775 | self, use_mtls_env, use_client_cert, base_url |
| 776 | ): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 777 | discovery = read_datafile("plus.json") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 778 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 779 | with mock.patch.dict( |
| 780 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 781 | ): |
| 782 | with mock.patch.dict( |
| 783 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 784 | ): |
| 785 | plus = build_from_document( |
| 786 | discovery, |
| 787 | credentials=self.MOCK_CREDENTIALS, |
| 788 | client_options={ |
| 789 | "client_encrypted_cert_source": self.client_encrypted_cert_source |
| 790 | }, |
| 791 | ) |
| 792 | self.assertIsNotNone(plus) |
| 793 | self.check_http_client_cert(plus, has_client_cert=use_client_cert) |
| 794 | self.assertEqual(plus._baseUrl, base_url) |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 795 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 796 | @parameterized.expand( |
| 797 | [ |
| 798 | ("never", "true"), |
| 799 | ("auto", "true"), |
| 800 | ("always", "true"), |
| 801 | ("never", "false"), |
| 802 | ("auto", "false"), |
| 803 | ("always", "false"), |
| 804 | ] |
| 805 | ) |
| 806 | def test_endpoint_not_switch(self, use_mtls_env, use_client_cert): |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 807 | # Test endpoint is not switched if user provided api endpoint |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 808 | discovery = read_datafile("plus.json") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 809 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 810 | with mock.patch.dict( |
| 811 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 812 | ): |
| 813 | with mock.patch.dict( |
| 814 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 815 | ): |
| 816 | plus = build_from_document( |
| 817 | discovery, |
| 818 | credentials=self.MOCK_CREDENTIALS, |
| 819 | client_options={ |
| 820 | "api_endpoint": "https://foo.googleapis.com", |
| 821 | "client_encrypted_cert_source": self.client_encrypted_cert_source, |
| 822 | }, |
| 823 | ) |
| 824 | self.assertIsNotNone(plus) |
| 825 | self.check_http_client_cert(plus, has_client_cert=use_client_cert) |
| 826 | self.assertEqual(plus._baseUrl, "https://foo.googleapis.com") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 827 | |
| 828 | @parameterized.expand( |
| 829 | [ |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 830 | ("never", "true", REGULAR_ENDPOINT), |
| 831 | ("auto", "true", MTLS_ENDPOINT), |
| 832 | ("always", "true", MTLS_ENDPOINT), |
| 833 | ("never", "false", REGULAR_ENDPOINT), |
| 834 | ("auto", "false", REGULAR_ENDPOINT), |
| 835 | ("always", "false", MTLS_ENDPOINT), |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 836 | ] |
| 837 | ) |
| 838 | @mock.patch( |
| 839 | "google.auth.transport.mtls.has_default_client_cert_source", autospec=True |
| 840 | ) |
| 841 | @mock.patch( |
| 842 | "google.auth.transport.mtls.default_client_encrypted_cert_source", autospec=True |
| 843 | ) |
| 844 | def test_mtls_with_default_client_cert( |
| 845 | self, |
| 846 | use_mtls_env, |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 847 | use_client_cert, |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 848 | base_url, |
| 849 | default_client_encrypted_cert_source, |
| 850 | has_default_client_cert_source, |
| 851 | ): |
| 852 | has_default_client_cert_source.return_value = True |
| 853 | default_client_encrypted_cert_source.return_value = ( |
| 854 | self.client_encrypted_cert_source |
| 855 | ) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 856 | discovery = read_datafile("plus.json") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 857 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 858 | with mock.patch.dict( |
| 859 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 860 | ): |
| 861 | with mock.patch.dict( |
| 862 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 863 | ): |
| 864 | plus = build_from_document( |
| 865 | discovery, |
| 866 | credentials=self.MOCK_CREDENTIALS, |
| 867 | adc_cert_path=self.ADC_CERT_PATH, |
| 868 | adc_key_path=self.ADC_KEY_PATH, |
| 869 | ) |
| 870 | self.assertIsNotNone(plus) |
| 871 | self.check_http_client_cert(plus, has_client_cert=use_client_cert) |
| 872 | self.assertEqual(plus._baseUrl, base_url) |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 873 | |
| 874 | @parameterized.expand( |
| 875 | [ |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 876 | ("never", "true", REGULAR_ENDPOINT), |
| 877 | ("auto", "true", REGULAR_ENDPOINT), |
| 878 | ("always", "true", MTLS_ENDPOINT), |
| 879 | ("never", "false", REGULAR_ENDPOINT), |
| 880 | ("auto", "false", REGULAR_ENDPOINT), |
| 881 | ("always", "false", MTLS_ENDPOINT), |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 882 | ] |
| 883 | ) |
| 884 | @mock.patch( |
| 885 | "google.auth.transport.mtls.has_default_client_cert_source", autospec=True |
| 886 | ) |
| 887 | def test_mtls_with_no_client_cert( |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 888 | self, use_mtls_env, use_client_cert, base_url, has_default_client_cert_source |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 889 | ): |
| 890 | has_default_client_cert_source.return_value = False |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 891 | discovery = read_datafile("plus.json") |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 892 | |
arithmetic1728 | 2fc5ca1 | 2020-08-27 14:08:12 -0700 | [diff] [blame] | 893 | with mock.patch.dict( |
| 894 | "os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env} |
| 895 | ): |
| 896 | with mock.patch.dict( |
| 897 | "os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert} |
| 898 | ): |
| 899 | plus = build_from_document( |
| 900 | discovery, |
| 901 | credentials=self.MOCK_CREDENTIALS, |
| 902 | adc_cert_path=self.ADC_CERT_PATH, |
| 903 | adc_key_path=self.ADC_KEY_PATH, |
| 904 | ) |
| 905 | self.assertIsNotNone(plus) |
| 906 | self.check_http_client_cert(plus, has_client_cert="false") |
| 907 | self.assertEqual(plus._baseUrl, base_url) |
arithmetic1728 | 981eadf | 2020-06-02 10:20:10 -0700 | [diff] [blame] | 908 | |
| 909 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 910 | class DiscoveryFromHttp(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 911 | def setUp(self): |
| 912 | self.old_environ = os.environ.copy() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 913 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 914 | def tearDown(self): |
| 915 | os.environ = self.old_environ |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 916 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 917 | def test_userip_is_added_to_discovery_uri(self): |
| 918 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 919 | # out of the raised exception. |
| 920 | os.environ["REMOTE_ADDR"] = "10.0.0.1" |
| 921 | try: |
| 922 | http = HttpMockSequence( |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 923 | [({"status": "400"}, read_datafile("zoo.json", "rb"))] |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 924 | ) |
| 925 | zoo = build( |
| 926 | "zoo", |
| 927 | "v1", |
| 928 | http=http, |
| 929 | developerKey=None, |
Anthonios Partheniou | 3b4f2e2 | 2021-03-19 11:36:01 -0400 | [diff] [blame] | 930 | discoveryServiceUrl="http://example.com" |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 931 | ) |
| 932 | self.fail("Should have raised an exception.") |
| 933 | except HttpError as e: |
| 934 | self.assertEqual(e.uri, "http://example.com?userIp=10.0.0.1") |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 935 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 936 | def test_userip_missing_is_not_added_to_discovery_uri(self): |
| 937 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 938 | # out of the raised exception. |
| 939 | try: |
| 940 | http = HttpMockSequence( |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 941 | [({"status": "400"}, read_datafile("zoo.json", "rb"))] |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 942 | ) |
| 943 | zoo = build( |
| 944 | "zoo", |
| 945 | "v1", |
| 946 | http=http, |
| 947 | developerKey=None, |
Anthonios Partheniou | 3b4f2e2 | 2021-03-19 11:36:01 -0400 | [diff] [blame] | 948 | discoveryServiceUrl="http://example.com" |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 949 | ) |
| 950 | self.fail("Should have raised an exception.") |
| 951 | except HttpError as e: |
| 952 | self.assertEqual(e.uri, "http://example.com") |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 953 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 954 | def test_key_is_added_to_discovery_uri(self): |
| 955 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 956 | # out of the raised exception. |
| 957 | try: |
| 958 | http = HttpMockSequence( |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 959 | [({"status": "400"}, read_datafile("zoo.json", "rb"))] |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 960 | ) |
| 961 | zoo = build( |
| 962 | "zoo", |
| 963 | "v1", |
| 964 | http=http, |
| 965 | developerKey="foo", |
| 966 | discoveryServiceUrl="http://example.com", |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 967 | static_discovery=False, |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 968 | ) |
| 969 | self.fail("Should have raised an exception.") |
| 970 | except HttpError as e: |
| 971 | self.assertEqual(e.uri, "http://example.com?key=foo") |
Arunpn | 9d779cc | 2018-11-30 10:25:01 -0800 | [diff] [blame] | 972 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 973 | def test_discovery_loading_from_v2_discovery_uri(self): |
| 974 | http = HttpMockSequence( |
| 975 | [ |
| 976 | ({"status": "404"}, "Not found"), |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 977 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 978 | ] |
| 979 | ) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 980 | zoo = build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 981 | self.assertTrue(hasattr(zoo, "animals")) |
| 982 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 983 | def test_api_endpoint_override_from_client_options(self): |
| 984 | http = HttpMockSequence( |
| 985 | [ |
| 986 | ({"status": "404"}, "Not found"), |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 987 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 988 | ] |
| 989 | ) |
| 990 | api_endpoint = "https://foo.googleapis.com/" |
| 991 | options = google.api_core.client_options.ClientOptions( |
| 992 | api_endpoint=api_endpoint |
| 993 | ) |
| 994 | zoo = build( |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 995 | "zoo", "v1", http=http, cache_discovery=False, client_options=options, static_discovery=False |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 996 | ) |
| 997 | self.assertEqual(zoo._baseUrl, api_endpoint) |
| 998 | |
| 999 | def test_api_endpoint_override_from_client_options_dict(self): |
| 1000 | http = HttpMockSequence( |
| 1001 | [ |
| 1002 | ({"status": "404"}, "Not found"), |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1003 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1004 | ] |
| 1005 | ) |
| 1006 | api_endpoint = "https://foo.googleapis.com/" |
| 1007 | zoo = build( |
| 1008 | "zoo", |
| 1009 | "v1", |
| 1010 | http=http, |
| 1011 | cache_discovery=False, |
| 1012 | client_options={"api_endpoint": api_endpoint}, |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1013 | static_discovery=False, |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1014 | ) |
| 1015 | self.assertEqual(zoo._baseUrl, api_endpoint) |
| 1016 | |
Dmitry Frenkel | cd4e8f4 | 2020-07-30 12:34:03 -0700 | [diff] [blame] | 1017 | def test_discovery_with_empty_version_uses_v2(self): |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 1018 | http = HttpMockSequence([({"status": "200"}, read_datafile("zoo.json", "rb")),]) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1019 | build("zoo", version=None, http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | cd4e8f4 | 2020-07-30 12:34:03 -0700 | [diff] [blame] | 1020 | validate_discovery_requests(self, http, "zoo", None, V2_DISCOVERY_URI) |
| 1021 | |
| 1022 | def test_discovery_with_empty_version_preserves_custom_uri(self): |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 1023 | http = HttpMockSequence([({"status": "200"}, read_datafile("zoo.json", "rb")),]) |
Dmitry Frenkel | cd4e8f4 | 2020-07-30 12:34:03 -0700 | [diff] [blame] | 1024 | custom_discovery_uri = "https://foo.bar/$discovery" |
| 1025 | build( |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 1026 | "zoo", |
| 1027 | version=None, |
| 1028 | http=http, |
| 1029 | cache_discovery=False, |
| 1030 | discoveryServiceUrl=custom_discovery_uri, |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1031 | static_discovery=False, |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 1032 | ) |
| 1033 | validate_discovery_requests(self, http, "zoo", None, custom_discovery_uri) |
Dmitry Frenkel | cd4e8f4 | 2020-07-30 12:34:03 -0700 | [diff] [blame] | 1034 | |
| 1035 | def test_discovery_with_valid_version_uses_v1(self): |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 1036 | http = HttpMockSequence([({"status": "200"}, read_datafile("zoo.json", "rb")),]) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1037 | build("zoo", version="v123", http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | cd4e8f4 | 2020-07-30 12:34:03 -0700 | [diff] [blame] | 1038 | validate_discovery_requests(self, http, "zoo", "v123", V1_DISCOVERY_URI) |
| 1039 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 1040 | |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1041 | class DiscoveryRetryFromHttp(unittest.TestCase): |
| 1042 | def test_repeated_500_retries_and_fails(self): |
| 1043 | http = HttpMockSequence( |
| 1044 | [ |
| 1045 | ({"status": "500"}, read_datafile("500.json", "rb")), |
| 1046 | ({"status": "503"}, read_datafile("503.json", "rb")), |
| 1047 | ] |
| 1048 | ) |
| 1049 | with self.assertRaises(HttpError): |
| 1050 | with mock.patch("time.sleep") as mocked_sleep: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1051 | build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1052 | |
| 1053 | mocked_sleep.assert_called_once() |
| 1054 | # We also want to verify that we stayed with v1 discovery |
| 1055 | validate_discovery_requests(self, http, "zoo", "v1", V1_DISCOVERY_URI) |
| 1056 | |
| 1057 | def test_v2_repeated_500_retries_and_fails(self): |
| 1058 | http = HttpMockSequence( |
| 1059 | [ |
| 1060 | ({"status": "404"}, "Not found"), # last v1 discovery call |
| 1061 | ({"status": "500"}, read_datafile("500.json", "rb")), |
| 1062 | ({"status": "503"}, read_datafile("503.json", "rb")), |
| 1063 | ] |
| 1064 | ) |
| 1065 | with self.assertRaises(HttpError): |
| 1066 | with mock.patch("time.sleep") as mocked_sleep: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1067 | build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1068 | |
| 1069 | mocked_sleep.assert_called_once() |
| 1070 | # We also want to verify that we switched to v2 discovery |
| 1071 | validate_discovery_requests(self, http, "zoo", "v1", V2_DISCOVERY_URI) |
| 1072 | |
| 1073 | def test_single_500_retries_and_succeeds(self): |
| 1074 | http = HttpMockSequence( |
| 1075 | [ |
| 1076 | ({"status": "500"}, read_datafile("500.json", "rb")), |
| 1077 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
| 1078 | ] |
| 1079 | ) |
| 1080 | with mock.patch("time.sleep") as mocked_sleep: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1081 | zoo = build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1082 | |
| 1083 | self.assertTrue(hasattr(zoo, "animals")) |
| 1084 | mocked_sleep.assert_called_once() |
| 1085 | # We also want to verify that we stayed with v1 discovery |
| 1086 | validate_discovery_requests(self, http, "zoo", "v1", V1_DISCOVERY_URI) |
| 1087 | |
| 1088 | def test_single_500_then_404_retries_and_succeeds(self): |
| 1089 | http = HttpMockSequence( |
| 1090 | [ |
| 1091 | ({"status": "500"}, read_datafile("500.json", "rb")), |
| 1092 | ({"status": "404"}, "Not found"), # last v1 discovery call |
| 1093 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
| 1094 | ] |
| 1095 | ) |
| 1096 | with mock.patch("time.sleep") as mocked_sleep: |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1097 | zoo = build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1098 | |
| 1099 | self.assertTrue(hasattr(zoo, "animals")) |
| 1100 | mocked_sleep.assert_called_once() |
| 1101 | # We also want to verify that we switched to v2 discovery |
| 1102 | validate_discovery_requests(self, http, "zoo", "v1", V2_DISCOVERY_URI) |
| 1103 | |
| 1104 | |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1105 | class DiscoveryFromAppEngineCache(unittest.TestCase): |
Zev Goldstein | 09e6447 | 2020-05-14 16:29:20 -0400 | [diff] [blame] | 1106 | def setUp(self): |
| 1107 | self.old_environ = os.environ.copy() |
| 1108 | os.environ["APPENGINE_RUNTIME"] = "python27" |
| 1109 | |
| 1110 | def tearDown(self): |
| 1111 | os.environ = self.old_environ |
| 1112 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1113 | def test_appengine_memcache(self): |
| 1114 | # Hack module import |
| 1115 | self.orig_import = __import__ |
| 1116 | self.mocked_api = mock.MagicMock() |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1117 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1118 | def import_mock(name, *args, **kwargs): |
| 1119 | if name == "google.appengine.api": |
| 1120 | return self.mocked_api |
| 1121 | return self.orig_import(name, *args, **kwargs) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1122 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1123 | import_fullname = "__builtin__.__import__" |
| 1124 | if sys.version_info[0] >= 3: |
| 1125 | import_fullname = "builtins.__import__" |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1126 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1127 | with mock.patch(import_fullname, side_effect=import_mock): |
| 1128 | namespace = "google-api-client" |
| 1129 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1130 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1131 | self.mocked_api.memcache.get.return_value = None |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1132 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1133 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1134 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1135 | # memcache.get is called once |
| 1136 | url = "https://www.googleapis.com/discovery/v1/apis/plus/v1/rest" |
| 1137 | self.mocked_api.memcache.get.assert_called_once_with( |
| 1138 | url, namespace=namespace |
| 1139 | ) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1140 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1141 | # memcache.set is called once |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1142 | content = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1143 | self.mocked_api.memcache.set.assert_called_once_with( |
| 1144 | url, content, time=DISCOVERY_DOC_MAX_AGE, namespace=namespace |
| 1145 | ) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1146 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1147 | # Returns the cached content this time. |
| 1148 | self.mocked_api.memcache.get.return_value = content |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1149 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1150 | # Make sure the contents are returned from the cache. |
| 1151 | # (Otherwise it should through an error) |
| 1152 | self.http = HttpMock(None, {"status": "200"}) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1153 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1154 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1155 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1156 | # memcache.get is called twice |
| 1157 | self.mocked_api.memcache.get.assert_has_calls( |
| 1158 | [ |
| 1159 | mock.call(url, namespace=namespace), |
| 1160 | mock.call(url, namespace=namespace), |
| 1161 | ] |
| 1162 | ) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1163 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1164 | # memcahce.set is called just once |
| 1165 | self.mocked_api.memcache.set.assert_called_once_with( |
| 1166 | url, content, time=DISCOVERY_DOC_MAX_AGE, namespace=namespace |
| 1167 | ) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1168 | |
| 1169 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1170 | class DiscoveryFromStaticDocument(unittest.TestCase): |
| 1171 | def test_retrieve_from_local_when_static_discovery_true(self): |
| 1172 | http = HttpMockSequence([({"status": "400"}, "")]) |
| 1173 | drive = build("drive", "v3", http=http, cache_discovery=False, |
| 1174 | static_discovery=True) |
| 1175 | self.assertIsNotNone(drive) |
| 1176 | self.assertTrue(hasattr(drive, "files")) |
| 1177 | |
| 1178 | def test_retrieve_from_internet_when_static_discovery_false(self): |
| 1179 | http = HttpMockSequence([({"status": "400"}, "")]) |
| 1180 | with self.assertRaises(HttpError): |
| 1181 | build("drive", "v3", http=http, cache_discovery=False, |
| 1182 | static_discovery=False) |
| 1183 | |
| 1184 | def test_unknown_api_when_static_discovery_true(self): |
| 1185 | with self.assertRaises(UnknownApiNameOrVersion): |
| 1186 | build("doesnotexist", "v3", cache_discovery=False, |
| 1187 | static_discovery=True) |
| 1188 | |
| 1189 | |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1190 | class DictCache(Cache): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1191 | def __init__(self): |
| 1192 | self.d = {} |
| 1193 | |
| 1194 | def get(self, url): |
| 1195 | return self.d.get(url, None) |
| 1196 | |
| 1197 | def set(self, url, content): |
| 1198 | self.d[url] = content |
| 1199 | |
| 1200 | def contains(self, url): |
| 1201 | return url in self.d |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1202 | |
| 1203 | |
| 1204 | class DiscoveryFromFileCache(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1205 | def test_file_based_cache(self): |
| 1206 | cache = mock.Mock(wraps=DictCache()) |
| 1207 | with mock.patch( |
| 1208 | "googleapiclient.discovery_cache.autodetect", return_value=cache |
| 1209 | ): |
| 1210 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1211 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1212 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1213 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1214 | # cache.get is called once |
| 1215 | url = "https://www.googleapis.com/discovery/v1/apis/plus/v1/rest" |
| 1216 | cache.get.assert_called_once_with(url) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1217 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1218 | # cache.set is called once |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1219 | content = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1220 | cache.set.assert_called_once_with(url, content) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1221 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1222 | # Make sure there is a cache entry for the plus v1 discovery doc. |
| 1223 | self.assertTrue(cache.contains(url)) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1224 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1225 | # Make sure the contents are returned from the cache. |
| 1226 | # (Otherwise it should through an error) |
| 1227 | self.http = HttpMock(None, {"status": "200"}) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1228 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1229 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1230 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1231 | # cache.get is called twice |
| 1232 | cache.get.assert_has_calls([mock.call(url), mock.call(url)]) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1233 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1234 | # cahce.set is called just once |
| 1235 | cache.set.assert_called_once_with(url, content) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1236 | |
| 1237 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1238 | class Discovery(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1239 | def test_method_error_checking(self): |
| 1240 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1241 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 1242 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1243 | # Missing required parameters |
| 1244 | try: |
| 1245 | plus.activities().list() |
| 1246 | self.fail() |
| 1247 | except TypeError as e: |
| 1248 | self.assertTrue("Missing" in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1249 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1250 | # Missing required parameters even if supplied as None. |
| 1251 | try: |
| 1252 | plus.activities().list(collection=None, userId=None) |
| 1253 | self.fail() |
| 1254 | except TypeError as e: |
| 1255 | self.assertTrue("Missing" in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1256 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1257 | # Parameter doesn't match regex |
| 1258 | try: |
| 1259 | plus.activities().list(collection="not_a_collection_name", userId="me") |
| 1260 | self.fail() |
| 1261 | except TypeError as e: |
| 1262 | self.assertTrue("not an allowed value" in str(e)) |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 1263 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1264 | # Unexpected parameter |
| 1265 | try: |
| 1266 | plus.activities().list(flubber=12) |
| 1267 | self.fail() |
| 1268 | except TypeError as e: |
| 1269 | self.assertTrue("unexpected" in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1270 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1271 | def _check_query_types(self, request): |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1272 | parsed = urllib.parse.urlparse(request.uri) |
| 1273 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1274 | self.assertEqual(q["q"], ["foo"]) |
| 1275 | self.assertEqual(q["i"], ["1"]) |
| 1276 | self.assertEqual(q["n"], ["1.0"]) |
| 1277 | self.assertEqual(q["b"], ["false"]) |
| 1278 | self.assertEqual(q["a"], ["[1, 2, 3]"]) |
| 1279 | self.assertEqual(q["o"], ["{'a': 1}"]) |
| 1280 | self.assertEqual(q["e"], ["bar"]) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1281 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1282 | def test_type_coercion(self): |
| 1283 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1284 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 1285 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1286 | request = zoo.query( |
| 1287 | q="foo", i=1.0, n=1.0, b=0, a=[1, 2, 3], o={"a": 1}, e="bar" |
| 1288 | ) |
| 1289 | self._check_query_types(request) |
| 1290 | request = zoo.query( |
| 1291 | q="foo", i=1, n=1, b=False, a=[1, 2, 3], o={"a": 1}, e="bar" |
| 1292 | ) |
| 1293 | self._check_query_types(request) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 1294 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1295 | request = zoo.query( |
| 1296 | q="foo", i="1", n="1", b="", a=[1, 2, 3], o={"a": 1}, e="bar", er="two" |
| 1297 | ) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 1298 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1299 | request = zoo.query( |
| 1300 | q="foo", |
| 1301 | i="1", |
| 1302 | n="1", |
| 1303 | b="", |
| 1304 | a=[1, 2, 3], |
| 1305 | o={"a": 1}, |
| 1306 | e="bar", |
| 1307 | er=["one", "three"], |
| 1308 | rr=["foo", "bar"], |
| 1309 | ) |
| 1310 | self._check_query_types(request) |
Joe Gregorio | 6804c7a | 2011-11-18 14:30:32 -0500 | [diff] [blame] | 1311 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1312 | # Five is right out. |
| 1313 | self.assertRaises(TypeError, zoo.query, er=["one", "five"]) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 1314 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1315 | def test_optional_stack_query_parameters(self): |
| 1316 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1317 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1318 | request = zoo.query(trace="html", fields="description") |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 1319 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1320 | parsed = urllib.parse.urlparse(request.uri) |
| 1321 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1322 | self.assertEqual(q["trace"], ["html"]) |
| 1323 | self.assertEqual(q["fields"], ["description"]) |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 1324 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1325 | def test_string_params_value_of_none_get_dropped(self): |
| 1326 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1327 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1328 | request = zoo.query(trace=None, fields="description") |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1329 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1330 | parsed = urllib.parse.urlparse(request.uri) |
| 1331 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1332 | self.assertFalse("trace" in q) |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 1333 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1334 | def test_model_added_query_parameters(self): |
| 1335 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1336 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1337 | request = zoo.animals().get(name="Lion") |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 1338 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1339 | parsed = urllib.parse.urlparse(request.uri) |
| 1340 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1341 | self.assertEqual(q["alt"], ["json"]) |
| 1342 | self.assertEqual(request.headers["accept"], "application/json") |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 1343 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1344 | def test_fallback_to_raw_model(self): |
| 1345 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1346 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1347 | request = zoo.animals().getmedia(name="Lion") |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 1348 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1349 | parsed = urllib.parse.urlparse(request.uri) |
| 1350 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1351 | self.assertTrue("alt" not in q) |
| 1352 | self.assertEqual(request.headers["accept"], "*/*") |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 1353 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1354 | def test_patch(self): |
| 1355 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1356 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1357 | request = zoo.animals().patch(name="lion", body='{"description": "foo"}') |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 1358 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1359 | self.assertEqual(request.method, "PATCH") |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1360 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1361 | def test_batch_request_from_discovery(self): |
| 1362 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
| 1363 | # zoo defines a batchPath |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1364 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1365 | batch_request = zoo.new_batch_http_request() |
| 1366 | self.assertEqual( |
| 1367 | batch_request._batch_uri, "https://www.googleapis.com/batchZoo" |
| 1368 | ) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1369 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1370 | def test_batch_request_from_default(self): |
| 1371 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
| 1372 | # plus does not define a batchPath |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1373 | plus = build("plus", "v1", http=self.http, cache_discovery=False, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1374 | batch_request = plus.new_batch_http_request() |
| 1375 | self.assertEqual(batch_request._batch_uri, "https://www.googleapis.com/batch") |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 1376 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1377 | def test_tunnel_patch(self): |
| 1378 | http = HttpMockSequence( |
| 1379 | [ |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1380 | ({"status": "200"}, read_datafile("zoo.json", "rb")), |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1381 | ({"status": "200"}, "echo_request_headers_as_json"), |
| 1382 | ] |
| 1383 | ) |
| 1384 | http = tunnel_patch(http) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1385 | zoo = build("zoo", "v1", http=http, cache_discovery=False, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1386 | resp = zoo.animals().patch(name="lion", body='{"description": "foo"}').execute() |
Pepper Lebeck-Jobe | 860836f | 2015-06-12 20:42:23 -0400 | [diff] [blame] | 1387 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1388 | self.assertTrue("x-http-method-override" in resp) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 1389 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1390 | def test_plus_resources(self): |
| 1391 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1392 | plus = build("plus", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1393 | self.assertTrue(getattr(plus, "activities")) |
| 1394 | self.assertTrue(getattr(plus, "people")) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 1395 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1396 | def test_oauth2client_credentials(self): |
| 1397 | credentials = mock.Mock(spec=GoogleCredentials) |
| 1398 | credentials.create_scoped_required.return_value = False |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 1399 | |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1400 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1401 | service = build_from_document(discovery, credentials=credentials) |
| 1402 | self.assertEqual(service._http, credentials.authorize.return_value) |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 1403 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1404 | def test_google_auth_credentials(self): |
| 1405 | credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1406 | discovery = read_datafile("plus.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1407 | service = build_from_document(discovery, credentials=credentials) |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 1408 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1409 | self.assertIsInstance(service._http, google_auth_httplib2.AuthorizedHttp) |
| 1410 | self.assertEqual(service._http.credentials, credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 1411 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1412 | def test_no_scopes_no_credentials(self): |
| 1413 | # Zoo doesn't have scopes |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1414 | discovery = read_datafile("zoo.json") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1415 | service = build_from_document(discovery) |
| 1416 | # Should be an ordinary httplib2.Http instance and not AuthorizedHttp. |
| 1417 | self.assertIsInstance(service._http, httplib2.Http) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 1418 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1419 | def test_full_featured(self): |
| 1420 | # Zoo should exercise all discovery facets |
| 1421 | # and should also have no future.json file. |
| 1422 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1423 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1424 | self.assertTrue(getattr(zoo, "animals")) |
Orest Bolohan | e92c900 | 2014-05-30 11:15:43 -0700 | [diff] [blame] | 1425 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1426 | request = zoo.animals().list(name="bat", projection="full") |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1427 | parsed = urllib.parse.urlparse(request.uri) |
| 1428 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1429 | self.assertEqual(q["name"], ["bat"]) |
| 1430 | self.assertEqual(q["projection"], ["full"]) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 1431 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1432 | def test_nested_resources(self): |
| 1433 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1434 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1435 | self.assertTrue(getattr(zoo, "animals")) |
| 1436 | request = zoo.my().favorites().list(max_results="5") |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1437 | parsed = urllib.parse.urlparse(request.uri) |
| 1438 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1439 | self.assertEqual(q["max-results"], ["5"]) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 1440 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1441 | def test_top_level_functions(self): |
| 1442 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1443 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1444 | self.assertTrue(getattr(zoo, "query")) |
| 1445 | request = zoo.query(q="foo") |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1446 | parsed = urllib.parse.urlparse(request.uri) |
| 1447 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1448 | self.assertEqual(q["q"], ["foo"]) |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 1449 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1450 | def test_simple_media_uploads(self): |
| 1451 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1452 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1453 | doc = getattr(zoo.animals().insert, "__doc__") |
| 1454 | self.assertTrue("media_body" in doc) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 1455 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1456 | def test_simple_media_upload_no_max_size_provided(self): |
| 1457 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1458 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1459 | request = zoo.animals().crossbreed(media_body=datafile("small.png")) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1460 | self.assertEqual("image/png", request.headers["content-type"]) |
| 1461 | self.assertEqual(b"PNG", request.body[1:4]) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1462 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1463 | def test_simple_media_raise_correct_exceptions(self): |
| 1464 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1465 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 1466 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1467 | try: |
| 1468 | zoo.animals().insert(media_body=datafile("smiley.png")) |
| 1469 | self.fail("should throw exception if media is too large.") |
| 1470 | except MediaUploadSizeError: |
| 1471 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1472 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1473 | try: |
| 1474 | zoo.animals().insert(media_body=datafile("small.jpg")) |
| 1475 | self.fail("should throw exception if mimetype is unacceptable.") |
| 1476 | except UnacceptableMimeTypeError: |
| 1477 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1478 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1479 | def test_simple_media_good_upload(self): |
| 1480 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1481 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1482 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1483 | request = zoo.animals().insert(media_body=datafile("small.png")) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1484 | self.assertEqual("image/png", request.headers["content-type"]) |
| 1485 | self.assertEqual(b"PNG", request.body[1:4]) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1486 | assertUrisEqual( |
| 1487 | self, |
| 1488 | "https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json", |
| 1489 | request.uri, |
| 1490 | ) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1491 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1492 | def test_simple_media_unknown_mimetype(self): |
| 1493 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1494 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1495 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1496 | try: |
| 1497 | zoo.animals().insert(media_body=datafile("small-png")) |
| 1498 | self.fail("should throw exception if mimetype is unknown.") |
| 1499 | except UnknownFileType: |
| 1500 | pass |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 1501 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1502 | request = zoo.animals().insert( |
| 1503 | media_body=datafile("small-png"), media_mime_type="image/png" |
| 1504 | ) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1505 | self.assertEqual("image/png", request.headers["content-type"]) |
| 1506 | self.assertEqual(b"PNG", request.body[1:4]) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1507 | assertUrisEqual( |
| 1508 | self, |
| 1509 | "https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json", |
| 1510 | request.uri, |
| 1511 | ) |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 1512 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1513 | def test_multipart_media_raise_correct_exceptions(self): |
| 1514 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1515 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Brian J. Watson | 38051ac | 2016-10-25 07:53:08 -0700 | [diff] [blame] | 1516 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1517 | try: |
| 1518 | zoo.animals().insert(media_body=datafile("smiley.png"), body={}) |
| 1519 | self.fail("should throw exception if media is too large.") |
| 1520 | except MediaUploadSizeError: |
| 1521 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1522 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1523 | try: |
| 1524 | zoo.animals().insert(media_body=datafile("small.jpg"), body={}) |
| 1525 | self.fail("should throw exception if mimetype is unacceptable.") |
| 1526 | except UnacceptableMimeTypeError: |
| 1527 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1528 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1529 | def test_multipart_media_good_upload(self, static_discovery=False): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1530 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1531 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1532 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1533 | request = zoo.animals().insert(media_body=datafile("small.png"), body={}) |
| 1534 | self.assertTrue(request.headers["content-type"].startswith("multipart/related")) |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1535 | contents = read_datafile("small.png", "rb") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1536 | boundary = re.match(b"--=+([^=]+)", request.body).group(1) |
| 1537 | self.assertEqual( |
| 1538 | request.body.rstrip(b"\n"), # Python 2.6 does not add a trailing \n |
| 1539 | b"--===============" |
| 1540 | + boundary |
| 1541 | + b"==\n" |
| 1542 | + b"Content-Type: application/json\n" |
| 1543 | + b"MIME-Version: 1.0\n\n" |
| 1544 | + b'{"data": {}}\n' |
| 1545 | + b"--===============" |
| 1546 | + boundary |
| 1547 | + b"==\n" |
| 1548 | + b"Content-Type: image/png\n" |
| 1549 | + b"MIME-Version: 1.0\n" |
| 1550 | + b"Content-Transfer-Encoding: binary\n\n" |
| 1551 | + contents |
| 1552 | + b"\n--===============" |
| 1553 | + boundary |
| 1554 | + b"==--", |
| 1555 | ) |
| 1556 | assertUrisEqual( |
| 1557 | self, |
| 1558 | "https://www.googleapis.com/upload/zoo/v1/animals?uploadType=multipart&alt=json", |
| 1559 | request.uri, |
| 1560 | ) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1561 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1562 | def test_media_capable_method_without_media(self): |
| 1563 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1564 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1565 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1566 | request = zoo.animals().insert(body={}) |
| 1567 | self.assertTrue(request.headers["content-type"], "application/json") |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 1568 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1569 | def test_resumable_multipart_media_good_upload(self): |
| 1570 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1571 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 1572 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1573 | media_upload = MediaFileUpload(datafile("small.png"), resumable=True) |
| 1574 | request = zoo.animals().insert(media_body=media_upload, body={}) |
| 1575 | self.assertTrue(request.headers["content-type"].startswith("application/json")) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1576 | self.assertEqual('{"data": {}}', request.body) |
| 1577 | self.assertEqual(media_upload, request.resumable) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1578 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1579 | self.assertEqual("image/png", request.resumable.mimetype()) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1580 | |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 1581 | self.assertNotEqual(request.body, None) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1582 | self.assertEqual(request.resumable_uri, None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1583 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1584 | http = HttpMockSequence( |
| 1585 | [ |
| 1586 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1587 | ({"status": "308", "location": "http://upload.example.com/2"}, ""), |
| 1588 | ( |
| 1589 | { |
| 1590 | "status": "308", |
| 1591 | "location": "http://upload.example.com/3", |
| 1592 | "range": "0-12", |
| 1593 | }, |
| 1594 | "", |
| 1595 | ), |
| 1596 | ( |
| 1597 | { |
| 1598 | "status": "308", |
| 1599 | "location": "http://upload.example.com/4", |
| 1600 | "range": "0-%d" % (media_upload.size() - 2), |
| 1601 | }, |
| 1602 | "", |
| 1603 | ), |
| 1604 | ({"status": "200"}, '{"foo": "bar"}'), |
| 1605 | ] |
| 1606 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1607 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1608 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1609 | self.assertEqual(None, body) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1610 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1611 | self.assertEqual(0, status.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1612 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1613 | # Two requests should have been made and the resumable_uri should have been |
| 1614 | # updated for each one. |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1615 | self.assertEqual(request.resumable_uri, "http://upload.example.com/2") |
| 1616 | self.assertEqual(media_upload, request.resumable) |
| 1617 | self.assertEqual(0, request.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1618 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1619 | # This next chuck call should upload the first chunk |
| 1620 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1621 | self.assertEqual(request.resumable_uri, "http://upload.example.com/3") |
| 1622 | self.assertEqual(media_upload, request.resumable) |
| 1623 | self.assertEqual(13, request.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1624 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1625 | # This call will upload the next chunk |
| 1626 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1627 | self.assertEqual(request.resumable_uri, "http://upload.example.com/4") |
| 1628 | self.assertEqual(media_upload.size() - 1, request.resumable_progress) |
| 1629 | self.assertEqual('{"data": {}}', request.body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1630 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1631 | # Final call to next_chunk should complete the upload. |
| 1632 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1633 | self.assertEqual(body, {"foo": "bar"}) |
| 1634 | self.assertEqual(status, None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1635 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1636 | def test_resumable_media_good_upload(self): |
| 1637 | """Not a multipart upload.""" |
| 1638 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1639 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1640 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1641 | media_upload = MediaFileUpload(datafile("small.png"), resumable=True) |
| 1642 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1643 | self.assertEqual(media_upload, request.resumable) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1644 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1645 | self.assertEqual("image/png", request.resumable.mimetype()) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1646 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1647 | self.assertEqual(request.body, None) |
| 1648 | self.assertEqual(request.resumable_uri, None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1649 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1650 | http = HttpMockSequence( |
| 1651 | [ |
| 1652 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1653 | ( |
| 1654 | { |
| 1655 | "status": "308", |
| 1656 | "location": "http://upload.example.com/2", |
| 1657 | "range": "0-12", |
| 1658 | }, |
| 1659 | "", |
| 1660 | ), |
| 1661 | ( |
| 1662 | { |
| 1663 | "status": "308", |
| 1664 | "location": "http://upload.example.com/3", |
| 1665 | "range": "0-%d" % (media_upload.size() - 2), |
| 1666 | }, |
| 1667 | "", |
| 1668 | ), |
| 1669 | ({"status": "200"}, '{"foo": "bar"}'), |
| 1670 | ] |
| 1671 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1672 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1673 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1674 | self.assertEqual(None, body) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1675 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1676 | self.assertEqual(13, status.resumable_progress) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1677 | |
| 1678 | # Two requests should have been made and the resumable_uri should have been |
| 1679 | # updated for each one. |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1680 | self.assertEqual(request.resumable_uri, "http://upload.example.com/2") |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1681 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1682 | self.assertEqual(media_upload, request.resumable) |
| 1683 | self.assertEqual(13, request.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1684 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1685 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1686 | self.assertEqual(request.resumable_uri, "http://upload.example.com/3") |
| 1687 | self.assertEqual(media_upload.size() - 1, request.resumable_progress) |
| 1688 | self.assertEqual(request.body, None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1689 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1690 | # Final call to next_chunk should complete the upload. |
| 1691 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1692 | self.assertEqual(body, {"foo": "bar"}) |
| 1693 | self.assertEqual(status, None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1694 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1695 | def test_resumable_media_good_upload_from_execute(self): |
| 1696 | """Not a multipart upload.""" |
| 1697 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1698 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1699 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1700 | media_upload = MediaFileUpload(datafile("small.png"), resumable=True) |
| 1701 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 1702 | assertUrisEqual( |
| 1703 | self, |
| 1704 | "https://www.googleapis.com/upload/zoo/v1/animals?uploadType=resumable&alt=json", |
| 1705 | request.uri, |
| 1706 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1707 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1708 | http = HttpMockSequence( |
| 1709 | [ |
| 1710 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1711 | ( |
| 1712 | { |
| 1713 | "status": "308", |
| 1714 | "location": "http://upload.example.com/2", |
| 1715 | "range": "0-12", |
| 1716 | }, |
| 1717 | "", |
| 1718 | ), |
| 1719 | ( |
| 1720 | { |
| 1721 | "status": "308", |
| 1722 | "location": "http://upload.example.com/3", |
| 1723 | "range": "0-%d" % media_upload.size(), |
| 1724 | }, |
| 1725 | "", |
| 1726 | ), |
| 1727 | ({"status": "200"}, '{"foo": "bar"}'), |
| 1728 | ] |
| 1729 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1730 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1731 | body = request.execute(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1732 | self.assertEqual(body, {"foo": "bar"}) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1733 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1734 | def test_resumable_media_fail_unknown_response_code_first_request(self): |
| 1735 | """Not a multipart upload.""" |
| 1736 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1737 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1738 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1739 | media_upload = MediaFileUpload(datafile("small.png"), resumable=True) |
| 1740 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1741 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1742 | http = HttpMockSequence( |
| 1743 | [({"status": "400", "location": "http://upload.example.com"}, "")] |
| 1744 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1745 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1746 | try: |
| 1747 | request.execute(http=http) |
| 1748 | self.fail("Should have raised ResumableUploadError.") |
| 1749 | except ResumableUploadError as e: |
| 1750 | self.assertEqual(400, e.resp.status) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1751 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1752 | def test_resumable_media_fail_unknown_response_code_subsequent_request(self): |
| 1753 | """Not a multipart upload.""" |
| 1754 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1755 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1756 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1757 | media_upload = MediaFileUpload(datafile("small.png"), resumable=True) |
| 1758 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1759 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1760 | http = HttpMockSequence( |
| 1761 | [ |
| 1762 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1763 | ({"status": "400"}, ""), |
| 1764 | ] |
| 1765 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1766 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1767 | self.assertRaises(HttpError, request.execute, http=http) |
| 1768 | self.assertTrue(request._in_error_state) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1769 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1770 | http = HttpMockSequence( |
| 1771 | [ |
| 1772 | ({"status": "308", "range": "0-5"}, ""), |
| 1773 | ({"status": "308", "range": "0-6"}, ""), |
| 1774 | ] |
| 1775 | ) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1776 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1777 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 1778 | self.assertEqual( |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1779 | status.resumable_progress, |
| 1780 | 7, |
| 1781 | "Should have first checked length and then tried to PUT more.", |
| 1782 | ) |
| 1783 | self.assertFalse(request._in_error_state) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1784 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1785 | # Put it back in an error state. |
| 1786 | http = HttpMockSequence([({"status": "400"}, "")]) |
| 1787 | self.assertRaises(HttpError, request.execute, http=http) |
| 1788 | self.assertTrue(request._in_error_state) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1789 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1790 | # Pretend the last request that 400'd actually succeeded. |
| 1791 | http = HttpMockSequence([({"status": "200"}, '{"foo": "bar"}')]) |
| 1792 | status, body = request.next_chunk(http=http) |
| 1793 | self.assertEqual(body, {"foo": "bar"}) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1794 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1795 | def test_media_io_base_stream_unlimited_chunksize_resume(self): |
| 1796 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1797 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1798 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1799 | # Set up a seekable stream and try to upload in single chunk. |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1800 | fd = io.BytesIO(b'01234"56789"') |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1801 | media_upload = MediaIoBaseUpload( |
| 1802 | fd=fd, mimetype="text/plain", chunksize=-1, resumable=True |
| 1803 | ) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1804 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1805 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1806 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1807 | # The single chunk fails, restart at the right point. |
| 1808 | http = HttpMockSequence( |
| 1809 | [ |
| 1810 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1811 | ( |
| 1812 | { |
| 1813 | "status": "308", |
| 1814 | "location": "http://upload.example.com/2", |
| 1815 | "range": "0-4", |
| 1816 | }, |
| 1817 | "", |
| 1818 | ), |
| 1819 | ({"status": "200"}, "echo_request_body"), |
| 1820 | ] |
| 1821 | ) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1822 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1823 | body = request.execute(http=http) |
| 1824 | self.assertEqual("56789", body) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1825 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1826 | def test_media_io_base_stream_chunksize_resume(self): |
| 1827 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1828 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1829 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1830 | # Set up a seekable stream and try to upload in chunks. |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1831 | fd = io.BytesIO(b"0123456789") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1832 | media_upload = MediaIoBaseUpload( |
| 1833 | fd=fd, mimetype="text/plain", chunksize=5, resumable=True |
| 1834 | ) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1835 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1836 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1837 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1838 | # The single chunk fails, pull the content sent out of the exception. |
| 1839 | http = HttpMockSequence( |
| 1840 | [ |
| 1841 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1842 | ({"status": "400"}, "echo_request_body"), |
| 1843 | ] |
| 1844 | ) |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1845 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1846 | try: |
| 1847 | body = request.execute(http=http) |
| 1848 | except HttpError as e: |
| 1849 | self.assertEqual(b"01234", e.content) |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1850 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1851 | def test_resumable_media_handle_uploads_of_unknown_size(self): |
| 1852 | http = HttpMockSequence( |
| 1853 | [ |
| 1854 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1855 | ({"status": "200"}, "echo_request_headers_as_json"), |
| 1856 | ] |
| 1857 | ) |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 1858 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1859 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1860 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1861 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1862 | # Create an upload that doesn't know the full size of the media. |
| 1863 | class IoBaseUnknownLength(MediaUpload): |
| 1864 | def chunksize(self): |
| 1865 | return 10 |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1866 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1867 | def mimetype(self): |
| 1868 | return "image/png" |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1869 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1870 | def size(self): |
| 1871 | return None |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1872 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1873 | def resumable(self): |
| 1874 | return True |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1875 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1876 | def getbytes(self, begin, length): |
| 1877 | return "0123456789" |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1878 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1879 | upload = IoBaseUnknownLength() |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1880 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1881 | request = zoo.animals().insert(media_body=upload, body=None) |
| 1882 | status, body = request.next_chunk(http=http) |
| 1883 | self.assertEqual(body, {"Content-Range": "bytes 0-9/*", "Content-Length": "10"}) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1884 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1885 | def test_resumable_media_no_streaming_on_unsupported_platforms(self): |
| 1886 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1887 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1888 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1889 | class IoBaseHasStream(MediaUpload): |
| 1890 | def chunksize(self): |
| 1891 | return 10 |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1892 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1893 | def mimetype(self): |
| 1894 | return "image/png" |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1895 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1896 | def size(self): |
| 1897 | return None |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1898 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1899 | def resumable(self): |
| 1900 | return True |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1901 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1902 | def getbytes(self, begin, length): |
| 1903 | return "0123456789" |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1904 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1905 | def has_stream(self): |
| 1906 | return True |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1907 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1908 | def stream(self): |
| 1909 | raise NotImplementedError() |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1910 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1911 | upload = IoBaseHasStream() |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1912 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1913 | orig_version = sys.version_info |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1914 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1915 | sys.version_info = (2, 6, 5, "final", 0) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1916 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1917 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1918 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1919 | # This should raise an exception because stream() will be called. |
| 1920 | http = HttpMockSequence( |
| 1921 | [ |
| 1922 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1923 | ({"status": "200"}, "echo_request_headers_as_json"), |
| 1924 | ] |
| 1925 | ) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1926 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1927 | self.assertRaises(NotImplementedError, request.next_chunk, http=http) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1928 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1929 | sys.version_info = orig_version |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1930 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1931 | def test_resumable_media_handle_uploads_of_unknown_size_eof(self): |
| 1932 | http = HttpMockSequence( |
| 1933 | [ |
| 1934 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1935 | ({"status": "200"}, "echo_request_headers_as_json"), |
| 1936 | ] |
| 1937 | ) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1938 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1939 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1940 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 1941 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1942 | fd = io.BytesIO(b"data goes here") |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1943 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1944 | # Create an upload that doesn't know the full size of the media. |
| 1945 | upload = MediaIoBaseUpload( |
| 1946 | fd=fd, mimetype="image/png", chunksize=15, resumable=True |
| 1947 | ) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1948 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1949 | request = zoo.animals().insert(media_body=upload, body=None) |
| 1950 | status, body = request.next_chunk(http=http) |
| 1951 | self.assertEqual( |
| 1952 | body, {"Content-Range": "bytes 0-13/14", "Content-Length": "14"} |
| 1953 | ) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1954 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1955 | def test_resumable_media_handle_resume_of_upload_of_unknown_size(self): |
| 1956 | http = HttpMockSequence( |
| 1957 | [ |
| 1958 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 1959 | ({"status": "400"}, ""), |
| 1960 | ] |
| 1961 | ) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 1962 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1963 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 1964 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1965 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1966 | # Create an upload that doesn't know the full size of the media. |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 1967 | fd = io.BytesIO(b"data goes here") |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1968 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1969 | upload = MediaIoBaseUpload( |
| 1970 | fd=fd, mimetype="image/png", chunksize=500, resumable=True |
| 1971 | ) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1972 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1973 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1974 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1975 | # Put it in an error state. |
| 1976 | self.assertRaises(HttpError, request.next_chunk, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1977 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1978 | http = HttpMockSequence( |
| 1979 | [({"status": "400", "range": "0-5"}, "echo_request_headers_as_json")] |
| 1980 | ) |
| 1981 | try: |
| 1982 | # Should resume the upload by first querying the status of the upload. |
| 1983 | request.next_chunk(http=http) |
| 1984 | except HttpError as e: |
| 1985 | expected = {"Content-Range": "bytes */14", "content-length": "0"} |
| 1986 | self.assertEqual( |
| 1987 | expected, |
| 1988 | json.loads(e.content.decode("utf-8")), |
| 1989 | "Should send an empty body when requesting the current upload status.", |
| 1990 | ) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 1991 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 1992 | def test_pickle(self): |
| 1993 | sorted_resource_keys = [ |
| 1994 | "_baseUrl", |
| 1995 | "_developerKey", |
| 1996 | "_dynamic_attrs", |
| 1997 | "_http", |
| 1998 | "_model", |
| 1999 | "_requestBuilder", |
| 2000 | "_resourceDesc", |
| 2001 | "_rootDesc", |
| 2002 | "_schema", |
| 2003 | "animals", |
| 2004 | "global_", |
| 2005 | "load", |
| 2006 | "loadNoTemplate", |
| 2007 | "my", |
| 2008 | "new_batch_http_request", |
| 2009 | "query", |
| 2010 | "scopedAnimals", |
| 2011 | ] |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 2012 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2013 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 2014 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2015 | self.assertEqual(sorted(zoo.__dict__.keys()), sorted_resource_keys) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 2016 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2017 | pickled_zoo = pickle.dumps(zoo) |
| 2018 | new_zoo = pickle.loads(pickled_zoo) |
| 2019 | self.assertEqual(sorted(new_zoo.__dict__.keys()), sorted_resource_keys) |
| 2020 | self.assertTrue(hasattr(new_zoo, "animals")) |
| 2021 | self.assertTrue(callable(new_zoo.animals)) |
| 2022 | self.assertTrue(hasattr(new_zoo, "global_")) |
| 2023 | self.assertTrue(callable(new_zoo.global_)) |
| 2024 | self.assertTrue(hasattr(new_zoo, "load")) |
| 2025 | self.assertTrue(callable(new_zoo.load)) |
| 2026 | self.assertTrue(hasattr(new_zoo, "loadNoTemplate")) |
| 2027 | self.assertTrue(callable(new_zoo.loadNoTemplate)) |
| 2028 | self.assertTrue(hasattr(new_zoo, "my")) |
| 2029 | self.assertTrue(callable(new_zoo.my)) |
| 2030 | self.assertTrue(hasattr(new_zoo, "query")) |
| 2031 | self.assertTrue(callable(new_zoo.query)) |
| 2032 | self.assertTrue(hasattr(new_zoo, "scopedAnimals")) |
| 2033 | self.assertTrue(callable(new_zoo.scopedAnimals)) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2034 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2035 | self.assertEqual(sorted(zoo._dynamic_attrs), sorted(new_zoo._dynamic_attrs)) |
| 2036 | self.assertEqual(zoo._baseUrl, new_zoo._baseUrl) |
| 2037 | self.assertEqual(zoo._developerKey, new_zoo._developerKey) |
| 2038 | self.assertEqual(zoo._requestBuilder, new_zoo._requestBuilder) |
| 2039 | self.assertEqual(zoo._resourceDesc, new_zoo._resourceDesc) |
| 2040 | self.assertEqual(zoo._rootDesc, new_zoo._rootDesc) |
| 2041 | # _http, _model and _schema won't be equal since we will get new |
| 2042 | # instances upon un-pickling |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2043 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2044 | def _dummy_zoo_request(self): |
Dmitry Frenkel | f3348f9 | 2020-07-15 13:05:58 -0700 | [diff] [blame] | 2045 | zoo_contents = read_datafile("zoo.json") |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2046 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2047 | zoo_uri = uritemplate.expand(DISCOVERY_URI, {"api": "zoo", "apiVersion": "v1"}) |
| 2048 | if "REMOTE_ADDR" in os.environ: |
| 2049 | zoo_uri = util._add_query_parameter( |
| 2050 | zoo_uri, "userIp", os.environ["REMOTE_ADDR"] |
| 2051 | ) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2052 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2053 | http = build_http() |
| 2054 | original_request = http.request |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2055 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2056 | def wrapped_request(uri, method="GET", *args, **kwargs): |
| 2057 | if uri == zoo_uri: |
| 2058 | return httplib2.Response({"status": "200"}), zoo_contents |
| 2059 | return original_request(uri, method=method, *args, **kwargs) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2060 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2061 | http.request = wrapped_request |
| 2062 | return http |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2063 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2064 | def _dummy_token(self): |
| 2065 | access_token = "foo" |
| 2066 | client_id = "some_client_id" |
| 2067 | client_secret = "cOuDdkfjxxnv+" |
| 2068 | refresh_token = "1/0/a.df219fjls0" |
| 2069 | token_expiry = datetime.datetime.utcnow() |
| 2070 | user_agent = "refresh_checker/1.0" |
| 2071 | return OAuth2Credentials( |
| 2072 | access_token, |
| 2073 | client_id, |
| 2074 | client_secret, |
| 2075 | refresh_token, |
| 2076 | token_expiry, |
| 2077 | GOOGLE_TOKEN_URI, |
| 2078 | user_agent, |
| 2079 | ) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2080 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2081 | def test_pickle_with_credentials(self): |
| 2082 | credentials = self._dummy_token() |
| 2083 | http = self._dummy_zoo_request() |
| 2084 | http = credentials.authorize(http) |
| 2085 | self.assertTrue(hasattr(http.request, "credentials")) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2086 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 2087 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2088 | pickled_zoo = pickle.dumps(zoo) |
| 2089 | new_zoo = pickle.loads(pickled_zoo) |
| 2090 | self.assertEqual(sorted(zoo.__dict__.keys()), sorted(new_zoo.__dict__.keys())) |
| 2091 | new_http = new_zoo._http |
| 2092 | self.assertFalse(hasattr(new_http.request, "credentials")) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 2093 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2094 | def test_resumable_media_upload_no_content(self): |
| 2095 | self.http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 2096 | zoo = build("zoo", "v1", http=self.http, static_discovery=False) |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 2097 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2098 | media_upload = MediaFileUpload(datafile("empty"), resumable=True) |
| 2099 | request = zoo.animals().insert(media_body=media_upload, body=None) |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 2100 | |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 2101 | self.assertEqual(media_upload, request.resumable) |
| 2102 | self.assertEqual(request.body, None) |
| 2103 | self.assertEqual(request.resumable_uri, None) |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 2104 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2105 | http = HttpMockSequence( |
| 2106 | [ |
| 2107 | ({"status": "200", "location": "http://upload.example.com"}, ""), |
| 2108 | ( |
| 2109 | { |
| 2110 | "status": "308", |
| 2111 | "location": "http://upload.example.com/2", |
| 2112 | "range": "0-0", |
| 2113 | }, |
| 2114 | "", |
| 2115 | ), |
| 2116 | ] |
| 2117 | ) |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 2118 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2119 | status, body = request.next_chunk(http=http) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 2120 | self.assertEqual(None, body) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2121 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
Bu Sun Kim | 1cf3cbc | 2020-03-12 12:38:23 -0700 | [diff] [blame] | 2122 | self.assertEqual(0, status.progress()) |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 2123 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 2124 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 2125 | class Next(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2126 | def test_next_successful_none_on_no_next_page_token(self): |
| 2127 | self.http = HttpMock(datafile("tasks.json"), {"status": "200"}) |
| 2128 | tasks = build("tasks", "v1", http=self.http) |
| 2129 | request = tasks.tasklists().list() |
| 2130 | self.assertEqual(None, tasks.tasklists().list_next(request, {})) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 2131 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2132 | def test_next_successful_none_on_empty_page_token(self): |
| 2133 | self.http = HttpMock(datafile("tasks.json"), {"status": "200"}) |
| 2134 | tasks = build("tasks", "v1", http=self.http) |
| 2135 | request = tasks.tasklists().list() |
| 2136 | next_request = tasks.tasklists().list_next(request, {"nextPageToken": ""}) |
| 2137 | self.assertEqual(None, next_request) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 2138 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2139 | def test_next_successful_with_next_page_token(self): |
| 2140 | self.http = HttpMock(datafile("tasks.json"), {"status": "200"}) |
| 2141 | tasks = build("tasks", "v1", http=self.http) |
| 2142 | request = tasks.tasklists().list() |
| 2143 | next_request = tasks.tasklists().list_next(request, {"nextPageToken": "123abc"}) |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 2144 | parsed = urllib.parse.urlparse(next_request.uri) |
| 2145 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2146 | self.assertEqual(q["pageToken"][0], "123abc") |
Son Dinh | 2a9a213 | 2015-07-23 16:30:56 +0000 | [diff] [blame] | 2147 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2148 | def test_next_successful_with_next_page_token_alternate_name(self): |
| 2149 | self.http = HttpMock(datafile("bigquery.json"), {"status": "200"}) |
| 2150 | bigquery = build("bigquery", "v2", http=self.http) |
| 2151 | request = bigquery.tabledata().list(datasetId="", projectId="", tableId="") |
| 2152 | next_request = bigquery.tabledata().list_next(request, {"pageToken": "123abc"}) |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 2153 | parsed = urllib.parse.urlparse(next_request.uri) |
| 2154 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2155 | self.assertEqual(q["pageToken"][0], "123abc") |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 2156 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2157 | def test_next_successful_with_next_page_token_in_body(self): |
| 2158 | self.http = HttpMock(datafile("logging.json"), {"status": "200"}) |
| 2159 | logging = build("logging", "v2", http=self.http) |
| 2160 | request = logging.entries().list(body={}) |
| 2161 | next_request = logging.entries().list_next(request, {"nextPageToken": "123abc"}) |
| 2162 | body = JsonModel().deserialize(next_request.body) |
| 2163 | self.assertEqual(body["pageToken"], "123abc") |
David Schweikert | 8019f2f | 2021-06-08 16:54:44 +0200 | [diff] [blame] | 2164 | # The body is changed, make sure that body_length is changed too (see |
| 2165 | # github #1403) |
| 2166 | self.assertEqual(next_request.body_size, len(next_request.body)) |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 2167 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2168 | def test_next_with_method_with_no_properties(self): |
| 2169 | self.http = HttpMock(datafile("latitude.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 2170 | service = build("latitude", "v1", http=self.http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2171 | service.currentLocation().get() |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 2172 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2173 | def test_next_nonexistent_with_no_next_page_token(self): |
| 2174 | self.http = HttpMock(datafile("drive.json"), {"status": "200"}) |
| 2175 | drive = build("drive", "v3", http=self.http) |
| 2176 | drive.changes().watch(body={}) |
| 2177 | self.assertFalse(callable(getattr(drive.changes(), "watch_next", None))) |
Thomas Coffee | 20af04d | 2017-02-10 15:24:44 -0800 | [diff] [blame] | 2178 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2179 | def test_next_successful_with_next_page_token_required(self): |
| 2180 | self.http = HttpMock(datafile("drive.json"), {"status": "200"}) |
| 2181 | drive = build("drive", "v3", http=self.http) |
| 2182 | request = drive.changes().list(pageToken="startPageToken") |
| 2183 | next_request = drive.changes().list_next(request, {"nextPageToken": "123abc"}) |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 2184 | parsed = urllib.parse.urlparse(next_request.uri) |
| 2185 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2186 | self.assertEqual(q["pageToken"][0], "123abc") |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 2187 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 2188 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 2189 | class MediaGet(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2190 | def test_get_media(self): |
| 2191 | http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 2192 | zoo = build("zoo", "v1", http=http, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2193 | request = zoo.animals().get_media(name="Lion") |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 2194 | |
Anthonios Partheniou | 9f7b410 | 2021-07-23 12:18:25 -0400 | [diff] [blame] | 2195 | parsed = urllib.parse.urlparse(request.uri) |
| 2196 | q = urllib.parse.parse_qs(parsed.query) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2197 | self.assertEqual(q["alt"], ["media"]) |
| 2198 | self.assertEqual(request.headers["accept"], "*/*") |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 2199 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2200 | http = HttpMockSequence([({"status": "200"}, "standing in for media")]) |
| 2201 | response = request.execute(http=http) |
| 2202 | self.assertEqual(b"standing in for media", response) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 2203 | |
| 2204 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 2205 | if __name__ == "__main__": |
| 2206 | unittest.main() |