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