Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Http tests |
| 18 | |
| 19 | Unit tests for the apiclient.http. |
| 20 | """ |
| 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 23 | |
Joe Gregorio | 7cbceab | 2011-06-27 10:46:54 -0400 | [diff] [blame] | 24 | # Do not remove the httplib2 import |
| 25 | import httplib2 |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 26 | import os |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 27 | import unittest |
Joe Gregorio | ba5c790 | 2012-08-03 12:48:16 -0400 | [diff] [blame] | 28 | import urllib |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 29 | import StringIO |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 30 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 31 | from apiclient.discovery import build |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 32 | from apiclient.errors import BatchError |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 33 | from apiclient.errors import HttpError |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 34 | from apiclient.errors import InvalidChunkSizeError |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 35 | from apiclient.http import BatchHttpRequest |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 36 | from apiclient.http import HttpMock |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 37 | from apiclient.http import HttpMockSequence |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 38 | from apiclient.http import HttpRequest |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 39 | from apiclient.http import MediaFileUpload |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 40 | from apiclient.http import MediaUpload |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 41 | from apiclient.http import MediaInMemoryUpload |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 42 | from apiclient.http import MediaIoBaseUpload |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 43 | from apiclient.http import MediaIoBaseDownload |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 44 | from apiclient.http import set_user_agent |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 45 | from apiclient.http import MAX_URI_LENGTH |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 46 | from apiclient.model import JsonModel |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 47 | from oauth2client.client import Credentials |
| 48 | |
| 49 | |
| 50 | class MockCredentials(Credentials): |
| 51 | """Mock class for all Credentials objects.""" |
| 52 | def __init__(self, bearer_token): |
| 53 | super(MockCredentials, self).__init__() |
| 54 | self._authorized = 0 |
| 55 | self._refreshed = 0 |
| 56 | self._applied = 0 |
| 57 | self._bearer_token = bearer_token |
| 58 | |
| 59 | def authorize(self, http): |
| 60 | self._authorized += 1 |
| 61 | |
| 62 | request_orig = http.request |
| 63 | |
| 64 | # The closure that will replace 'httplib2.Http.request'. |
| 65 | def new_request(uri, method='GET', body=None, headers=None, |
| 66 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 67 | connection_type=None): |
| 68 | # Modify the request headers to add the appropriate |
| 69 | # Authorization header. |
| 70 | if headers is None: |
| 71 | headers = {} |
| 72 | self.apply(headers) |
| 73 | |
| 74 | resp, content = request_orig(uri, method, body, headers, |
| 75 | redirections, connection_type) |
| 76 | |
| 77 | return resp, content |
| 78 | |
| 79 | # Replace the request method with our own closure. |
| 80 | http.request = new_request |
| 81 | |
| 82 | # Set credentials as a property of the request method. |
| 83 | setattr(http.request, 'credentials', self) |
| 84 | |
| 85 | return http |
| 86 | |
| 87 | def refresh(self, http): |
| 88 | self._refreshed += 1 |
| 89 | |
| 90 | def apply(self, headers): |
| 91 | self._applied += 1 |
| 92 | headers['authorization'] = self._bearer_token + ' ' + str(self._refreshed) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 93 | |
| 94 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 95 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 96 | |
| 97 | |
| 98 | def datafile(filename): |
| 99 | return os.path.join(DATA_DIR, filename) |
| 100 | |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 101 | class TestUserAgent(unittest.TestCase): |
| 102 | |
| 103 | def test_set_user_agent(self): |
| 104 | http = HttpMockSequence([ |
| 105 | ({'status': '200'}, 'echo_request_headers'), |
| 106 | ]) |
| 107 | |
| 108 | http = set_user_agent(http, "my_app/5.5") |
| 109 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 110 | self.assertEqual('my_app/5.5', content['user-agent']) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 111 | |
| 112 | def test_set_user_agent_nested(self): |
| 113 | http = HttpMockSequence([ |
| 114 | ({'status': '200'}, 'echo_request_headers'), |
| 115 | ]) |
| 116 | |
| 117 | http = set_user_agent(http, "my_app/5.5") |
| 118 | http = set_user_agent(http, "my_library/0.1") |
| 119 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 120 | self.assertEqual('my_app/5.5 my_library/0.1', content['user-agent']) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 121 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 122 | |
| 123 | class TestMediaUpload(unittest.TestCase): |
| 124 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 125 | def test_media_file_upload_to_from_json(self): |
| 126 | upload = MediaFileUpload( |
| 127 | datafile('small.png'), chunksize=500, resumable=True) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 128 | self.assertEqual('image/png', upload.mimetype()) |
| 129 | self.assertEqual(190, upload.size()) |
| 130 | self.assertEqual(True, upload.resumable()) |
| 131 | self.assertEqual(500, upload.chunksize()) |
| 132 | self.assertEqual('PNG', upload.getbytes(1, 3)) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 133 | |
| 134 | json = upload.to_json() |
| 135 | new_upload = MediaUpload.new_from_json(json) |
| 136 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 137 | self.assertEqual('image/png', new_upload.mimetype()) |
| 138 | self.assertEqual(190, new_upload.size()) |
| 139 | self.assertEqual(True, new_upload.resumable()) |
| 140 | self.assertEqual(500, new_upload.chunksize()) |
| 141 | self.assertEqual('PNG', new_upload.getbytes(1, 3)) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 142 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 143 | def test_media_file_upload_raises_on_invalid_chunksize(self): |
| 144 | self.assertRaises(InvalidChunkSizeError, MediaFileUpload, |
| 145 | datafile('small.png'), mimetype='image/png', chunksize=-2, |
| 146 | resumable=True) |
| 147 | |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 148 | def test_media_inmemory_upload(self): |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 149 | media = MediaInMemoryUpload('abcdef', mimetype='text/plain', chunksize=10, |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 150 | resumable=True) |
| 151 | self.assertEqual('text/plain', media.mimetype()) |
| 152 | self.assertEqual(10, media.chunksize()) |
| 153 | self.assertTrue(media.resumable()) |
| 154 | self.assertEqual('bc', media.getbytes(1, 2)) |
| 155 | self.assertEqual(6, media.size()) |
| 156 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 157 | def test_http_request_to_from_json(self): |
| 158 | |
| 159 | def _postproc(*kwargs): |
| 160 | pass |
| 161 | |
| 162 | http = httplib2.Http() |
| 163 | media_upload = MediaFileUpload( |
| 164 | datafile('small.png'), chunksize=500, resumable=True) |
| 165 | req = HttpRequest( |
| 166 | http, |
| 167 | _postproc, |
| 168 | 'http://example.com', |
| 169 | method='POST', |
| 170 | body='{}', |
| 171 | headers={'content-type': 'multipart/related; boundary="---flubber"'}, |
| 172 | methodId='foo', |
| 173 | resumable=media_upload) |
| 174 | |
| 175 | json = req.to_json() |
| 176 | new_req = HttpRequest.from_json(json, http, _postproc) |
| 177 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 178 | self.assertEqual({'content-type': |
| 179 | 'multipart/related; boundary="---flubber"'}, |
| 180 | new_req.headers) |
| 181 | self.assertEqual('http://example.com', new_req.uri) |
| 182 | self.assertEqual('{}', new_req.body) |
| 183 | self.assertEqual(http, new_req.http) |
| 184 | self.assertEqual(media_upload.to_json(), new_req.resumable.to_json()) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 185 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 186 | |
| 187 | class TestMediaIoBaseUpload(unittest.TestCase): |
| 188 | |
| 189 | def test_media_io_base_upload_from_file_io(self): |
| 190 | try: |
| 191 | import io |
| 192 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 193 | fd = io.FileIO(datafile('small.png'), 'r') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 194 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 195 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 196 | self.assertEqual('image/png', upload.mimetype()) |
| 197 | self.assertEqual(190, upload.size()) |
| 198 | self.assertEqual(True, upload.resumable()) |
| 199 | self.assertEqual(500, upload.chunksize()) |
| 200 | self.assertEqual('PNG', upload.getbytes(1, 3)) |
| 201 | except ImportError: |
| 202 | pass |
| 203 | |
| 204 | def test_media_io_base_upload_from_file_object(self): |
| 205 | f = open(datafile('small.png'), 'r') |
| 206 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 207 | fd=f, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 208 | self.assertEqual('image/png', upload.mimetype()) |
| 209 | self.assertEqual(190, upload.size()) |
| 210 | self.assertEqual(True, upload.resumable()) |
| 211 | self.assertEqual(500, upload.chunksize()) |
| 212 | self.assertEqual('PNG', upload.getbytes(1, 3)) |
| 213 | f.close() |
| 214 | |
| 215 | def test_media_io_base_upload_serializable(self): |
| 216 | f = open(datafile('small.png'), 'r') |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 217 | upload = MediaIoBaseUpload(fd=f, mimetype='image/png') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 218 | |
| 219 | try: |
| 220 | json = upload.to_json() |
| 221 | self.fail('MediaIoBaseUpload should not be serializable.') |
| 222 | except NotImplementedError: |
| 223 | pass |
| 224 | |
| 225 | def test_media_io_base_upload_from_string_io(self): |
| 226 | f = open(datafile('small.png'), 'r') |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 227 | fd = StringIO.StringIO(f.read()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 228 | f.close() |
| 229 | |
| 230 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 231 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 232 | self.assertEqual('image/png', upload.mimetype()) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 233 | self.assertEqual(190, upload.size()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 234 | self.assertEqual(True, upload.resumable()) |
| 235 | self.assertEqual(500, upload.chunksize()) |
| 236 | self.assertEqual('PNG', upload.getbytes(1, 3)) |
| 237 | f.close() |
| 238 | |
| 239 | def test_media_io_base_upload_from_bytes(self): |
| 240 | try: |
| 241 | import io |
| 242 | |
| 243 | f = open(datafile('small.png'), 'r') |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 244 | fd = io.BytesIO(f.read()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 245 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 246 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 247 | self.assertEqual('image/png', upload.mimetype()) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 248 | self.assertEqual(190, upload.size()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 249 | self.assertEqual(True, upload.resumable()) |
| 250 | self.assertEqual(500, upload.chunksize()) |
| 251 | self.assertEqual('PNG', upload.getbytes(1, 3)) |
| 252 | except ImportError: |
| 253 | pass |
| 254 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 255 | def test_media_io_base_upload_raises_on_invalid_chunksize(self): |
| 256 | try: |
| 257 | import io |
| 258 | |
| 259 | f = open(datafile('small.png'), 'r') |
| 260 | fd = io.BytesIO(f.read()) |
| 261 | self.assertRaises(InvalidChunkSizeError, MediaIoBaseUpload, |
| 262 | fd, 'image/png', chunksize=-2, resumable=True) |
| 263 | except ImportError: |
| 264 | pass |
| 265 | |
| 266 | def test_media_io_base_upload_streamable(self): |
| 267 | try: |
| 268 | import io |
| 269 | |
| 270 | fd = io.BytesIO('stuff') |
| 271 | upload = MediaIoBaseUpload( |
| 272 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 273 | self.assertEqual(True, upload.has_stream()) |
| 274 | self.assertEqual(fd, upload.stream()) |
| 275 | except ImportError: |
| 276 | pass |
| 277 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 278 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 279 | class TestMediaIoBaseDownload(unittest.TestCase): |
| 280 | |
| 281 | def setUp(self): |
| 282 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 283 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 284 | self.request = zoo.animals().get_media(name='Lion') |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 285 | self.fd = StringIO.StringIO() |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 286 | |
| 287 | def test_media_io_base_download(self): |
| 288 | self.request.http = HttpMockSequence([ |
| 289 | ({'status': '200', |
| 290 | 'content-range': '0-2/5'}, '123'), |
| 291 | ({'status': '200', |
| 292 | 'content-range': '3-4/5'}, '45'), |
| 293 | ]) |
| 294 | |
| 295 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 296 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 297 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 298 | self.assertEqual(self.fd, download._fd) |
| 299 | self.assertEqual(3, download._chunksize) |
| 300 | self.assertEqual(0, download._progress) |
| 301 | self.assertEqual(None, download._total_size) |
| 302 | self.assertEqual(False, download._done) |
| 303 | self.assertEqual(self.request.uri, download._uri) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 304 | |
| 305 | status, done = download.next_chunk() |
| 306 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 307 | self.assertEqual(self.fd.getvalue(), '123') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 308 | self.assertEqual(False, done) |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 309 | self.assertEqual(3, download._progress) |
| 310 | self.assertEqual(5, download._total_size) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 311 | self.assertEqual(3, status.resumable_progress) |
| 312 | |
| 313 | status, done = download.next_chunk() |
| 314 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 315 | self.assertEqual(self.fd.getvalue(), '12345') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 316 | self.assertEqual(True, done) |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 317 | self.assertEqual(5, download._progress) |
| 318 | self.assertEqual(5, download._total_size) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 319 | |
| 320 | def test_media_io_base_download_handle_redirects(self): |
| 321 | self.request.http = HttpMockSequence([ |
| 322 | ({'status': '307', |
| 323 | 'location': 'https://secure.example.net/lion'}, ''), |
| 324 | ({'status': '200', |
| 325 | 'content-range': '0-2/5'}, 'abc'), |
| 326 | ]) |
| 327 | |
| 328 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 329 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 330 | |
| 331 | status, done = download.next_chunk() |
| 332 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 333 | self.assertEqual('https://secure.example.net/lion', download._uri) |
| 334 | self.assertEqual(self.fd.getvalue(), 'abc') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 335 | self.assertEqual(False, done) |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 336 | self.assertEqual(3, download._progress) |
| 337 | self.assertEqual(5, download._total_size) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 338 | |
| 339 | def test_media_io_base_download_handle_4xx(self): |
| 340 | self.request.http = HttpMockSequence([ |
| 341 | ({'status': '400'}, ''), |
| 342 | ]) |
| 343 | |
| 344 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 345 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 346 | |
| 347 | try: |
| 348 | status, done = download.next_chunk() |
| 349 | self.fail('Should raise an exception') |
| 350 | except HttpError: |
| 351 | pass |
| 352 | |
| 353 | # Even after raising an exception we can pick up where we left off. |
| 354 | self.request.http = HttpMockSequence([ |
| 355 | ({'status': '200', |
| 356 | 'content-range': '0-2/5'}, '123'), |
| 357 | ]) |
| 358 | |
| 359 | status, done = download.next_chunk() |
| 360 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 361 | self.assertEqual(self.fd.getvalue(), '123') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 362 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 363 | EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1 |
| 364 | Content-Type: application/json |
| 365 | MIME-Version: 1.0 |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 366 | Host: www.googleapis.com |
| 367 | content-length: 2\r\n\r\n{}""" |
| 368 | |
| 369 | |
| 370 | NO_BODY_EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1 |
| 371 | Content-Type: application/json |
| 372 | MIME-Version: 1.0 |
| 373 | Host: www.googleapis.com |
| 374 | content-length: 0\r\n\r\n""" |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 375 | |
| 376 | |
| 377 | RESPONSE = """HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 378 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 379 | Content-Length: 14 |
| 380 | ETag: "etag/pony"\r\n\r\n{"answer": 42}""" |
| 381 | |
| 382 | |
| 383 | BATCH_RESPONSE = """--batch_foobarbaz |
| 384 | Content-Type: application/http |
| 385 | Content-Transfer-Encoding: binary |
| 386 | Content-ID: <randomness+1> |
| 387 | |
| 388 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 389 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 390 | Content-Length: 14 |
| 391 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 392 | |
| 393 | --batch_foobarbaz |
| 394 | Content-Type: application/http |
| 395 | Content-Transfer-Encoding: binary |
| 396 | Content-ID: <randomness+2> |
| 397 | |
| 398 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 399 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 400 | Content-Length: 14 |
| 401 | ETag: "etag/sheep"\r\n\r\n{"baz": "qux"} |
| 402 | --batch_foobarbaz--""" |
| 403 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 404 | |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 405 | BATCH_ERROR_RESPONSE = """--batch_foobarbaz |
| 406 | Content-Type: application/http |
| 407 | Content-Transfer-Encoding: binary |
| 408 | Content-ID: <randomness+1> |
| 409 | |
| 410 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 411 | Content-Type: application/json |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 412 | Content-Length: 14 |
| 413 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 414 | |
| 415 | --batch_foobarbaz |
| 416 | Content-Type: application/http |
| 417 | Content-Transfer-Encoding: binary |
| 418 | Content-ID: <randomness+2> |
| 419 | |
| 420 | HTTP/1.1 403 Access Not Configured |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 421 | Content-Type: application/json |
| 422 | Content-Length: 245 |
| 423 | ETag: "etag/sheep"\r\n\r\n{ |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 424 | "error": { |
| 425 | "errors": [ |
| 426 | { |
| 427 | "domain": "usageLimits", |
| 428 | "reason": "accessNotConfigured", |
| 429 | "message": "Access Not Configured", |
| 430 | "debugInfo": "QuotaState: BLOCKED" |
| 431 | } |
| 432 | ], |
| 433 | "code": 403, |
| 434 | "message": "Access Not Configured" |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | --batch_foobarbaz--""" |
| 439 | |
| 440 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 441 | BATCH_RESPONSE_WITH_401 = """--batch_foobarbaz |
| 442 | Content-Type: application/http |
| 443 | Content-Transfer-Encoding: binary |
| 444 | Content-ID: <randomness+1> |
| 445 | |
Joe Gregorio | c752e33 | 2012-07-11 14:43:52 -0400 | [diff] [blame] | 446 | HTTP/1.1 401 Authorization Required |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 447 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 448 | Content-Length: 14 |
| 449 | ETag: "etag/pony"\r\n\r\n{"error": {"message": |
| 450 | "Authorizaton failed."}} |
| 451 | |
| 452 | --batch_foobarbaz |
| 453 | Content-Type: application/http |
| 454 | Content-Transfer-Encoding: binary |
| 455 | Content-ID: <randomness+2> |
| 456 | |
| 457 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 458 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 459 | Content-Length: 14 |
| 460 | ETag: "etag/sheep"\r\n\r\n{"baz": "qux"} |
| 461 | --batch_foobarbaz--""" |
| 462 | |
| 463 | |
| 464 | BATCH_SINGLE_RESPONSE = """--batch_foobarbaz |
| 465 | Content-Type: application/http |
| 466 | Content-Transfer-Encoding: binary |
| 467 | Content-ID: <randomness+1> |
| 468 | |
| 469 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 470 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 471 | Content-Length: 14 |
| 472 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 473 | --batch_foobarbaz--""" |
| 474 | |
| 475 | class Callbacks(object): |
| 476 | def __init__(self): |
| 477 | self.responses = {} |
| 478 | self.exceptions = {} |
| 479 | |
| 480 | def f(self, request_id, response, exception): |
| 481 | self.responses[request_id] = response |
| 482 | self.exceptions[request_id] = exception |
| 483 | |
| 484 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 485 | class TestBatch(unittest.TestCase): |
| 486 | |
| 487 | def setUp(self): |
| 488 | model = JsonModel() |
| 489 | self.request1 = HttpRequest( |
| 490 | None, |
| 491 | model.response, |
| 492 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 493 | method='POST', |
| 494 | body='{}', |
| 495 | headers={'content-type': 'application/json'}) |
| 496 | |
| 497 | self.request2 = HttpRequest( |
| 498 | None, |
| 499 | model.response, |
| 500 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 501 | method='GET', |
| 502 | body='', |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 503 | headers={'content-type': 'application/json'}) |
| 504 | |
| 505 | |
| 506 | def test_id_to_from_content_id_header(self): |
| 507 | batch = BatchHttpRequest() |
| 508 | self.assertEquals('12', batch._header_to_id(batch._id_to_header('12'))) |
| 509 | |
| 510 | def test_invalid_content_id_header(self): |
| 511 | batch = BatchHttpRequest() |
| 512 | self.assertRaises(BatchError, batch._header_to_id, '[foo+x]') |
| 513 | self.assertRaises(BatchError, batch._header_to_id, 'foo+1') |
| 514 | self.assertRaises(BatchError, batch._header_to_id, '<foo>') |
| 515 | |
| 516 | def test_serialize_request(self): |
| 517 | batch = BatchHttpRequest() |
| 518 | request = HttpRequest( |
| 519 | None, |
| 520 | None, |
| 521 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 522 | method='POST', |
| 523 | body='{}', |
| 524 | headers={'content-type': 'application/json'}, |
| 525 | methodId=None, |
| 526 | resumable=None) |
| 527 | s = batch._serialize_request(request).splitlines() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 528 | self.assertEqual(EXPECTED.splitlines(), s) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 529 | |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 530 | def test_serialize_request_media_body(self): |
| 531 | batch = BatchHttpRequest() |
| 532 | f = open(datafile('small.png')) |
| 533 | body = f.read() |
| 534 | f.close() |
| 535 | |
| 536 | request = HttpRequest( |
| 537 | None, |
| 538 | None, |
| 539 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 540 | method='POST', |
| 541 | body=body, |
| 542 | headers={'content-type': 'application/json'}, |
| 543 | methodId=None, |
| 544 | resumable=None) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 545 | # Just testing it shouldn't raise an exception. |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 546 | s = batch._serialize_request(request).splitlines() |
| 547 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 548 | def test_serialize_request_no_body(self): |
| 549 | batch = BatchHttpRequest() |
| 550 | request = HttpRequest( |
| 551 | None, |
| 552 | None, |
| 553 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 554 | method='POST', |
| 555 | body='', |
| 556 | headers={'content-type': 'application/json'}, |
| 557 | methodId=None, |
| 558 | resumable=None) |
| 559 | s = batch._serialize_request(request).splitlines() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 560 | self.assertEqual(NO_BODY_EXPECTED.splitlines(), s) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 561 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 562 | def test_deserialize_response(self): |
| 563 | batch = BatchHttpRequest() |
| 564 | resp, content = batch._deserialize_response(RESPONSE) |
| 565 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 566 | self.assertEqual(200, resp.status) |
| 567 | self.assertEqual('OK', resp.reason) |
| 568 | self.assertEqual(11, resp.version) |
| 569 | self.assertEqual('{"answer": 42}', content) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 570 | |
| 571 | def test_new_id(self): |
| 572 | batch = BatchHttpRequest() |
| 573 | |
| 574 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 575 | self.assertEqual('1', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 576 | |
| 577 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 578 | self.assertEqual('2', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 579 | |
| 580 | batch.add(self.request1, request_id='3') |
| 581 | |
| 582 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 583 | self.assertEqual('4', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 584 | |
| 585 | def test_add(self): |
| 586 | batch = BatchHttpRequest() |
| 587 | batch.add(self.request1, request_id='1') |
| 588 | self.assertRaises(KeyError, batch.add, self.request1, request_id='1') |
| 589 | |
| 590 | def test_add_fail_for_resumable(self): |
| 591 | batch = BatchHttpRequest() |
| 592 | |
| 593 | upload = MediaFileUpload( |
| 594 | datafile('small.png'), chunksize=500, resumable=True) |
| 595 | self.request1.resumable = upload |
| 596 | self.assertRaises(BatchError, batch.add, self.request1, request_id='1') |
| 597 | |
| 598 | def test_execute(self): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 599 | batch = BatchHttpRequest() |
| 600 | callbacks = Callbacks() |
| 601 | |
| 602 | batch.add(self.request1, callback=callbacks.f) |
| 603 | batch.add(self.request2, callback=callbacks.f) |
| 604 | http = HttpMockSequence([ |
| 605 | ({'status': '200', |
| 606 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 607 | BATCH_RESPONSE), |
| 608 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 609 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 610 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 611 | self.assertEqual(None, callbacks.exceptions['1']) |
| 612 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
| 613 | self.assertEqual(None, callbacks.exceptions['2']) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 614 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 615 | def test_execute_request_body(self): |
| 616 | batch = BatchHttpRequest() |
| 617 | |
| 618 | batch.add(self.request1) |
| 619 | batch.add(self.request2) |
| 620 | http = HttpMockSequence([ |
| 621 | ({'status': '200', |
| 622 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 623 | 'echo_request_body'), |
| 624 | ]) |
| 625 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 626 | batch.execute(http=http) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 627 | self.fail('Should raise exception') |
| 628 | except BatchError, e: |
| 629 | boundary, _ = e.content.split(None, 1) |
| 630 | self.assertEqual('--', boundary[:2]) |
| 631 | parts = e.content.split(boundary) |
| 632 | self.assertEqual(4, len(parts)) |
| 633 | self.assertEqual('', parts[0]) |
| 634 | self.assertEqual('--', parts[3]) |
| 635 | header = parts[1].splitlines()[1] |
| 636 | self.assertEqual('Content-Type: application/http', header) |
| 637 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 638 | def test_execute_refresh_and_retry_on_401(self): |
| 639 | batch = BatchHttpRequest() |
| 640 | callbacks = Callbacks() |
| 641 | cred_1 = MockCredentials('Foo') |
| 642 | cred_2 = MockCredentials('Bar') |
| 643 | |
| 644 | http = HttpMockSequence([ |
| 645 | ({'status': '200', |
| 646 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 647 | BATCH_RESPONSE_WITH_401), |
| 648 | ({'status': '200', |
| 649 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 650 | BATCH_SINGLE_RESPONSE), |
| 651 | ]) |
| 652 | |
| 653 | creds_http_1 = HttpMockSequence([]) |
| 654 | cred_1.authorize(creds_http_1) |
| 655 | |
| 656 | creds_http_2 = HttpMockSequence([]) |
| 657 | cred_2.authorize(creds_http_2) |
| 658 | |
| 659 | self.request1.http = creds_http_1 |
| 660 | self.request2.http = creds_http_2 |
| 661 | |
| 662 | batch.add(self.request1, callback=callbacks.f) |
| 663 | batch.add(self.request2, callback=callbacks.f) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 664 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 665 | |
| 666 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 667 | self.assertEqual(None, callbacks.exceptions['1']) |
| 668 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
| 669 | self.assertEqual(None, callbacks.exceptions['2']) |
| 670 | |
| 671 | self.assertEqual(1, cred_1._refreshed) |
| 672 | self.assertEqual(0, cred_2._refreshed) |
| 673 | |
| 674 | self.assertEqual(1, cred_1._authorized) |
| 675 | self.assertEqual(1, cred_2._authorized) |
| 676 | |
| 677 | self.assertEqual(1, cred_2._applied) |
| 678 | self.assertEqual(2, cred_1._applied) |
| 679 | |
| 680 | def test_http_errors_passed_to_callback(self): |
| 681 | batch = BatchHttpRequest() |
| 682 | callbacks = Callbacks() |
| 683 | cred_1 = MockCredentials('Foo') |
| 684 | cred_2 = MockCredentials('Bar') |
| 685 | |
| 686 | http = HttpMockSequence([ |
| 687 | ({'status': '200', |
| 688 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 689 | BATCH_RESPONSE_WITH_401), |
| 690 | ({'status': '200', |
| 691 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 692 | BATCH_RESPONSE_WITH_401), |
| 693 | ]) |
| 694 | |
| 695 | creds_http_1 = HttpMockSequence([]) |
| 696 | cred_1.authorize(creds_http_1) |
| 697 | |
| 698 | creds_http_2 = HttpMockSequence([]) |
| 699 | cred_2.authorize(creds_http_2) |
| 700 | |
| 701 | self.request1.http = creds_http_1 |
| 702 | self.request2.http = creds_http_2 |
| 703 | |
| 704 | batch.add(self.request1, callback=callbacks.f) |
| 705 | batch.add(self.request2, callback=callbacks.f) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 706 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 707 | |
| 708 | self.assertEqual(None, callbacks.responses['1']) |
| 709 | self.assertEqual(401, callbacks.exceptions['1'].resp.status) |
Joe Gregorio | c752e33 | 2012-07-11 14:43:52 -0400 | [diff] [blame] | 710 | self.assertEqual( |
| 711 | 'Authorization Required', callbacks.exceptions['1'].resp.reason) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 712 | self.assertEqual({u'baz': u'qux'}, callbacks.responses['2']) |
| 713 | self.assertEqual(None, callbacks.exceptions['2']) |
| 714 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 715 | def test_execute_global_callback(self): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 716 | callbacks = Callbacks() |
| 717 | batch = BatchHttpRequest(callback=callbacks.f) |
| 718 | |
| 719 | batch.add(self.request1) |
| 720 | batch.add(self.request2) |
| 721 | http = HttpMockSequence([ |
| 722 | ({'status': '200', |
| 723 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 724 | BATCH_RESPONSE), |
| 725 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 726 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 727 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 728 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 729 | |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 730 | def test_execute_batch_http_error(self): |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 731 | callbacks = Callbacks() |
| 732 | batch = BatchHttpRequest(callback=callbacks.f) |
| 733 | |
| 734 | batch.add(self.request1) |
| 735 | batch.add(self.request2) |
| 736 | http = HttpMockSequence([ |
| 737 | ({'status': '200', |
| 738 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 739 | BATCH_ERROR_RESPONSE), |
| 740 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 741 | batch.execute(http=http) |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 742 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 743 | expected = ('<HttpError 403 when requesting ' |
| 744 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar returned ' |
| 745 | '"Access Not Configured">') |
| 746 | self.assertEqual(expected, str(callbacks.exceptions['2'])) |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 747 | |
Joe Gregorio | ba5c790 | 2012-08-03 12:48:16 -0400 | [diff] [blame] | 748 | class TestRequestUriTooLong(unittest.TestCase): |
| 749 | |
| 750 | def test_turn_get_into_post(self): |
| 751 | |
| 752 | def _postproc(resp, content): |
| 753 | return content |
| 754 | |
| 755 | http = HttpMockSequence([ |
| 756 | ({'status': '200'}, |
| 757 | 'echo_request_body'), |
| 758 | ({'status': '200'}, |
| 759 | 'echo_request_headers'), |
| 760 | ]) |
| 761 | |
| 762 | # Send a long query parameter. |
| 763 | query = { |
| 764 | 'q': 'a' * MAX_URI_LENGTH + '?&' |
| 765 | } |
| 766 | req = HttpRequest( |
| 767 | http, |
| 768 | _postproc, |
| 769 | 'http://example.com?' + urllib.urlencode(query), |
| 770 | method='GET', |
| 771 | body=None, |
| 772 | headers={}, |
| 773 | methodId='foo', |
| 774 | resumable=None) |
| 775 | |
| 776 | # Query parameters should be sent in the body. |
| 777 | response = req.execute() |
| 778 | self.assertEqual('q=' + 'a' * MAX_URI_LENGTH + '%3F%26', response) |
| 779 | |
| 780 | # Extra headers should be set. |
| 781 | response = req.execute() |
| 782 | self.assertEqual('GET', response['x-http-method-override']) |
| 783 | self.assertEqual(str(MAX_URI_LENGTH + 8), response['content-length']) |
| 784 | self.assertEqual( |
| 785 | 'application/x-www-form-urlencoded', response['content-type']) |
| 786 | |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 787 | if __name__ == '__main__': |
| 788 | unittest.main() |