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