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