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