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