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