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: |
| 64 | buzz = build('buzz', 'v1', self.http) |
| 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): |
| 73 | discovery = file(datafile('buzz.json')).read() |
| 74 | buzz = build_from_document(discovery, base="https://www.googleapis.com/") |
| 75 | self.assertTrue(buzz 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): |
| 78 | discovery = file(datafile('buzz.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/" |
| 81 | buzz = build_from_document(discovery, base=base) |
| 82 | self.assertEquals(base + "buzz/v1/", buzz._baseUrl) |
| 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 | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 123 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 124 | buzz = build('buzz', 'v1', self.http) |
| 125 | |
| 126 | # Missing required parameters |
| 127 | try: |
| 128 | buzz.activities().list() |
| 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 | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 135 | buzz.activities().list(scope='@myself', 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 | # Parameter doesn't match regex |
| 141 | try: |
| 142 | buzz.activities().list(scope='not@', userId='foo') |
| 143 | self.fail() |
| 144 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 145 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 146 | |
| 147 | # Unexpected parameter |
| 148 | try: |
| 149 | buzz.activities().list(flubber=12) |
| 150 | self.fail() |
| 151 | except TypeError, e: |
| 152 | self.assertTrue('unexpected' in str(e)) |
| 153 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 154 | def _check_query_types(self, request): |
| 155 | parsed = urlparse.urlparse(request.uri) |
| 156 | q = parse_qs(parsed[4]) |
| 157 | self.assertEqual(q['q'], ['foo']) |
| 158 | self.assertEqual(q['i'], ['1']) |
| 159 | self.assertEqual(q['n'], ['1.0']) |
| 160 | self.assertEqual(q['b'], ['false']) |
| 161 | self.assertEqual(q['a'], ['[1, 2, 3]']) |
| 162 | self.assertEqual(q['o'], ['{\'a\': 1}']) |
| 163 | self.assertEqual(q['e'], ['bar']) |
| 164 | |
| 165 | def test_type_coercion(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 166 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 167 | zoo = build('zoo', 'v1', http) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -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.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] | 171 | self._check_query_types(request) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 172 | request = zoo.query( |
| 173 | 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] | 174 | self._check_query_types(request) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 175 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 176 | request = zoo.query( |
| 177 | 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] | 178 | self._check_query_types(request) |
| 179 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 180 | def test_optional_stack_query_parameters(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 181 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 182 | zoo = build('zoo', 'v1', http) |
| 183 | request = zoo.query(trace='html', fields='description') |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 184 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 185 | parsed = urlparse.urlparse(request.uri) |
| 186 | q = parse_qs(parsed[4]) |
| 187 | self.assertEqual(q['trace'], ['html']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 188 | self.assertEqual(q['fields'], ['description']) |
| 189 | |
| 190 | def test_patch(self): |
| 191 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 192 | zoo = build('zoo', 'v1', http) |
| 193 | request = zoo.animals().patch(name='lion', body='{"description": "foo"}') |
| 194 | |
| 195 | self.assertEqual(request.method, 'PATCH') |
| 196 | |
| 197 | def test_tunnel_patch(self): |
| 198 | http = HttpMockSequence([ |
| 199 | ({'status': '200'}, file(datafile('zoo.json'), 'r').read()), |
| 200 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 201 | ]) |
| 202 | http = tunnel_patch(http) |
| 203 | zoo = build('zoo', 'v1', http) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 204 | resp = zoo.animals().patch( |
| 205 | name='lion', body='{"description": "foo"}').execute() |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 206 | |
| 207 | self.assertTrue('x-http-method-override' in resp) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 208 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 209 | def test_buzz_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 210 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 211 | buzz = build('buzz', 'v1', self.http) |
| 212 | self.assertTrue(getattr(buzz, 'activities')) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 213 | self.assertTrue(getattr(buzz, 'photos')) |
| 214 | self.assertTrue(getattr(buzz, 'people')) |
| 215 | self.assertTrue(getattr(buzz, 'groups')) |
| 216 | self.assertTrue(getattr(buzz, 'comments')) |
| 217 | self.assertTrue(getattr(buzz, 'related')) |
| 218 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 219 | def test_auth(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 220 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 221 | buzz = build('buzz', 'v1', self.http) |
| 222 | auth = buzz.auth_discovery() |
| 223 | self.assertTrue('request' in auth) |
| 224 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 225 | def test_full_featured(self): |
| 226 | # Zoo should exercise all discovery facets |
| 227 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 228 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 229 | zoo = build('zoo', 'v1', self.http) |
| 230 | self.assertTrue(getattr(zoo, 'animals')) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 231 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 232 | request = zoo.animals().list(name='bat', projection="full") |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 233 | parsed = urlparse.urlparse(request.uri) |
| 234 | q = parse_qs(parsed[4]) |
| 235 | self.assertEqual(q['name'], ['bat']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 236 | self.assertEqual(q['projection'], ['full']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 237 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 238 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 239 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 240 | zoo = build('zoo', 'v1', self.http) |
| 241 | self.assertTrue(getattr(zoo, 'animals')) |
| 242 | request = zoo.my().favorites().list(max_results="5") |
| 243 | parsed = urlparse.urlparse(request.uri) |
| 244 | q = parse_qs(parsed[4]) |
| 245 | self.assertEqual(q['max-results'], ['5']) |
| 246 | |
Joe Gregorio | d92897c | 2011-07-07 11:44:56 -0400 | [diff] [blame] | 247 | def test_methods_with_reserved_names(self): |
| 248 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 249 | zoo = build('zoo', 'v1', self.http) |
| 250 | self.assertTrue(getattr(zoo, 'animals')) |
| 251 | request = zoo.global_().print_().assert_(max_results="5") |
| 252 | parsed = urlparse.urlparse(request.uri) |
| 253 | self.assertEqual(parsed[2], '/zoo/global/print/assert') |
| 254 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 255 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 256 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 257 | zoo = build('zoo', 'v1', self.http) |
| 258 | self.assertTrue(getattr(zoo, 'query')) |
| 259 | request = zoo.query(q="foo") |
| 260 | parsed = urlparse.urlparse(request.uri) |
| 261 | q = parse_qs(parsed[4]) |
| 262 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 263 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 264 | def test_simple_media_uploads(self): |
| 265 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 266 | zoo = build('zoo', 'v1', self.http) |
| 267 | doc = getattr(zoo.animals().insert, '__doc__') |
| 268 | self.assertTrue('media_body' in doc) |
| 269 | |
Joe Gregorio | 84d3c1f | 2011-07-25 10:39:45 -0400 | [diff] [blame] | 270 | def test_simple_media_upload_no_max_size_provided(self): |
| 271 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 272 | zoo = build('zoo', 'v1', self.http) |
| 273 | request = zoo.animals().crossbreed(media_body=datafile('small.png')) |
| 274 | self.assertEquals('image/png', request.headers['content-type']) |
| 275 | self.assertEquals('PNG', request.body[1:4]) |
| 276 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 277 | def test_simple_media_raise_correct_exceptions(self): |
| 278 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 279 | zoo = build('zoo', 'v1', self.http) |
| 280 | |
| 281 | try: |
| 282 | zoo.animals().insert(media_body=datafile('smiley.png')) |
| 283 | self.fail("should throw exception if media is too large.") |
| 284 | except MediaUploadSizeError: |
| 285 | pass |
| 286 | |
| 287 | try: |
| 288 | zoo.animals().insert(media_body=datafile('small.jpg')) |
| 289 | self.fail("should throw exception if mimetype is unacceptable.") |
| 290 | except UnacceptableMimeTypeError: |
| 291 | pass |
| 292 | |
| 293 | def test_simple_media_good_upload(self): |
| 294 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 295 | zoo = build('zoo', 'v1', self.http) |
| 296 | |
| 297 | request = zoo.animals().insert(media_body=datafile('small.png')) |
| 298 | self.assertEquals('image/png', request.headers['content-type']) |
| 299 | self.assertEquals('PNG', request.body[1:4]) |
| 300 | |
| 301 | def test_multipart_media_raise_correct_exceptions(self): |
| 302 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 303 | zoo = build('zoo', 'v1', self.http) |
| 304 | |
| 305 | try: |
| 306 | zoo.animals().insert(media_body=datafile('smiley.png'), body={}) |
| 307 | self.fail("should throw exception if media is too large.") |
| 308 | except MediaUploadSizeError: |
| 309 | pass |
| 310 | |
| 311 | try: |
| 312 | zoo.animals().insert(media_body=datafile('small.jpg'), body={}) |
| 313 | self.fail("should throw exception if mimetype is unacceptable.") |
| 314 | except UnacceptableMimeTypeError: |
| 315 | pass |
| 316 | |
| 317 | def test_multipart_media_good_upload(self): |
| 318 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 319 | zoo = build('zoo', 'v1', self.http) |
| 320 | |
| 321 | request = zoo.animals().insert(media_body=datafile('small.png'), body={}) |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 322 | self.assertTrue(request.headers['content-type'].startswith( |
| 323 | 'multipart/related')) |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 324 | self.assertEquals('--==', request.body[0:4]) |
| 325 | |
| 326 | def test_media_capable_method_without_media(self): |
| 327 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 328 | zoo = build('zoo', 'v1', self.http) |
| 329 | |
| 330 | request = zoo.animals().insert(body={}) |
| 331 | self.assertTrue(request.headers['content-type'], 'application/json') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 332 | |
| 333 | class Next(unittest.TestCase): |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 334 | def test_next_for_people_liked(self): |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 335 | """Legacy test for Buzz _next support.""" |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 336 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 337 | buzz = build('buzz', 'v1', self.http) |
| 338 | people = {'links': |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 339 | {'next': |
| 340 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 341 | request = buzz.people().liked_next(people) |
| 342 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 343 | |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 344 | def test_next_successful_none_on_no_next_page_token(self): |
| 345 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
| 346 | tasks = build('tasks', 'v1', self.http) |
| 347 | request = tasks.tasklists().list() |
| 348 | self.assertEqual(None, tasks.tasklists().list_next(request, {})) |
| 349 | |
| 350 | def test_next_successful_with_next_page_token(self): |
| 351 | self.http = HttpMock(datafile('tasks.json'), {'status': '200'}) |
| 352 | tasks = build('tasks', 'v1', self.http) |
| 353 | request = tasks.tasklists().list() |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 354 | next_request = tasks.tasklists().list_next( |
| 355 | request, {'nextPageToken': '123abc'}) |
Joe Gregorio | 3c676f9 | 2011-07-25 10:38:14 -0400 | [diff] [blame] | 356 | parsed = list(urlparse.urlparse(next_request.uri)) |
| 357 | q = parse_qs(parsed[4]) |
| 358 | self.assertEqual(q['pageToken'][0], '123abc') |
| 359 | |
Joe Gregorio | 555f33c | 2011-08-19 14:56:07 -0400 | [diff] [blame] | 360 | def test_next_with_method_with_no_properties(self): |
| 361 | self.http = HttpMock(datafile('latitude.json'), {'status': '200'}) |
| 362 | service = build('latitude', 'v1', self.http) |
| 363 | request = service.currentLocation().get() |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 364 | |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 365 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 366 | class DeveloperKey(unittest.TestCase): |
Joe Gregorio | a98733f | 2011-09-16 10:12:28 -0400 | [diff] [blame] | 367 | |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 368 | def test_param(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 369 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 370 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 371 | activities = {'links': |
| 372 | {'next': |
| 373 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 374 | request = buzz.activities().list_next(activities) |
| 375 | parsed = urlparse.urlparse(request.uri) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 376 | q = parse_qs(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 377 | self.assertEqual(q['key'], ['foobie_bletch']) |
| 378 | |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 379 | def test_next_for_activities_list(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 380 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 381 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 382 | activities = {'links': |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 383 | {'next': |
| 384 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 385 | request = buzz.activities().list_next(activities) |
| 386 | self.assertEqual(request.uri, |
| 387 | 'http://www.googleapis.com/next-link?key=foobie_bletch') |
| 388 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 389 | |
| 390 | if __name__ == '__main__': |
| 391 | unittest.main() |