Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
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 | # |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 4 | # Copyright 2010 Google Inc. |
| 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 | """ |
| 23 | |
| 24 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 25 | |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 26 | import copy |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 27 | import datetime |
Joe Gregorio | 32f048f | 2012-08-27 16:31:27 -0400 | [diff] [blame] | 28 | import gflags |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 29 | import httplib2 |
| 30 | import os |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 31 | import pickle |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 32 | import sys |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 33 | import unittest |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 34 | import urlparse |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 35 | import StringIO |
| 36 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 37 | |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 38 | try: |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 39 | from urlparse import parse_qs |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 40 | except ImportError: |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 41 | from cgi import parse_qs |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 42 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 43 | |
| 44 | from apiclient.discovery import _add_query_parameter |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 45 | from apiclient.discovery import _fix_up_media_upload |
| 46 | from apiclient.discovery import _fix_up_method_description |
| 47 | from apiclient.discovery import _fix_up_parameters |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 48 | from apiclient.discovery import build |
| 49 | from apiclient.discovery import build_from_document |
| 50 | from apiclient.discovery import DISCOVERY_URI |
| 51 | from apiclient.discovery import key2param |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 52 | from apiclient.discovery import MEDIA_BODY_PARAMETER_DEFAULT_VALUE |
| 53 | from apiclient.discovery import STACK_QUERY_PARAMETERS |
| 54 | from apiclient.discovery import STACK_QUERY_PARAMETER_DEFAULT_VALUE |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 55 | from apiclient.errors import HttpError |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 56 | from apiclient.errors import InvalidJsonError |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 57 | from apiclient.errors import MediaUploadSizeError |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 58 | from apiclient.errors import ResumableUploadError |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 59 | from apiclient.errors import UnacceptableMimeTypeError |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 60 | from apiclient.http import HttpMock |
| 61 | from apiclient.http import HttpMockSequence |
| 62 | from apiclient.http import MediaFileUpload |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 63 | from apiclient.http import MediaIoBaseUpload |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 64 | from apiclient.http import MediaUpload |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 65 | from apiclient.http import MediaUploadProgress |
| 66 | from apiclient.http import tunnel_patch |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 67 | from oauth2client import GOOGLE_TOKEN_URI |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 68 | from oauth2client.anyjson import simplejson |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 69 | from oauth2client.client import OAuth2Credentials |
| 70 | import uritemplate |
| 71 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 72 | |
| 73 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 74 | |
Joe Gregorio | 32f048f | 2012-08-27 16:31:27 -0400 | [diff] [blame] | 75 | FLAGS = gflags.FLAGS |
| 76 | FLAGS.positional_parameters_enforcement = 'EXCEPTION' |
| 77 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 78 | |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 79 | def assertUrisEqual(testcase, expected, actual): |
| 80 | """Test that URIs are the same, up to reordering of query parameters.""" |
| 81 | expected = urlparse.urlparse(expected) |
| 82 | actual = urlparse.urlparse(actual) |
| 83 | testcase.assertEqual(expected.scheme, actual.scheme) |
| 84 | testcase.assertEqual(expected.netloc, actual.netloc) |
| 85 | testcase.assertEqual(expected.path, actual.path) |
| 86 | testcase.assertEqual(expected.params, actual.params) |
| 87 | testcase.assertEqual(expected.fragment, actual.fragment) |
| 88 | expected_query = parse_qs(expected.query) |
| 89 | actual_query = parse_qs(actual.query) |
| 90 | for name in expected_query.keys(): |
| 91 | testcase.assertEqual(expected_query[name], actual_query[name]) |
| 92 | for name in actual_query.keys(): |
| 93 | testcase.assertEqual(expected_query[name], actual_query[name]) |
| 94 | |
| 95 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 96 | def datafile(filename): |
| 97 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 98 | |
| 99 | |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 100 | class SetupHttplib2(unittest.TestCase): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 101 | |
Joe Gregorio | 504a17f | 2012-12-07 14:14:26 -0500 | [diff] [blame] | 102 | def test_retries(self): |
| 103 | # Merely loading apiclient.discovery should set the RETRIES to 1. |
| 104 | self.assertEqual(1, httplib2.RETRIES) |
| 105 | |
| 106 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 107 | class Utilities(unittest.TestCase): |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 108 | |
| 109 | def setUp(self): |
| 110 | with open(datafile('zoo.json'), 'r') as fh: |
| 111 | self.zoo_root_desc = simplejson.loads(fh.read()) |
| 112 | self.zoo_get_method_desc = self.zoo_root_desc['methods']['query'] |
| 113 | zoo_animals_resource = self.zoo_root_desc['resources']['animals'] |
| 114 | self.zoo_insert_method_desc = zoo_animals_resource['methods']['insert'] |
| 115 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 116 | def test_key2param(self): |
| 117 | self.assertEqual('max_results', key2param('max-results')) |
| 118 | self.assertEqual('x007_bond', key2param('007-bond')) |
| 119 | |
Daniel Hermes | c211324 | 2013-02-27 10:16:13 -0800 | [diff] [blame^] | 120 | def _base_fix_up_parameters_test(self, method_desc, http_method, root_desc): |
| 121 | self.assertEqual(method_desc['httpMethod'], http_method) |
| 122 | |
| 123 | method_desc_copy = copy.deepcopy(method_desc) |
| 124 | self.assertEqual(method_desc, method_desc_copy) |
| 125 | |
| 126 | parameters = _fix_up_parameters(method_desc_copy, root_desc, http_method) |
| 127 | |
| 128 | self.assertNotEqual(method_desc, method_desc_copy) |
| 129 | |
| 130 | for param_name in STACK_QUERY_PARAMETERS: |
| 131 | self.assertEqual(STACK_QUERY_PARAMETER_DEFAULT_VALUE, |
| 132 | parameters[param_name]) |
| 133 | |
| 134 | for param_name, value in root_desc.get('parameters', {}).iteritems(): |
| 135 | self.assertEqual(value, parameters[param_name]) |
| 136 | |
| 137 | return parameters |
| 138 | |
| 139 | def test_fix_up_parameters_get(self): |
| 140 | parameters = self._base_fix_up_parameters_test(self.zoo_get_method_desc, |
| 141 | 'GET', self.zoo_root_desc) |
| 142 | # Since http_method is 'GET' |
| 143 | self.assertFalse(parameters.has_key('body')) |
| 144 | |
| 145 | def test_fix_up_parameters_insert(self): |
| 146 | parameters = self._base_fix_up_parameters_test(self.zoo_insert_method_desc, |
| 147 | 'POST', self.zoo_root_desc) |
| 148 | body = { |
| 149 | 'description': 'The request body.', |
| 150 | 'type': 'object', |
| 151 | 'required': True, |
| 152 | '$ref': 'Animal', |
| 153 | } |
| 154 | self.assertEqual(parameters['body'], body) |
| 155 | |
| 156 | def test_fix_up_parameters_check_body(self): |
| 157 | dummy_root_desc = {} |
| 158 | no_payload_http_method = 'DELETE' |
| 159 | with_payload_http_method = 'PUT' |
| 160 | |
| 161 | invalid_method_desc = {'response': 'Who cares'} |
| 162 | valid_method_desc = {'request': {'key1': 'value1', 'key2': 'value2'}} |
| 163 | |
| 164 | parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc, |
| 165 | no_payload_http_method) |
| 166 | self.assertFalse(parameters.has_key('body')) |
| 167 | |
| 168 | parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc, |
| 169 | no_payload_http_method) |
| 170 | self.assertFalse(parameters.has_key('body')) |
| 171 | |
| 172 | parameters = _fix_up_parameters(invalid_method_desc, dummy_root_desc, |
| 173 | with_payload_http_method) |
| 174 | self.assertFalse(parameters.has_key('body')) |
| 175 | |
| 176 | parameters = _fix_up_parameters(valid_method_desc, dummy_root_desc, |
| 177 | with_payload_http_method) |
| 178 | body = { |
| 179 | 'description': 'The request body.', |
| 180 | 'type': 'object', |
| 181 | 'required': True, |
| 182 | 'key1': 'value1', |
| 183 | 'key2': 'value2', |
| 184 | } |
| 185 | self.assertEqual(parameters['body'], body) |
| 186 | |
| 187 | def _base_fix_up_method_description_test( |
| 188 | self, method_desc, initial_parameters, final_parameters, |
| 189 | final_accept, final_max_size, final_media_path_url): |
| 190 | fake_root_desc = {'rootUrl': 'http://root/', |
| 191 | 'servicePath': 'fake/'} |
| 192 | fake_path_url = 'fake-path/' |
| 193 | |
| 194 | accept, max_size, media_path_url = _fix_up_media_upload( |
| 195 | method_desc, fake_root_desc, fake_path_url, initial_parameters) |
| 196 | self.assertEqual(accept, final_accept) |
| 197 | self.assertEqual(max_size, final_max_size) |
| 198 | self.assertEqual(media_path_url, final_media_path_url) |
| 199 | self.assertEqual(initial_parameters, final_parameters) |
| 200 | |
| 201 | def test_fix_up_media_upload_no_initial_invalid(self): |
| 202 | invalid_method_desc = {'response': 'Who cares'} |
| 203 | self._base_fix_up_method_description_test(invalid_method_desc, {}, {}, |
| 204 | [], 0, None) |
| 205 | |
| 206 | def test_fix_up_media_upload_no_initial_valid_minimal(self): |
| 207 | valid_method_desc = {'mediaUpload': {'accept': []}} |
| 208 | final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE} |
| 209 | self._base_fix_up_method_description_test( |
| 210 | valid_method_desc, {}, final_parameters, [], 0, |
| 211 | 'http://root/upload/fake/fake-path/') |
| 212 | |
| 213 | def test_fix_up_media_upload_no_initial_valid_full(self): |
| 214 | valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}} |
| 215 | final_parameters = {'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE} |
| 216 | ten_gb = 10 * 2**30 |
| 217 | self._base_fix_up_method_description_test( |
| 218 | valid_method_desc, {}, final_parameters, ['*/*'], |
| 219 | ten_gb, 'http://root/upload/fake/fake-path/') |
| 220 | |
| 221 | def test_fix_up_media_upload_with_initial_invalid(self): |
| 222 | invalid_method_desc = {'response': 'Who cares'} |
| 223 | initial_parameters = {'body': {}} |
| 224 | self._base_fix_up_method_description_test( |
| 225 | invalid_method_desc, initial_parameters, |
| 226 | initial_parameters, [], 0, None) |
| 227 | |
| 228 | def test_fix_up_media_upload_with_initial_valid_minimal(self): |
| 229 | valid_method_desc = {'mediaUpload': {'accept': []}} |
| 230 | initial_parameters = {'body': {}} |
| 231 | final_parameters = {'body': {'required': False}, |
| 232 | 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE} |
| 233 | self._base_fix_up_method_description_test( |
| 234 | valid_method_desc, initial_parameters, final_parameters, [], 0, |
| 235 | 'http://root/upload/fake/fake-path/') |
| 236 | |
| 237 | def test_fix_up_media_upload_with_initial_valid_full(self): |
| 238 | valid_method_desc = {'mediaUpload': {'accept': ['*/*'], 'maxSize': '10GB'}} |
| 239 | initial_parameters = {'body': {}} |
| 240 | final_parameters = {'body': {'required': False}, |
| 241 | 'media_body': MEDIA_BODY_PARAMETER_DEFAULT_VALUE} |
| 242 | ten_gb = 10 * 2**30 |
| 243 | self._base_fix_up_method_description_test( |
| 244 | valid_method_desc, initial_parameters, final_parameters, ['*/*'], |
| 245 | ten_gb, 'http://root/upload/fake/fake-path/') |
| 246 | |
| 247 | def test_fix_up_method_description_get(self): |
| 248 | result = _fix_up_method_description(self.zoo_get_method_desc, |
| 249 | self.zoo_root_desc) |
| 250 | path_url = 'query' |
| 251 | http_method = 'GET' |
| 252 | method_id = 'bigquery.query' |
| 253 | accept = [] |
| 254 | max_size = 0L |
| 255 | media_path_url = None |
| 256 | self.assertEqual(result, (path_url, http_method, method_id, accept, |
| 257 | max_size, media_path_url)) |
| 258 | |
| 259 | def test_fix_up_method_description_insert(self): |
| 260 | result = _fix_up_method_description(self.zoo_insert_method_desc, |
| 261 | self.zoo_root_desc) |
| 262 | path_url = 'animals' |
| 263 | http_method = 'POST' |
| 264 | method_id = 'zoo.animals.insert' |
| 265 | accept = ['image/png'] |
| 266 | max_size = 1024L |
| 267 | media_path_url = 'https://www.googleapis.com/upload/zoo/v1/animals' |
| 268 | self.assertEqual(result, (path_url, http_method, method_id, accept, |
| 269 | max_size, media_path_url)) |
| 270 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 271 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 272 | class DiscoveryErrors(unittest.TestCase): |
| 273 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 274 | def test_tests_should_be_run_with_strict_positional_enforcement(self): |
| 275 | try: |
| 276 | plus = build('plus', 'v1', None) |
| 277 | self.fail("should have raised a TypeError exception over missing http=.") |
| 278 | except TypeError: |
| 279 | pass |
| 280 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 281 | def test_failed_to_parse_discovery_json(self): |
| 282 | self.http = HttpMock(datafile('malformed.json'), {'status': '200'}) |
| 283 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 284 | plus = build('plus', 'v1', http=self.http) |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 285 | self.fail("should have raised an exception over malformed JSON.") |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 286 | except InvalidJsonError: |
| 287 | pass |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 288 | |
| 289 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 290 | class DiscoveryFromDocument(unittest.TestCase): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 291 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 292 | def test_can_build_from_local_document(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 293 | discovery = file(datafile('plus.json')).read() |
| 294 | plus = build_from_document(discovery, base="https://www.googleapis.com/") |
| 295 | self.assertTrue(plus is not None) |
Joe Gregorio | 4772f3d | 2012-12-10 10:22:37 -0500 | [diff] [blame] | 296 | self.assertTrue(hasattr(plus, 'activities')) |
| 297 | |
| 298 | def test_can_build_from_local_deserialized_document(self): |
| 299 | discovery = file(datafile('plus.json')).read() |
| 300 | discovery = simplejson.loads(discovery) |
| 301 | plus = build_from_document(discovery, base="https://www.googleapis.com/") |
| 302 | self.assertTrue(plus is not None) |
| 303 | self.assertTrue(hasattr(plus, 'activities')) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 304 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 305 | def test_building_with_base_remembers_base(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 306 | discovery = file(datafile('plus.json')).read() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 307 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 308 | base = "https://www.example.com/" |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 309 | plus = build_from_document(discovery, base=base) |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 310 | self.assertEquals("https://www.googleapis.com/plus/v1/", plus._baseUrl) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 311 | |
| 312 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 313 | class DiscoveryFromHttp(unittest.TestCase): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 314 | def setUp(self): |
Joe Beda | fb463cb | 2011-09-19 17:39:49 -0700 | [diff] [blame] | 315 | self.old_environ = os.environ.copy() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 316 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 317 | def tearDown(self): |
| 318 | os.environ = self.old_environ |
| 319 | |
| 320 | def test_userip_is_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 321 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 322 | # out of the raised exception. |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 323 | os.environ['REMOTE_ADDR'] = '10.0.0.1' |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 324 | try: |
| 325 | http = HttpMockSequence([ |
| 326 | ({'status': '400'}, file(datafile('zoo.json'), 'r').read()), |
| 327 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 328 | zoo = build('zoo', 'v1', http=http, developerKey='foo', |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 329 | discoveryServiceUrl='http://example.com') |
| 330 | self.fail('Should have raised an exception.') |
| 331 | except HttpError, e: |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 332 | self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1') |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 333 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 334 | def test_userip_missing_is_not_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 335 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 336 | # out of the raised exception. |
| 337 | try: |
| 338 | http = HttpMockSequence([ |
| 339 | ({'status': '400'}, file(datafile('zoo.json'), 'r').read()), |
| 340 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 341 | zoo = build('zoo', 'v1', http=http, developerKey=None, |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 342 | discoveryServiceUrl='http://example.com') |
| 343 | self.fail('Should have raised an exception.') |
| 344 | except HttpError, e: |
| 345 | self.assertEqual(e.uri, 'http://example.com') |
| 346 | |
| 347 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 348 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 349 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 350 | def test_method_error_checking(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 351 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 352 | plus = build('plus', 'v1', http=self.http) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 353 | |
| 354 | # Missing required parameters |
| 355 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 356 | plus.activities().list() |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 357 | self.fail() |
| 358 | except TypeError, e: |
| 359 | self.assertTrue('Missing' in str(e)) |
| 360 | |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 361 | # Missing required parameters even if supplied as None. |
| 362 | try: |
| 363 | plus.activities().list(collection=None, userId=None) |
| 364 | self.fail() |
| 365 | except TypeError, e: |
| 366 | self.assertTrue('Missing' in str(e)) |
| 367 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 368 | # Parameter doesn't match regex |
| 369 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 370 | plus.activities().list(collection='not_a_collection_name', userId='me') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 371 | self.fail() |
| 372 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 373 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 374 | |
| 375 | # Unexpected parameter |
| 376 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 377 | plus.activities().list(flubber=12) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 378 | self.fail() |
| 379 | except TypeError, e: |
| 380 | self.assertTrue('unexpected' in str(e)) |
| 381 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 382 | def _check_query_types(self, request): |
| 383 | parsed = urlparse.urlparse(request.uri) |
| 384 | q = parse_qs(parsed[4]) |
| 385 | self.assertEqual(q['q'], ['foo']) |
| 386 | self.assertEqual(q['i'], ['1']) |
| 387 | self.assertEqual(q['n'], ['1.0']) |
| 388 | self.assertEqual(q['b'], ['false']) |
| 389 | self.assertEqual(q['a'], ['[1, 2, 3]']) |
| 390 | self.assertEqual(q['o'], ['{\'a\': 1}']) |
| 391 | self.assertEqual(q['e'], ['bar']) |
| 392 | |
| 393 | def test_type_coercion(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 394 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 395 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 396 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 397 | request = zoo.query( |
| 398 | 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] | 399 | self._check_query_types(request) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 400 | request = zoo.query( |
| 401 | 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] | 402 | self._check_query_types(request) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 403 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 404 | request = zoo.query( |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 405 | 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] | 406 | |
| 407 | request = zoo.query( |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 408 | q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar', |
| 409 | er=['one', 'three'], rr=['foo', 'bar']) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 410 | self._check_query_types(request) |
| 411 | |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 412 | # Five is right out. |
Joe Gregorio | 20c26e5 | 2012-03-02 15:58:31 -0500 | [diff] [blame] | 413 | self.assertRaises(TypeError, zoo.query, er=['one', 'five']) |
Craig Citro | 1e74282 | 2012-03-01 12:59:22 -0800 | [diff] [blame] | 414 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 415 | def test_optional_stack_query_parameters(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 416 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 417 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 418 | request = zoo.query(trace='html', fields='description') |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 419 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 420 | parsed = urlparse.urlparse(request.uri) |
| 421 | q = parse_qs(parsed[4]) |
| 422 | self.assertEqual(q['trace'], ['html']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 423 | self.assertEqual(q['fields'], ['description']) |
| 424 | |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 425 | def test_string_params_value_of_none_get_dropped(self): |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 426 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 427 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 2467afa | 2012-06-20 12:21:25 -0400 | [diff] [blame] | 428 | request = zoo.query(trace=None, fields='description') |
| 429 | |
| 430 | parsed = urlparse.urlparse(request.uri) |
| 431 | q = parse_qs(parsed[4]) |
| 432 | self.assertFalse('trace' in q) |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 433 | |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 434 | def test_model_added_query_parameters(self): |
| 435 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 436 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 437 | request = zoo.animals().get(name='Lion') |
| 438 | |
| 439 | parsed = urlparse.urlparse(request.uri) |
| 440 | q = parse_qs(parsed[4]) |
| 441 | self.assertEqual(q['alt'], ['json']) |
| 442 | self.assertEqual(request.headers['accept'], 'application/json') |
| 443 | |
| 444 | def test_fallback_to_raw_model(self): |
| 445 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 446 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | e08a166 | 2011-12-07 09:48:22 -0500 | [diff] [blame] | 447 | request = zoo.animals().getmedia(name='Lion') |
| 448 | |
| 449 | parsed = urlparse.urlparse(request.uri) |
| 450 | q = parse_qs(parsed[4]) |
| 451 | self.assertTrue('alt' not in q) |
| 452 | self.assertEqual(request.headers['accept'], '*/*') |
| 453 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 454 | def test_patch(self): |
| 455 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 456 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 457 | request = zoo.animals().patch(name='lion', body='{"description": "foo"}') |
| 458 | |
| 459 | self.assertEqual(request.method, 'PATCH') |
| 460 | |
| 461 | def test_tunnel_patch(self): |
| 462 | http = HttpMockSequence([ |
| 463 | ({'status': '200'}, file(datafile('zoo.json'), 'r').read()), |
| 464 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 465 | ]) |
| 466 | http = tunnel_patch(http) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 467 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 468 | resp = zoo.animals().patch( |
| 469 | name='lion', body='{"description": "foo"}').execute() |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 470 | |
| 471 | self.assertTrue('x-http-method-override' in resp) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 472 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 473 | def test_plus_resources(self): |
| 474 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 475 | plus = build('plus', 'v1', http=self.http) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 476 | self.assertTrue(getattr(plus, 'activities')) |
| 477 | self.assertTrue(getattr(plus, 'people')) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 478 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 479 | def test_full_featured(self): |
| 480 | # Zoo should exercise all discovery facets |
| 481 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 482 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 483 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 484 | self.assertTrue(getattr(zoo, 'animals')) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 485 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 486 | request = zoo.animals().list(name='bat', projection="full") |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 487 | parsed = urlparse.urlparse(request.uri) |
| 488 | q = parse_qs(parsed[4]) |
| 489 | self.assertEqual(q['name'], ['bat']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 490 | self.assertEqual(q['projection'], ['full']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 491 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 492 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 493 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 494 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 495 | self.assertTrue(getattr(zoo, 'animals')) |
| 496 | request = zoo.my().favorites().list(max_results="5") |
| 497 | parsed = urlparse.urlparse(request.uri) |
| 498 | q = parse_qs(parsed[4]) |
| 499 | self.assertEqual(q['max-results'], ['5']) |
| 500 | |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 501 | def test_methods_with_reserved_names(self): |
| 502 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 503 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 504 | self.assertTrue(getattr(zoo, 'animals')) |
| 505 | request = zoo.global_().print_().assert_(max_results="5") |
| 506 | parsed = urlparse.urlparse(request.uri) |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 507 | self.assertEqual(parsed[2], '/zoo/v1/global/print/assert') |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 508 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 509 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 510 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 511 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 512 | self.assertTrue(getattr(zoo, 'query')) |
| 513 | request = zoo.query(q="foo") |
| 514 | parsed = urlparse.urlparse(request.uri) |
| 515 | q = parse_qs(parsed[4]) |
| 516 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 517 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 518 | def test_simple_media_uploads(self): |
| 519 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 520 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 521 | doc = getattr(zoo.animals().insert, '__doc__') |
| 522 | self.assertTrue('media_body' in doc) |
| 523 | |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 524 | def test_simple_media_upload_no_max_size_provided(self): |
| 525 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 526 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 527 | request = zoo.animals().crossbreed(media_body=datafile('small.png')) |
| 528 | self.assertEquals('image/png', request.headers['content-type']) |
| 529 | self.assertEquals('PNG', request.body[1:4]) |
| 530 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 531 | def test_simple_media_raise_correct_exceptions(self): |
| 532 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 533 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 534 | |
| 535 | try: |
| 536 | zoo.animals().insert(media_body=datafile('smiley.png')) |
| 537 | self.fail("should throw exception if media is too large.") |
| 538 | except MediaUploadSizeError: |
| 539 | pass |
| 540 | |
| 541 | try: |
| 542 | zoo.animals().insert(media_body=datafile('small.jpg')) |
| 543 | self.fail("should throw exception if mimetype is unacceptable.") |
| 544 | except UnacceptableMimeTypeError: |
| 545 | pass |
| 546 | |
| 547 | def test_simple_media_good_upload(self): |
| 548 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 549 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 550 | |
| 551 | request = zoo.animals().insert(media_body=datafile('small.png')) |
| 552 | self.assertEquals('image/png', request.headers['content-type']) |
| 553 | self.assertEquals('PNG', request.body[1:4]) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 554 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 555 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=media&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 556 | request.uri) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 557 | |
| 558 | def test_multipart_media_raise_correct_exceptions(self): |
| 559 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 560 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 561 | |
| 562 | try: |
| 563 | zoo.animals().insert(media_body=datafile('smiley.png'), body={}) |
| 564 | self.fail("should throw exception if media is too large.") |
| 565 | except MediaUploadSizeError: |
| 566 | pass |
| 567 | |
| 568 | try: |
| 569 | zoo.animals().insert(media_body=datafile('small.jpg'), body={}) |
| 570 | self.fail("should throw exception if mimetype is unacceptable.") |
| 571 | except UnacceptableMimeTypeError: |
| 572 | pass |
| 573 | |
| 574 | def test_multipart_media_good_upload(self): |
| 575 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 576 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 577 | |
| 578 | request = zoo.animals().insert(media_body=datafile('small.png'), body={}) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 579 | self.assertTrue(request.headers['content-type'].startswith( |
| 580 | 'multipart/related')) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 581 | self.assertEquals('--==', request.body[0:4]) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 582 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 583 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=multipart&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 584 | request.uri) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 585 | |
| 586 | def test_media_capable_method_without_media(self): |
| 587 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 588 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 589 | |
| 590 | request = zoo.animals().insert(body={}) |
| 591 | self.assertTrue(request.headers['content-type'], 'application/json') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 592 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 593 | def test_resumable_multipart_media_good_upload(self): |
| 594 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 595 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 596 | |
| 597 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 598 | request = zoo.animals().insert(media_body=media_upload, body={}) |
| 599 | self.assertTrue(request.headers['content-type'].startswith( |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 600 | 'application/json')) |
| 601 | self.assertEquals('{"data": {}}', request.body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 602 | self.assertEquals(media_upload, request.resumable) |
| 603 | |
| 604 | self.assertEquals('image/png', request.resumable.mimetype()) |
| 605 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 606 | self.assertNotEquals(request.body, None) |
| 607 | self.assertEquals(request.resumable_uri, None) |
| 608 | |
| 609 | http = HttpMockSequence([ |
| 610 | ({'status': '200', |
| 611 | 'location': 'http://upload.example.com'}, ''), |
| 612 | ({'status': '308', |
| 613 | 'location': 'http://upload.example.com/2', |
| 614 | 'range': '0-12'}, ''), |
| 615 | ({'status': '308', |
| 616 | 'location': 'http://upload.example.com/3', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 617 | 'range': '0-%d' % (media_upload.size() - 2)}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 618 | ({'status': '200'}, '{"foo": "bar"}'), |
| 619 | ]) |
| 620 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 621 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 622 | self.assertEquals(None, body) |
| 623 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
| 624 | self.assertEquals(13, status.resumable_progress) |
| 625 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 626 | # Two requests should have been made and the resumable_uri should have been |
| 627 | # updated for each one. |
| 628 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/2') |
| 629 | |
| 630 | self.assertEquals(media_upload, request.resumable) |
| 631 | self.assertEquals(13, request.resumable_progress) |
| 632 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 633 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 634 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/3') |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 635 | self.assertEquals(media_upload.size()-1, request.resumable_progress) |
| 636 | self.assertEquals('{"data": {}}', request.body) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 637 | |
| 638 | # Final call to next_chunk should complete the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 639 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 640 | self.assertEquals(body, {"foo": "bar"}) |
| 641 | self.assertEquals(status, None) |
| 642 | |
| 643 | |
| 644 | def test_resumable_media_good_upload(self): |
| 645 | """Not a multipart upload.""" |
| 646 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 647 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 648 | |
| 649 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 650 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 651 | self.assertEquals(media_upload, request.resumable) |
| 652 | |
| 653 | self.assertEquals('image/png', request.resumable.mimetype()) |
| 654 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 655 | self.assertEquals(request.body, None) |
| 656 | self.assertEquals(request.resumable_uri, None) |
| 657 | |
| 658 | http = HttpMockSequence([ |
| 659 | ({'status': '200', |
| 660 | 'location': 'http://upload.example.com'}, ''), |
| 661 | ({'status': '308', |
| 662 | 'location': 'http://upload.example.com/2', |
| 663 | 'range': '0-12'}, ''), |
| 664 | ({'status': '308', |
| 665 | 'location': 'http://upload.example.com/3', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 666 | 'range': '0-%d' % (media_upload.size() - 2)}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 667 | ({'status': '200'}, '{"foo": "bar"}'), |
| 668 | ]) |
| 669 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 670 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 671 | self.assertEquals(None, body) |
| 672 | self.assertTrue(isinstance(status, MediaUploadProgress)) |
| 673 | self.assertEquals(13, status.resumable_progress) |
| 674 | |
| 675 | # Two requests should have been made and the resumable_uri should have been |
| 676 | # updated for each one. |
| 677 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/2') |
| 678 | |
| 679 | self.assertEquals(media_upload, request.resumable) |
| 680 | self.assertEquals(13, request.resumable_progress) |
| 681 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 682 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 683 | self.assertEquals(request.resumable_uri, 'http://upload.example.com/3') |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 684 | self.assertEquals(media_upload.size()-1, request.resumable_progress) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 685 | self.assertEquals(request.body, None) |
| 686 | |
| 687 | # Final call to next_chunk should complete the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 688 | status, body = request.next_chunk(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 689 | self.assertEquals(body, {"foo": "bar"}) |
| 690 | self.assertEquals(status, None) |
| 691 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 692 | def test_resumable_media_good_upload_from_execute(self): |
| 693 | """Not a multipart upload.""" |
| 694 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 695 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 696 | |
| 697 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 698 | request = zoo.animals().insert(media_body=media_upload, body=None) |
Joe Gregorio | f1ba7f1 | 2012-11-16 15:10:47 -0500 | [diff] [blame] | 699 | assertUrisEqual(self, |
Joe Gregorio | a283815 | 2012-07-16 11:52:17 -0400 | [diff] [blame] | 700 | 'https://www.googleapis.com/upload/zoo/v1/animals?uploadType=resumable&alt=json', |
Joe Gregorio | de86044 | 2012-03-02 15:55:52 -0500 | [diff] [blame] | 701 | request.uri) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 702 | |
| 703 | http = HttpMockSequence([ |
| 704 | ({'status': '200', |
| 705 | 'location': 'http://upload.example.com'}, ''), |
| 706 | ({'status': '308', |
| 707 | 'location': 'http://upload.example.com/2', |
| 708 | 'range': '0-12'}, ''), |
| 709 | ({'status': '308', |
| 710 | 'location': 'http://upload.example.com/3', |
Joe Gregorio | 945be3e | 2012-01-27 17:01:06 -0500 | [diff] [blame] | 711 | 'range': '0-%d' % media_upload.size()}, ''), |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 712 | ({'status': '200'}, '{"foo": "bar"}'), |
| 713 | ]) |
| 714 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 715 | body = request.execute(http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 716 | self.assertEquals(body, {"foo": "bar"}) |
| 717 | |
| 718 | def test_resumable_media_fail_unknown_response_code_first_request(self): |
| 719 | """Not a multipart upload.""" |
| 720 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 721 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 722 | |
| 723 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 724 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 725 | |
| 726 | http = HttpMockSequence([ |
| 727 | ({'status': '400', |
| 728 | 'location': 'http://upload.example.com'}, ''), |
| 729 | ]) |
| 730 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 731 | self.assertRaises(ResumableUploadError, request.execute, http=http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 732 | |
| 733 | def test_resumable_media_fail_unknown_response_code_subsequent_request(self): |
| 734 | """Not a multipart upload.""" |
| 735 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 736 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 737 | |
| 738 | media_upload = MediaFileUpload(datafile('small.png'), resumable=True) |
| 739 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 740 | |
| 741 | http = HttpMockSequence([ |
| 742 | ({'status': '200', |
| 743 | 'location': 'http://upload.example.com'}, ''), |
| 744 | ({'status': '400'}, ''), |
| 745 | ]) |
| 746 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 747 | self.assertRaises(HttpError, request.execute, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 748 | self.assertTrue(request._in_error_state) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 749 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 750 | http = HttpMockSequence([ |
| 751 | ({'status': '308', |
| 752 | 'range': '0-5'}, ''), |
| 753 | ({'status': '308', |
| 754 | 'range': '0-6'}, ''), |
| 755 | ]) |
| 756 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 757 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 758 | self.assertEquals(status.resumable_progress, 7, |
| 759 | 'Should have first checked length and then tried to PUT more.') |
| 760 | self.assertFalse(request._in_error_state) |
| 761 | |
| 762 | # Put it back in an error state. |
| 763 | http = HttpMockSequence([ |
| 764 | ({'status': '400'}, ''), |
| 765 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 766 | self.assertRaises(HttpError, request.execute, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 767 | self.assertTrue(request._in_error_state) |
| 768 | |
| 769 | # Pretend the last request that 400'd actually succeeded. |
| 770 | http = HttpMockSequence([ |
| 771 | ({'status': '200'}, '{"foo": "bar"}'), |
| 772 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 773 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 774 | self.assertEqual(body, {'foo': 'bar'}) |
| 775 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 776 | def test_media_io_base_stream_unlimited_chunksize_resume(self): |
| 777 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 778 | zoo = build('zoo', 'v1', http=self.http) |
| 779 | |
| 780 | try: |
| 781 | import io |
| 782 | |
| 783 | # Set up a seekable stream and try to upload in single chunk. |
| 784 | fd = io.BytesIO('01234"56789"') |
| 785 | media_upload = MediaIoBaseUpload( |
| 786 | fd=fd, mimetype='text/plain', chunksize=-1, resumable=True) |
| 787 | |
| 788 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 789 | |
| 790 | # The single chunk fails, restart at the right point. |
| 791 | http = HttpMockSequence([ |
| 792 | ({'status': '200', |
| 793 | 'location': 'http://upload.example.com'}, ''), |
| 794 | ({'status': '308', |
| 795 | 'location': 'http://upload.example.com/2', |
| 796 | 'range': '0-4'}, ''), |
| 797 | ({'status': '200'}, 'echo_request_body'), |
| 798 | ]) |
| 799 | |
| 800 | body = request.execute(http=http) |
| 801 | self.assertEqual('56789', body) |
| 802 | |
| 803 | except ImportError: |
| 804 | pass |
| 805 | |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 806 | |
| 807 | def test_media_io_base_stream_chunksize_resume(self): |
| 808 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 809 | zoo = build('zoo', 'v1', http=self.http) |
| 810 | |
| 811 | try: |
| 812 | import io |
| 813 | |
| 814 | # Set up a seekable stream and try to upload in chunks. |
| 815 | fd = io.BytesIO('0123456789') |
| 816 | media_upload = MediaIoBaseUpload( |
| 817 | fd=fd, mimetype='text/plain', chunksize=5, resumable=True) |
| 818 | |
| 819 | request = zoo.animals().insert(media_body=media_upload, body=None) |
| 820 | |
| 821 | # The single chunk fails, pull the content sent out of the exception. |
| 822 | http = HttpMockSequence([ |
| 823 | ({'status': '200', |
| 824 | 'location': 'http://upload.example.com'}, ''), |
| 825 | ({'status': '400'}, 'echo_request_body'), |
| 826 | ]) |
| 827 | |
| 828 | try: |
| 829 | body = request.execute(http=http) |
| 830 | except HttpError, e: |
| 831 | self.assertEqual('01234', e.content) |
| 832 | |
| 833 | except ImportError: |
| 834 | pass |
| 835 | |
| 836 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 837 | def test_resumable_media_handle_uploads_of_unknown_size(self): |
| 838 | http = HttpMockSequence([ |
| 839 | ({'status': '200', |
| 840 | 'location': 'http://upload.example.com'}, ''), |
| 841 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 842 | ]) |
| 843 | |
| 844 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 845 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 846 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 847 | # 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] | 848 | class IoBaseUnknownLength(MediaUpload): |
| 849 | def chunksize(self): |
| 850 | return 10 |
| 851 | |
| 852 | def mimetype(self): |
| 853 | return 'image/png' |
| 854 | |
| 855 | def size(self): |
| 856 | return None |
| 857 | |
| 858 | def resumable(self): |
| 859 | return True |
| 860 | |
| 861 | def getbytes(self, begin, length): |
| 862 | return '0123456789' |
| 863 | |
| 864 | upload = IoBaseUnknownLength() |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 865 | |
| 866 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 867 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 868 | self.assertEqual(body, { |
| 869 | 'Content-Range': 'bytes 0-9/*', |
| 870 | 'Content-Length': '10', |
| 871 | }) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 872 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 873 | def test_resumable_media_no_streaming_on_unsupported_platforms(self): |
| 874 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 875 | zoo = build('zoo', 'v1', http=self.http) |
| 876 | |
| 877 | class IoBaseHasStream(MediaUpload): |
| 878 | def chunksize(self): |
| 879 | return 10 |
| 880 | |
| 881 | def mimetype(self): |
| 882 | return 'image/png' |
| 883 | |
| 884 | def size(self): |
| 885 | return None |
| 886 | |
| 887 | def resumable(self): |
| 888 | return True |
| 889 | |
| 890 | def getbytes(self, begin, length): |
| 891 | return '0123456789' |
| 892 | |
| 893 | def has_stream(self): |
| 894 | return True |
| 895 | |
| 896 | def stream(self): |
| 897 | raise NotImplementedError() |
| 898 | |
| 899 | upload = IoBaseHasStream() |
| 900 | |
| 901 | orig_version = sys.version_info |
| 902 | sys.version_info = (2, 5, 5, 'final', 0) |
| 903 | |
| 904 | request = zoo.animals().insert(media_body=upload, body=None) |
| 905 | |
| 906 | http = HttpMockSequence([ |
| 907 | ({'status': '200', |
| 908 | 'location': 'http://upload.example.com'}, ''), |
| 909 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 910 | ]) |
| 911 | |
| 912 | # This should not raise an exception because stream() shouldn't be called. |
| 913 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 914 | self.assertEqual(body, { |
| 915 | 'Content-Range': 'bytes 0-9/*', |
| 916 | 'Content-Length': '10' |
| 917 | }) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 918 | |
| 919 | sys.version_info = (2, 6, 5, 'final', 0) |
| 920 | |
| 921 | request = zoo.animals().insert(media_body=upload, body=None) |
| 922 | |
| 923 | # This should raise an exception because stream() will be called. |
| 924 | http = HttpMockSequence([ |
| 925 | ({'status': '200', |
| 926 | 'location': 'http://upload.example.com'}, ''), |
| 927 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 928 | ]) |
| 929 | |
| 930 | self.assertRaises(NotImplementedError, request.next_chunk, http=http) |
| 931 | |
| 932 | sys.version_info = orig_version |
| 933 | |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 934 | def test_resumable_media_handle_uploads_of_unknown_size_eof(self): |
| 935 | http = HttpMockSequence([ |
| 936 | ({'status': '200', |
| 937 | 'location': 'http://upload.example.com'}, ''), |
| 938 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 939 | ]) |
| 940 | |
| 941 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 942 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 943 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 944 | fd = StringIO.StringIO('data goes here') |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 945 | |
| 946 | # Create an upload that doesn't know the full size of the media. |
| 947 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 948 | fd=fd, mimetype='image/png', chunksize=15, resumable=True) |
Joe Gregorio | 44454e4 | 2012-06-15 08:38:53 -0400 | [diff] [blame] | 949 | |
| 950 | request = zoo.animals().insert(media_body=upload, body=None) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 951 | status, body = request.next_chunk(http=http) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 952 | self.assertEqual(body, { |
| 953 | 'Content-Range': 'bytes 0-13/14', |
| 954 | 'Content-Length': '14', |
| 955 | }) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 956 | |
| 957 | def test_resumable_media_handle_resume_of_upload_of_unknown_size(self): |
| 958 | http = HttpMockSequence([ |
| 959 | ({'status': '200', |
| 960 | 'location': 'http://upload.example.com'}, ''), |
| 961 | ({'status': '400'}, ''), |
| 962 | ]) |
| 963 | |
| 964 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 965 | zoo = build('zoo', 'v1', http=self.http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 966 | |
| 967 | # Create an upload that doesn't know the full size of the media. |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 968 | fd = StringIO.StringIO('data goes here') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 969 | |
| 970 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 971 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 972 | |
| 973 | request = zoo.animals().insert(media_body=upload, body=None) |
| 974 | |
| 975 | # Put it in an error state. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 976 | self.assertRaises(HttpError, request.next_chunk, http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 977 | |
| 978 | http = HttpMockSequence([ |
| 979 | ({'status': '400', |
| 980 | 'range': '0-5'}, 'echo_request_headers_as_json'), |
| 981 | ]) |
| 982 | try: |
| 983 | # Should resume the upload by first querying the status of the upload. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 984 | request.next_chunk(http=http) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 985 | except HttpError, e: |
| 986 | expected = { |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 987 | 'Content-Range': 'bytes */14', |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 988 | 'content-length': '0' |
| 989 | } |
| 990 | self.assertEqual(expected, simplejson.loads(e.content), |
| 991 | 'Should send an empty body when requesting the current upload status.') |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 992 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 993 | def test_pickle(self): |
| 994 | sorted_resource_keys = ['_baseUrl', |
| 995 | '_developerKey', |
| 996 | '_dynamic_attrs', |
| 997 | '_http', |
| 998 | '_model', |
| 999 | '_requestBuilder', |
| 1000 | '_resourceDesc', |
| 1001 | '_rootDesc', |
| 1002 | '_schema', |
| 1003 | 'animals', |
| 1004 | 'global_', |
| 1005 | 'load', |
| 1006 | 'loadNoTemplate', |
| 1007 | 'my', |
| 1008 | 'query', |
| 1009 | 'scopedAnimals'] |
| 1010 | |
| 1011 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 1012 | zoo = build('zoo', 'v1', http=http) |
| 1013 | self.assertEqual(sorted(zoo.__dict__.keys()), sorted_resource_keys) |
| 1014 | |
| 1015 | pickled_zoo = pickle.dumps(zoo) |
| 1016 | new_zoo = pickle.loads(pickled_zoo) |
| 1017 | self.assertEqual(sorted(new_zoo.__dict__.keys()), sorted_resource_keys) |
| 1018 | self.assertTrue(hasattr(new_zoo, 'animals')) |
| 1019 | self.assertTrue(callable(new_zoo.animals)) |
| 1020 | self.assertTrue(hasattr(new_zoo, 'global_')) |
| 1021 | self.assertTrue(callable(new_zoo.global_)) |
| 1022 | self.assertTrue(hasattr(new_zoo, 'load')) |
| 1023 | self.assertTrue(callable(new_zoo.load)) |
| 1024 | self.assertTrue(hasattr(new_zoo, 'loadNoTemplate')) |
| 1025 | self.assertTrue(callable(new_zoo.loadNoTemplate)) |
| 1026 | self.assertTrue(hasattr(new_zoo, 'my')) |
| 1027 | self.assertTrue(callable(new_zoo.my)) |
| 1028 | self.assertTrue(hasattr(new_zoo, 'query')) |
| 1029 | self.assertTrue(callable(new_zoo.query)) |
| 1030 | self.assertTrue(hasattr(new_zoo, 'scopedAnimals')) |
| 1031 | self.assertTrue(callable(new_zoo.scopedAnimals)) |
| 1032 | |
Joe Gregorio | 003b6e4 | 2013-02-13 15:42:19 -0500 | [diff] [blame] | 1033 | self.assertEqual(sorted(zoo._dynamic_attrs), sorted(new_zoo._dynamic_attrs)) |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1034 | self.assertEqual(zoo._baseUrl, new_zoo._baseUrl) |
| 1035 | self.assertEqual(zoo._developerKey, new_zoo._developerKey) |
| 1036 | self.assertEqual(zoo._requestBuilder, new_zoo._requestBuilder) |
| 1037 | self.assertEqual(zoo._resourceDesc, new_zoo._resourceDesc) |
| 1038 | self.assertEqual(zoo._rootDesc, new_zoo._rootDesc) |
| 1039 | # _http, _model and _schema won't be equal since we will get new |
| 1040 | # instances upon un-pickling |
| 1041 | |
| 1042 | def _dummy_zoo_request(self): |
| 1043 | with open(os.path.join(DATA_DIR, 'zoo.json'), 'rU') as fh: |
| 1044 | zoo_contents = fh.read() |
| 1045 | |
| 1046 | zoo_uri = uritemplate.expand(DISCOVERY_URI, |
| 1047 | {'api': 'zoo', 'apiVersion': 'v1'}) |
| 1048 | if 'REMOTE_ADDR' in os.environ: |
| 1049 | zoo_uri = _add_query_parameter(zoo_uri, 'userIp', |
| 1050 | os.environ['REMOTE_ADDR']) |
| 1051 | |
| 1052 | http = httplib2.Http() |
| 1053 | original_request = http.request |
| 1054 | def wrapped_request(uri, method='GET', *args, **kwargs): |
| 1055 | if uri == zoo_uri: |
| 1056 | return httplib2.Response({'status': '200'}), zoo_contents |
| 1057 | return original_request(uri, method=method, *args, **kwargs) |
| 1058 | http.request = wrapped_request |
| 1059 | return http |
| 1060 | |
| 1061 | def _dummy_token(self): |
| 1062 | access_token = 'foo' |
| 1063 | client_id = 'some_client_id' |
| 1064 | client_secret = 'cOuDdkfjxxnv+' |
| 1065 | refresh_token = '1/0/a.df219fjls0' |
| 1066 | token_expiry = datetime.datetime.utcnow() |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1067 | user_agent = 'refresh_checker/1.0' |
| 1068 | return OAuth2Credentials( |
| 1069 | access_token, client_id, client_secret, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1070 | refresh_token, token_expiry, GOOGLE_TOKEN_URI, |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1071 | user_agent) |
| 1072 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 1073 | def test_pickle_with_credentials(self): |
| 1074 | credentials = self._dummy_token() |
| 1075 | http = self._dummy_zoo_request() |
| 1076 | http = credentials.authorize(http) |
| 1077 | self.assertTrue(hasattr(http.request, 'credentials')) |
| 1078 | |
| 1079 | zoo = build('zoo', 'v1', http=http) |
| 1080 | pickled_zoo = pickle.dumps(zoo) |
| 1081 | new_zoo = pickle.loads(pickled_zoo) |
| 1082 | self.assertEqual(sorted(zoo.__dict__.keys()), |
| 1083 | sorted(new_zoo.__dict__.keys())) |
| 1084 | new_http = new_zoo._http |
| 1085 | self.assertFalse(hasattr(new_http.request, 'credentials')) |
| 1086 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1087 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 1088 | class Next(unittest.TestCase): |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 1089 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1090 | def test_next_successful_none_on_no_next_page_token(self): |
| 1091 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1092 | tasks = build('tasks', 'v1', http=self.http) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1093 | request = tasks.tasklists().list() |
| 1094 | self.assertEqual(None, tasks.tasklists().list_next(request, {})) |
| 1095 | |
| 1096 | def test_next_successful_with_next_page_token(self): |
| 1097 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1098 | tasks = build('tasks', 'v1', http=self.http) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1099 | request = tasks.tasklists().list() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 1100 | next_request = tasks.tasklists().list_next( |
| 1101 | request, {'nextPageToken': '123abc'}) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 1102 | parsed = list(urlparse.urlparse(next_request.uri)) |
| 1103 | q = parse_qs(parsed[4]) |
| 1104 | self.assertEqual(q['pageToken'][0], '123abc') |
| 1105 | |
Joe Gregorio | 555f33c | 2011-08-19 14:56:07 -0400 | [diff] [blame] | 1106 | def test_next_with_method_with_no_properties(self): |
| 1107 | self.http = HttpMock(datafile('latitude.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1108 | service = build('latitude', 'v1', http=self.http) |
Joe Gregorio | 555f33c | 2011-08-19 14:56:07 -0400 | [diff] [blame] | 1109 | request = service.currentLocation().get() |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 1110 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 1111 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1112 | class MediaGet(unittest.TestCase): |
| 1113 | |
| 1114 | def test_get_media(self): |
| 1115 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1116 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1117 | request = zoo.animals().get_media(name='Lion') |
| 1118 | |
| 1119 | parsed = urlparse.urlparse(request.uri) |
| 1120 | q = parse_qs(parsed[4]) |
| 1121 | self.assertEqual(q['alt'], ['media']) |
| 1122 | self.assertEqual(request.headers['accept'], '*/*') |
| 1123 | |
| 1124 | http = HttpMockSequence([ |
| 1125 | ({'status': '200'}, 'standing in for media'), |
| 1126 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1127 | response = request.execute(http=http) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 1128 | self.assertEqual('standing in for media', response) |
| 1129 | |
| 1130 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1131 | if __name__ == '__main__': |
| 1132 | unittest.main() |