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 | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 30 | |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 31 | try: |
| 32 | from urlparse import parse_qs |
| 33 | except ImportError: |
| 34 | from cgi import parse_qs |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 35 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 36 | from apiclient.discovery import build, build_from_document, key2param |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 37 | from apiclient.http import HttpMock |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 38 | from apiclient.http import tunnel_patch |
| 39 | from apiclient.http import HttpMockSequence |
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 |
| 43 | from apiclient.errors import UnacceptableMimeTypeError |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 44 | |
| 45 | |
| 46 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 47 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 48 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 49 | def datafile(filename): |
| 50 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 51 | |
| 52 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 53 | class Utilities(unittest.TestCase): |
| 54 | def test_key2param(self): |
| 55 | self.assertEqual('max_results', key2param('max-results')) |
| 56 | self.assertEqual('x007_bond', key2param('007-bond')) |
| 57 | |
| 58 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 59 | class DiscoveryErrors(unittest.TestCase): |
| 60 | |
| 61 | def test_failed_to_parse_discovery_json(self): |
| 62 | self.http = HttpMock(datafile('malformed.json'), {'status': '200'}) |
| 63 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 64 | plus = build('plus', 'v1', self.http) |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 65 | self.fail("should have raised an exception over malformed JSON.") |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 66 | except InvalidJsonError: |
| 67 | pass |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 68 | |
| 69 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 70 | class DiscoveryFromDocument(unittest.TestCase): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 71 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 72 | def test_can_build_from_local_document(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 73 | discovery = file(datafile('plus.json')).read() |
| 74 | plus = build_from_document(discovery, base="https://www.googleapis.com/") |
| 75 | self.assertTrue(plus is not None) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 76 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 77 | def test_building_with_base_remembers_base(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 78 | discovery = file(datafile('plus.json')).read() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 79 | |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 80 | base = "https://www.example.com/" |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 81 | plus = build_from_document(discovery, base=base) |
| 82 | self.assertEquals(base + "plus/v1/", plus._baseUrl) |
ade@google.com | 6a8c1cb | 2011-09-06 17:40:00 +0100 | [diff] [blame] | 83 | |
| 84 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 85 | class DiscoveryFromHttp(unittest.TestCase): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 86 | def setUp(self): |
Joe Beda | fb463cb | 2011-09-19 17:39:49 -0700 | [diff] [blame] | 87 | self.old_environ = os.environ.copy() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 88 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 89 | def tearDown(self): |
| 90 | os.environ = self.old_environ |
| 91 | |
| 92 | def test_userip_is_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 93 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 94 | # out of the raised exception. |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 95 | os.environ['REMOTE_ADDR'] = '10.0.0.1' |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 96 | try: |
| 97 | http = HttpMockSequence([ |
| 98 | ({'status': '400'}, file(datafile('zoo.json'), 'r').read()), |
| 99 | ]) |
| 100 | zoo = build('zoo', 'v1', http, developerKey='foo', |
| 101 | discoveryServiceUrl='http://example.com') |
| 102 | self.fail('Should have raised an exception.') |
| 103 | except HttpError, e: |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 104 | self.assertEqual(e.uri, 'http://example.com?userIp=10.0.0.1') |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 105 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 106 | def test_userip_missing_is_not_added_to_discovery_uri(self): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 107 | # build() will raise an HttpError on a 400, use this to pick the request uri |
| 108 | # out of the raised exception. |
| 109 | try: |
| 110 | http = HttpMockSequence([ |
| 111 | ({'status': '400'}, file(datafile('zoo.json'), 'r').read()), |
| 112 | ]) |
| 113 | zoo = build('zoo', 'v1', http, developerKey=None, |
| 114 | discoveryServiceUrl='http://example.com') |
| 115 | self.fail('Should have raised an exception.') |
| 116 | except HttpError, e: |
| 117 | self.assertEqual(e.uri, 'http://example.com') |
| 118 | |
| 119 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 120 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 121 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 122 | def test_method_error_checking(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 123 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 124 | plus = build('plus', 'v1', self.http) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 125 | |
| 126 | # Missing required parameters |
| 127 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 128 | plus.activities().list() |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 129 | self.fail() |
| 130 | except TypeError, e: |
| 131 | self.assertTrue('Missing' in str(e)) |
| 132 | |
| 133 | # Parameter doesn't match regex |
| 134 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 135 | plus.activities().list(collection='not_a_collection_name', userId='me') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 136 | self.fail() |
| 137 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 138 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 139 | |
| 140 | # Unexpected parameter |
| 141 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 142 | plus.activities().list(flubber=12) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 143 | self.fail() |
| 144 | except TypeError, e: |
| 145 | self.assertTrue('unexpected' in str(e)) |
| 146 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 147 | def _check_query_types(self, request): |
| 148 | parsed = urlparse.urlparse(request.uri) |
| 149 | q = parse_qs(parsed[4]) |
| 150 | self.assertEqual(q['q'], ['foo']) |
| 151 | self.assertEqual(q['i'], ['1']) |
| 152 | self.assertEqual(q['n'], ['1.0']) |
| 153 | self.assertEqual(q['b'], ['false']) |
| 154 | self.assertEqual(q['a'], ['[1, 2, 3]']) |
| 155 | self.assertEqual(q['o'], ['{\'a\': 1}']) |
| 156 | self.assertEqual(q['e'], ['bar']) |
| 157 | |
| 158 | def test_type_coercion(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 159 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 160 | zoo = build('zoo', 'v1', http) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 161 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 162 | request = zoo.query( |
| 163 | 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] | 164 | self._check_query_types(request) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 165 | request = zoo.query( |
| 166 | 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] | 167 | self._check_query_types(request) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 168 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 169 | request = zoo.query( |
| 170 | q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar') |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 171 | self._check_query_types(request) |
| 172 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 173 | def test_optional_stack_query_parameters(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 174 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 175 | zoo = build('zoo', 'v1', http) |
| 176 | request = zoo.query(trace='html', fields='description') |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 177 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 178 | parsed = urlparse.urlparse(request.uri) |
| 179 | q = parse_qs(parsed[4]) |
| 180 | self.assertEqual(q['trace'], ['html']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 181 | self.assertEqual(q['fields'], ['description']) |
| 182 | |
| 183 | def test_patch(self): |
| 184 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 185 | zoo = build('zoo', 'v1', http) |
| 186 | request = zoo.animals().patch(name='lion', body='{"description": "foo"}') |
| 187 | |
| 188 | self.assertEqual(request.method, 'PATCH') |
| 189 | |
| 190 | def test_tunnel_patch(self): |
| 191 | http = HttpMockSequence([ |
| 192 | ({'status': '200'}, file(datafile('zoo.json'), 'r').read()), |
| 193 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 194 | ]) |
| 195 | http = tunnel_patch(http) |
| 196 | zoo = build('zoo', 'v1', http) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 197 | resp = zoo.animals().patch( |
| 198 | name='lion', body='{"description": "foo"}').execute() |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 199 | |
| 200 | self.assertTrue('x-http-method-override' in resp) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 201 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame^] | 202 | def test_plus_resources(self): |
| 203 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 204 | plus = build('plus', 'v1', self.http) |
| 205 | self.assertTrue(getattr(plus, 'activities')) |
| 206 | self.assertTrue(getattr(plus, 'people')) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 207 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 208 | def test_full_featured(self): |
| 209 | # Zoo should exercise all discovery facets |
| 210 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 211 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 212 | zoo = build('zoo', 'v1', self.http) |
| 213 | self.assertTrue(getattr(zoo, 'animals')) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 214 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 215 | request = zoo.animals().list(name='bat', projection="full") |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 216 | parsed = urlparse.urlparse(request.uri) |
| 217 | q = parse_qs(parsed[4]) |
| 218 | self.assertEqual(q['name'], ['bat']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 219 | self.assertEqual(q['projection'], ['full']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 220 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 221 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 222 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 223 | zoo = build('zoo', 'v1', self.http) |
| 224 | self.assertTrue(getattr(zoo, 'animals')) |
| 225 | request = zoo.my().favorites().list(max_results="5") |
| 226 | parsed = urlparse.urlparse(request.uri) |
| 227 | q = parse_qs(parsed[4]) |
| 228 | self.assertEqual(q['max-results'], ['5']) |
| 229 | |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 230 | def test_methods_with_reserved_names(self): |
| 231 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 232 | zoo = build('zoo', 'v1', self.http) |
| 233 | self.assertTrue(getattr(zoo, 'animals')) |
| 234 | request = zoo.global_().print_().assert_(max_results="5") |
| 235 | parsed = urlparse.urlparse(request.uri) |
| 236 | self.assertEqual(parsed[2], '/zoo/global/print/assert') |
| 237 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 238 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 239 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 240 | zoo = build('zoo', 'v1', self.http) |
| 241 | self.assertTrue(getattr(zoo, 'query')) |
| 242 | request = zoo.query(q="foo") |
| 243 | parsed = urlparse.urlparse(request.uri) |
| 244 | q = parse_qs(parsed[4]) |
| 245 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 246 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 247 | def test_simple_media_uploads(self): |
| 248 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 249 | zoo = build('zoo', 'v1', self.http) |
| 250 | doc = getattr(zoo.animals().insert, '__doc__') |
| 251 | self.assertTrue('media_body' in doc) |
| 252 | |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 253 | def test_simple_media_upload_no_max_size_provided(self): |
| 254 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 255 | zoo = build('zoo', 'v1', self.http) |
| 256 | request = zoo.animals().crossbreed(media_body=datafile('small.png')) |
| 257 | self.assertEquals('image/png', request.headers['content-type']) |
| 258 | self.assertEquals('PNG', request.body[1:4]) |
| 259 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 260 | def test_simple_media_raise_correct_exceptions(self): |
| 261 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 262 | zoo = build('zoo', 'v1', self.http) |
| 263 | |
| 264 | try: |
| 265 | zoo.animals().insert(media_body=datafile('smiley.png')) |
| 266 | self.fail("should throw exception if media is too large.") |
| 267 | except MediaUploadSizeError: |
| 268 | pass |
| 269 | |
| 270 | try: |
| 271 | zoo.animals().insert(media_body=datafile('small.jpg')) |
| 272 | self.fail("should throw exception if mimetype is unacceptable.") |
| 273 | except UnacceptableMimeTypeError: |
| 274 | pass |
| 275 | |
| 276 | def test_simple_media_good_upload(self): |
| 277 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 278 | zoo = build('zoo', 'v1', self.http) |
| 279 | |
| 280 | request = zoo.animals().insert(media_body=datafile('small.png')) |
| 281 | self.assertEquals('image/png', request.headers['content-type']) |
| 282 | self.assertEquals('PNG', request.body[1:4]) |
| 283 | |
| 284 | def test_multipart_media_raise_correct_exceptions(self): |
| 285 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 286 | zoo = build('zoo', 'v1', self.http) |
| 287 | |
| 288 | try: |
| 289 | zoo.animals().insert(media_body=datafile('smiley.png'), body={}) |
| 290 | self.fail("should throw exception if media is too large.") |
| 291 | except MediaUploadSizeError: |
| 292 | pass |
| 293 | |
| 294 | try: |
| 295 | zoo.animals().insert(media_body=datafile('small.jpg'), body={}) |
| 296 | self.fail("should throw exception if mimetype is unacceptable.") |
| 297 | except UnacceptableMimeTypeError: |
| 298 | pass |
| 299 | |
| 300 | def test_multipart_media_good_upload(self): |
| 301 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 302 | zoo = build('zoo', 'v1', self.http) |
| 303 | |
| 304 | request = zoo.animals().insert(media_body=datafile('small.png'), body={}) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 305 | self.assertTrue(request.headers['content-type'].startswith( |
| 306 | 'multipart/related')) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 307 | self.assertEquals('--==', request.body[0:4]) |
| 308 | |
| 309 | def test_media_capable_method_without_media(self): |
| 310 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 311 | zoo = build('zoo', 'v1', self.http) |
| 312 | |
| 313 | request = zoo.animals().insert(body={}) |
| 314 | self.assertTrue(request.headers['content-type'], 'application/json') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 315 | |
| 316 | class Next(unittest.TestCase): |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 317 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 318 | def test_next_successful_none_on_no_next_page_token(self): |
| 319 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
| 320 | tasks = build('tasks', 'v1', self.http) |
| 321 | request = tasks.tasklists().list() |
| 322 | self.assertEqual(None, tasks.tasklists().list_next(request, {})) |
| 323 | |
| 324 | def test_next_successful_with_next_page_token(self): |
| 325 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
| 326 | tasks = build('tasks', 'v1', self.http) |
| 327 | request = tasks.tasklists().list() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 328 | next_request = tasks.tasklists().list_next( |
| 329 | request, {'nextPageToken': '123abc'}) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 330 | parsed = list(urlparse.urlparse(next_request.uri)) |
| 331 | q = parse_qs(parsed[4]) |
| 332 | self.assertEqual(q['pageToken'][0], '123abc') |
| 333 | |
Joe Gregorio | 555f33c | 2011-08-19 14:56:07 -0400 | [diff] [blame] | 334 | def test_next_with_method_with_no_properties(self): |
| 335 | self.http = HttpMock(datafile('latitude.json'), {'status': '200'}) |
| 336 | service = build('latitude', 'v1', self.http) |
| 337 | request = service.currentLocation().get() |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 338 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 339 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 340 | if __name__ == '__main__': |
| 341 | unittest.main() |