Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 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 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 19 | Unit tests for the googleapiclient.http. |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 20 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 21 | from __future__ import absolute_import |
| 22 | from six.moves import range |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 23 | |
| 24 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 25 | |
Pat Ferate | c605087 | 2015-03-03 18:24:59 -0800 | [diff] [blame] | 26 | from six import PY3 |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 27 | from six import BytesIO, StringIO |
| 28 | from io import FileIO |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 29 | from six.moves.urllib.parse import urlencode |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 30 | |
Joe Gregorio | 7cbceab | 2011-06-27 10:46:54 -0400 | [diff] [blame] | 31 | # Do not remove the httplib2 import |
| 32 | import httplib2 |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 33 | import logging |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 34 | import mock |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 35 | import os |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 36 | import unittest2 as unittest |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 37 | import random |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 38 | import socket |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 39 | import ssl |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 40 | import time |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 41 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 42 | from googleapiclient.discovery import build |
| 43 | from googleapiclient.errors import BatchError |
| 44 | from googleapiclient.errors import HttpError |
| 45 | from googleapiclient.errors import InvalidChunkSizeError |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 46 | from googleapiclient.http import build_http |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 47 | from googleapiclient.http import BatchHttpRequest |
| 48 | from googleapiclient.http import HttpMock |
| 49 | from googleapiclient.http import HttpMockSequence |
| 50 | from googleapiclient.http import HttpRequest |
| 51 | from googleapiclient.http import MAX_URI_LENGTH |
| 52 | from googleapiclient.http import MediaFileUpload |
| 53 | from googleapiclient.http import MediaInMemoryUpload |
| 54 | from googleapiclient.http import MediaIoBaseDownload |
| 55 | from googleapiclient.http import MediaIoBaseUpload |
| 56 | from googleapiclient.http import MediaUpload |
| 57 | from googleapiclient.http import _StreamSlice |
| 58 | from googleapiclient.http import set_user_agent |
| 59 | from googleapiclient.model import JsonModel |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 60 | from oauth2client.client import Credentials |
| 61 | |
| 62 | |
| 63 | class MockCredentials(Credentials): |
| 64 | """Mock class for all Credentials objects.""" |
Jon Wayne Parrott | d3a5cf4 | 2017-06-19 17:55:04 -0700 | [diff] [blame] | 65 | def __init__(self, bearer_token, expired=False): |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 66 | super(MockCredentials, self).__init__() |
| 67 | self._authorized = 0 |
| 68 | self._refreshed = 0 |
| 69 | self._applied = 0 |
| 70 | self._bearer_token = bearer_token |
Jon Wayne Parrott | d3a5cf4 | 2017-06-19 17:55:04 -0700 | [diff] [blame] | 71 | self._access_token_expired = expired |
| 72 | |
| 73 | @property |
Jon Wayne Parrott | 20e6135 | 2018-01-18 09:16:37 -0800 | [diff] [blame] | 74 | def access_token(self): |
| 75 | return self._bearer_token |
| 76 | |
| 77 | @property |
Jon Wayne Parrott | d3a5cf4 | 2017-06-19 17:55:04 -0700 | [diff] [blame] | 78 | def access_token_expired(self): |
| 79 | return self._access_token_expired |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 80 | |
| 81 | def authorize(self, http): |
| 82 | self._authorized += 1 |
| 83 | |
| 84 | request_orig = http.request |
| 85 | |
| 86 | # The closure that will replace 'httplib2.Http.request'. |
| 87 | def new_request(uri, method='GET', body=None, headers=None, |
| 88 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 89 | connection_type=None): |
| 90 | # Modify the request headers to add the appropriate |
| 91 | # Authorization header. |
| 92 | if headers is None: |
| 93 | headers = {} |
| 94 | self.apply(headers) |
| 95 | |
| 96 | resp, content = request_orig(uri, method, body, headers, |
| 97 | redirections, connection_type) |
| 98 | |
| 99 | return resp, content |
| 100 | |
| 101 | # Replace the request method with our own closure. |
| 102 | http.request = new_request |
| 103 | |
| 104 | # Set credentials as a property of the request method. |
| 105 | setattr(http.request, 'credentials', self) |
| 106 | |
| 107 | return http |
| 108 | |
| 109 | def refresh(self, http): |
| 110 | self._refreshed += 1 |
| 111 | |
| 112 | def apply(self, headers): |
| 113 | self._applied += 1 |
| 114 | headers['authorization'] = self._bearer_token + ' ' + str(self._refreshed) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 115 | |
| 116 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 117 | class HttpMockWithErrors(object): |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 118 | def __init__(self, num_errors, success_json, success_data): |
| 119 | self.num_errors = num_errors |
| 120 | self.success_json = success_json |
| 121 | self.success_data = success_data |
| 122 | |
| 123 | def request(self, *args, **kwargs): |
| 124 | if not self.num_errors: |
| 125 | return httplib2.Response(self.success_json), self.success_data |
| 126 | else: |
| 127 | self.num_errors -= 1 |
Alexander Mohr | fff3ae5 | 2018-04-27 13:39:53 -0700 | [diff] [blame] | 128 | if self.num_errors == 1: # initial == 2 |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 129 | raise ssl.SSLError() |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 130 | if self.num_errors == 3: # initial == 4 |
| 131 | raise httplib2.ServerNotFoundError() |
| 132 | else: # initial != 2,4 |
Thomas Bonfort | 88ab76b | 2016-04-19 08:48:53 +0200 | [diff] [blame] | 133 | if self.num_errors == 2: |
Alexander Mohr | fff3ae5 | 2018-04-27 13:39:53 -0700 | [diff] [blame] | 134 | # first try a broken pipe error (#218) |
| 135 | ex = socket.error() |
Thomas Bonfort | 88ab76b | 2016-04-19 08:48:53 +0200 | [diff] [blame] | 136 | ex.errno = socket.errno.EPIPE |
| 137 | else: |
| 138 | # Initialize the timeout error code to the platform's error code. |
| 139 | try: |
| 140 | # For Windows: |
Alexander Mohr | fff3ae5 | 2018-04-27 13:39:53 -0700 | [diff] [blame] | 141 | ex = socket.error() |
Thomas Bonfort | 88ab76b | 2016-04-19 08:48:53 +0200 | [diff] [blame] | 142 | ex.errno = socket.errno.WSAETIMEDOUT |
| 143 | except AttributeError: |
| 144 | # For Linux/Mac: |
Alexander Mohr | fff3ae5 | 2018-04-27 13:39:53 -0700 | [diff] [blame] | 145 | if PY3: |
| 146 | ex = socket.timeout() |
| 147 | else: |
| 148 | ex = socket.error() |
| 149 | ex.errno = socket.errno.ETIMEDOUT |
Thomas Bonfort | 88ab76b | 2016-04-19 08:48:53 +0200 | [diff] [blame] | 150 | # Now raise the correct error. |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 151 | raise ex |
| 152 | |
| 153 | |
| 154 | class HttpMockWithNonRetriableErrors(object): |
| 155 | def __init__(self, num_errors, success_json, success_data): |
| 156 | self.num_errors = num_errors |
| 157 | self.success_json = success_json |
| 158 | self.success_data = success_data |
| 159 | |
| 160 | def request(self, *args, **kwargs): |
| 161 | if not self.num_errors: |
| 162 | return httplib2.Response(self.success_json), self.success_data |
| 163 | else: |
| 164 | self.num_errors -= 1 |
| 165 | ex = socket.error() |
Thomas Bonfort | 88ab76b | 2016-04-19 08:48:53 +0200 | [diff] [blame] | 166 | # set errno to a non-retriable value |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 167 | try: |
| 168 | # For Windows: |
| 169 | ex.errno = socket.errno.WSAECONNREFUSED |
| 170 | except AttributeError: |
| 171 | # For Linux/Mac: |
| 172 | ex.errno = socket.errno.ECONNREFUSED |
| 173 | # Now raise the correct timeout error. |
| 174 | raise ex |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 175 | |
| 176 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 177 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 178 | |
| 179 | |
| 180 | def datafile(filename): |
| 181 | return os.path.join(DATA_DIR, filename) |
| 182 | |
Ondrej Medek | b86bfc9 | 2018-01-19 18:53:50 +0100 | [diff] [blame] | 183 | def _postproc_none(*kwargs): |
| 184 | pass |
| 185 | |
| 186 | |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 187 | class TestUserAgent(unittest.TestCase): |
| 188 | |
| 189 | def test_set_user_agent(self): |
| 190 | http = HttpMockSequence([ |
| 191 | ({'status': '200'}, 'echo_request_headers'), |
| 192 | ]) |
| 193 | |
| 194 | http = set_user_agent(http, "my_app/5.5") |
| 195 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 196 | self.assertEqual('my_app/5.5', content['user-agent']) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 197 | |
| 198 | def test_set_user_agent_nested(self): |
| 199 | http = HttpMockSequence([ |
| 200 | ({'status': '200'}, 'echo_request_headers'), |
| 201 | ]) |
| 202 | |
| 203 | http = set_user_agent(http, "my_app/5.5") |
| 204 | http = set_user_agent(http, "my_library/0.1") |
| 205 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 206 | 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] | 207 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 208 | |
| 209 | class TestMediaUpload(unittest.TestCase): |
| 210 | |
Nam T. Nguyen | dc13631 | 2015-12-01 10:18:56 -0800 | [diff] [blame] | 211 | def test_media_file_upload_mimetype_detection(self): |
| 212 | upload = MediaFileUpload(datafile('small.png')) |
| 213 | self.assertEqual('image/png', upload.mimetype()) |
| 214 | |
| 215 | upload = MediaFileUpload(datafile('empty')) |
| 216 | self.assertEqual('application/octet-stream', upload.mimetype()) |
| 217 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 218 | def test_media_file_upload_to_from_json(self): |
| 219 | upload = MediaFileUpload( |
| 220 | datafile('small.png'), chunksize=500, resumable=True) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 221 | self.assertEqual('image/png', upload.mimetype()) |
| 222 | self.assertEqual(190, upload.size()) |
| 223 | self.assertEqual(True, upload.resumable()) |
| 224 | self.assertEqual(500, upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 225 | self.assertEqual(b'PNG', upload.getbytes(1, 3)) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 226 | |
| 227 | json = upload.to_json() |
| 228 | new_upload = MediaUpload.new_from_json(json) |
| 229 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 230 | self.assertEqual('image/png', new_upload.mimetype()) |
| 231 | self.assertEqual(190, new_upload.size()) |
| 232 | self.assertEqual(True, new_upload.resumable()) |
| 233 | self.assertEqual(500, new_upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 234 | self.assertEqual(b'PNG', new_upload.getbytes(1, 3)) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 235 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 236 | def test_media_file_upload_raises_on_invalid_chunksize(self): |
| 237 | self.assertRaises(InvalidChunkSizeError, MediaFileUpload, |
| 238 | datafile('small.png'), mimetype='image/png', chunksize=-2, |
| 239 | resumable=True) |
| 240 | |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 241 | def test_media_inmemory_upload(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 242 | media = MediaInMemoryUpload(b'abcdef', mimetype='text/plain', chunksize=10, |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 243 | resumable=True) |
| 244 | self.assertEqual('text/plain', media.mimetype()) |
| 245 | self.assertEqual(10, media.chunksize()) |
| 246 | self.assertTrue(media.resumable()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 247 | self.assertEqual(b'bc', media.getbytes(1, 2)) |
Ali Afshar | 1cb6b67 | 2012-03-12 08:46:14 -0400 | [diff] [blame] | 248 | self.assertEqual(6, media.size()) |
| 249 | |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 250 | def test_http_request_to_from_json(self): |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 251 | http = build_http() |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 252 | media_upload = MediaFileUpload( |
| 253 | datafile('small.png'), chunksize=500, resumable=True) |
| 254 | req = HttpRequest( |
| 255 | http, |
Ondrej Medek | b86bfc9 | 2018-01-19 18:53:50 +0100 | [diff] [blame] | 256 | _postproc_none, |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 257 | 'http://example.com', |
| 258 | method='POST', |
| 259 | body='{}', |
| 260 | headers={'content-type': 'multipart/related; boundary="---flubber"'}, |
| 261 | methodId='foo', |
| 262 | resumable=media_upload) |
| 263 | |
| 264 | json = req.to_json() |
Ondrej Medek | b86bfc9 | 2018-01-19 18:53:50 +0100 | [diff] [blame] | 265 | new_req = HttpRequest.from_json(json, http, _postproc_none) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 266 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 267 | self.assertEqual({'content-type': |
| 268 | 'multipart/related; boundary="---flubber"'}, |
| 269 | new_req.headers) |
| 270 | self.assertEqual('http://example.com', new_req.uri) |
| 271 | self.assertEqual('{}', new_req.body) |
| 272 | self.assertEqual(http, new_req.http) |
| 273 | self.assertEqual(media_upload.to_json(), new_req.resumable.to_json()) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 274 | |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 275 | self.assertEqual(random.random, new_req._rand) |
| 276 | self.assertEqual(time.sleep, new_req._sleep) |
| 277 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 278 | |
| 279 | class TestMediaIoBaseUpload(unittest.TestCase): |
| 280 | |
| 281 | def test_media_io_base_upload_from_file_io(self): |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 282 | fd = FileIO(datafile('small.png'), 'r') |
| 283 | upload = MediaIoBaseUpload( |
| 284 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 285 | self.assertEqual('image/png', upload.mimetype()) |
| 286 | self.assertEqual(190, upload.size()) |
| 287 | self.assertEqual(True, upload.resumable()) |
| 288 | self.assertEqual(500, upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 289 | self.assertEqual(b'PNG', upload.getbytes(1, 3)) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 290 | |
| 291 | def test_media_io_base_upload_from_file_object(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 292 | f = open(datafile('small.png'), 'rb') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 293 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 294 | fd=f, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 295 | self.assertEqual('image/png', upload.mimetype()) |
| 296 | self.assertEqual(190, upload.size()) |
| 297 | self.assertEqual(True, upload.resumable()) |
| 298 | self.assertEqual(500, upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 299 | self.assertEqual(b'PNG', upload.getbytes(1, 3)) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 300 | f.close() |
| 301 | |
| 302 | def test_media_io_base_upload_serializable(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 303 | f = open(datafile('small.png'), 'rb') |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 304 | upload = MediaIoBaseUpload(fd=f, mimetype='image/png') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 305 | |
| 306 | try: |
| 307 | json = upload.to_json() |
| 308 | self.fail('MediaIoBaseUpload should not be serializable.') |
| 309 | except NotImplementedError: |
| 310 | pass |
| 311 | |
Pat Ferate | c605087 | 2015-03-03 18:24:59 -0800 | [diff] [blame] | 312 | @unittest.skipIf(PY3, 'Strings and Bytes are different types') |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 313 | def test_media_io_base_upload_from_string_io(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 314 | f = open(datafile('small.png'), 'rb') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 315 | fd = StringIO(f.read()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 316 | f.close() |
| 317 | |
| 318 | upload = MediaIoBaseUpload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 319 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 320 | self.assertEqual('image/png', upload.mimetype()) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 321 | self.assertEqual(190, upload.size()) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 322 | self.assertEqual(True, upload.resumable()) |
| 323 | self.assertEqual(500, upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 324 | self.assertEqual(b'PNG', upload.getbytes(1, 3)) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 325 | f.close() |
| 326 | |
| 327 | def test_media_io_base_upload_from_bytes(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 328 | f = open(datafile('small.png'), 'rb') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 329 | fd = BytesIO(f.read()) |
| 330 | upload = MediaIoBaseUpload( |
| 331 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 332 | self.assertEqual('image/png', upload.mimetype()) |
| 333 | self.assertEqual(190, upload.size()) |
| 334 | self.assertEqual(True, upload.resumable()) |
| 335 | self.assertEqual(500, upload.chunksize()) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 336 | self.assertEqual(b'PNG', upload.getbytes(1, 3)) |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 337 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 338 | def test_media_io_base_upload_raises_on_invalid_chunksize(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 339 | f = open(datafile('small.png'), 'rb') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 340 | fd = BytesIO(f.read()) |
| 341 | self.assertRaises(InvalidChunkSizeError, MediaIoBaseUpload, |
| 342 | fd, 'image/png', chunksize=-2, resumable=True) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 343 | |
| 344 | def test_media_io_base_upload_streamable(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 345 | fd = BytesIO(b'stuff') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 346 | upload = MediaIoBaseUpload( |
| 347 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 348 | self.assertEqual(True, upload.has_stream()) |
| 349 | self.assertEqual(fd, upload.stream()) |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 350 | |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 351 | def test_media_io_base_next_chunk_retries(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 352 | f = open(datafile('small.png'), 'rb') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 353 | fd = BytesIO(f.read()) |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 354 | upload = MediaIoBaseUpload( |
| 355 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 356 | |
Zhihao Yuan | cc6d398 | 2016-07-27 11:40:45 -0500 | [diff] [blame] | 357 | # Simulate errors for both the request that creates the resumable upload |
| 358 | # and the upload itself. |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 359 | http = HttpMockSequence([ |
| 360 | ({'status': '500'}, ''), |
| 361 | ({'status': '500'}, ''), |
| 362 | ({'status': '503'}, ''), |
| 363 | ({'status': '200', 'location': 'location'}, ''), |
Zhihao Yuan | cc6d398 | 2016-07-27 11:40:45 -0500 | [diff] [blame] | 364 | ({'status': '403'}, USER_RATE_LIMIT_EXCEEDED_RESPONSE), |
| 365 | ({'status': '403'}, RATE_LIMIT_EXCEEDED_RESPONSE), |
| 366 | ({'status': '429'}, ''), |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 367 | ({'status': '200'}, '{}'), |
| 368 | ]) |
| 369 | |
| 370 | model = JsonModel() |
| 371 | uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar' |
| 372 | method = u'POST' |
| 373 | request = HttpRequest( |
| 374 | http, |
| 375 | model.response, |
| 376 | uri, |
| 377 | method=method, |
| 378 | headers={}, |
| 379 | resumable=upload) |
| 380 | |
| 381 | sleeptimes = [] |
| 382 | request._sleep = lambda x: sleeptimes.append(x) |
| 383 | request._rand = lambda: 10 |
| 384 | |
| 385 | request.execute(num_retries=3) |
| 386 | self.assertEqual([20, 40, 80, 20, 40, 80], sleeptimes) |
| 387 | |
Zhihao Yuan | cc6d398 | 2016-07-27 11:40:45 -0500 | [diff] [blame] | 388 | def test_media_io_base_next_chunk_no_retry_403_not_configured(self): |
| 389 | fd = BytesIO(b"i am png") |
| 390 | upload = MediaIoBaseUpload( |
| 391 | fd=fd, mimetype='image/png', chunksize=500, resumable=True) |
| 392 | |
| 393 | http = HttpMockSequence([ |
| 394 | ({'status': '403'}, NOT_CONFIGURED_RESPONSE), |
| 395 | ({'status': '200'}, '{}') |
| 396 | ]) |
| 397 | |
| 398 | model = JsonModel() |
| 399 | uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar' |
| 400 | method = u'POST' |
| 401 | request = HttpRequest( |
| 402 | http, |
| 403 | model.response, |
| 404 | uri, |
| 405 | method=method, |
| 406 | headers={}, |
| 407 | resumable=upload) |
| 408 | |
| 409 | request._rand = lambda: 1.0 |
| 410 | request._sleep = mock.MagicMock() |
| 411 | |
| 412 | with self.assertRaises(HttpError): |
| 413 | request.execute(num_retries=3) |
| 414 | request._sleep.assert_not_called() |
| 415 | |
Joe Gregorio | 910b9b1 | 2012-06-12 09:36:30 -0400 | [diff] [blame] | 416 | |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 417 | class TestMediaIoBaseDownload(unittest.TestCase): |
| 418 | |
| 419 | def setUp(self): |
| 420 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 421 | zoo = build('zoo', 'v1', http=http) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 422 | self.request = zoo.animals().get_media(name='Lion') |
Pat Ferate | ed9affd | 2015-03-03 16:03:15 -0800 | [diff] [blame] | 423 | self.fd = BytesIO() |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 424 | |
| 425 | def test_media_io_base_download(self): |
| 426 | self.request.http = HttpMockSequence([ |
| 427 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 428 | 'content-range': '0-2/5'}, b'123'), |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 429 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 430 | 'content-range': '3-4/5'}, b'45'), |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 431 | ]) |
Joe Gregorio | 97ef1cc | 2013-06-13 14:47:10 -0400 | [diff] [blame] | 432 | self.assertEqual(True, self.request.http.follow_redirects) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 433 | |
| 434 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 435 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 436 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 437 | self.assertEqual(self.fd, download._fd) |
| 438 | self.assertEqual(3, download._chunksize) |
| 439 | self.assertEqual(0, download._progress) |
| 440 | self.assertEqual(None, download._total_size) |
| 441 | self.assertEqual(False, download._done) |
| 442 | self.assertEqual(self.request.uri, download._uri) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 443 | |
| 444 | status, done = download.next_chunk() |
| 445 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 446 | self.assertEqual(self.fd.getvalue(), b'123') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 447 | self.assertEqual(False, done) |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 448 | self.assertEqual(3, download._progress) |
| 449 | self.assertEqual(5, download._total_size) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 450 | self.assertEqual(3, status.resumable_progress) |
| 451 | |
| 452 | status, done = download.next_chunk() |
| 453 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 454 | self.assertEqual(self.fd.getvalue(), b'12345') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 455 | self.assertEqual(True, done) |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 456 | self.assertEqual(5, download._progress) |
| 457 | self.assertEqual(5, download._total_size) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 458 | |
| 459 | def test_media_io_base_download_handle_redirects(self): |
| 460 | self.request.http = HttpMockSequence([ |
Joe Gregorio | 238feb7 | 2013-06-19 13:15:31 -0400 | [diff] [blame] | 461 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 462 | 'content-location': 'https://secure.example.net/lion'}, b''), |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 463 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 464 | 'content-range': '0-2/5'}, b'abc'), |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 465 | ]) |
| 466 | |
| 467 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 468 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 469 | |
| 470 | status, done = download.next_chunk() |
| 471 | |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 472 | self.assertEqual('https://secure.example.net/lion', download._uri) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 473 | |
| 474 | def test_media_io_base_download_handle_4xx(self): |
| 475 | self.request.http = HttpMockSequence([ |
| 476 | ({'status': '400'}, ''), |
| 477 | ]) |
| 478 | |
| 479 | download = MediaIoBaseDownload( |
Joe Gregorio | 4a2c29f | 2012-07-12 12:52:47 -0400 | [diff] [blame] | 480 | fd=self.fd, request=self.request, chunksize=3) |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 481 | |
| 482 | try: |
| 483 | status, done = download.next_chunk() |
| 484 | self.fail('Should raise an exception') |
| 485 | except HttpError: |
| 486 | pass |
| 487 | |
| 488 | # Even after raising an exception we can pick up where we left off. |
| 489 | self.request.http = HttpMockSequence([ |
| 490 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 491 | 'content-range': '0-2/5'}, b'123'), |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 492 | ]) |
| 493 | |
| 494 | status, done = download.next_chunk() |
| 495 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 496 | self.assertEqual(self.fd.getvalue(), b'123') |
Joe Gregorio | 708388c | 2012-06-15 13:43:04 -0400 | [diff] [blame] | 497 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 498 | def test_media_io_base_download_retries_connection_errors(self): |
| 499 | self.request.http = HttpMockWithErrors( |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 500 | 4, {'status': '200', 'content-range': '0-2/3'}, b'123') |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 501 | |
| 502 | download = MediaIoBaseDownload( |
| 503 | fd=self.fd, request=self.request, chunksize=3) |
| 504 | download._sleep = lambda _x: 0 # do nothing |
| 505 | download._rand = lambda: 10 |
| 506 | |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 507 | status, done = download.next_chunk(num_retries=4) |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 508 | |
| 509 | self.assertEqual(self.fd.getvalue(), b'123') |
| 510 | self.assertEqual(True, done) |
| 511 | |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 512 | def test_media_io_base_download_retries_5xx(self): |
| 513 | self.request.http = HttpMockSequence([ |
| 514 | ({'status': '500'}, ''), |
| 515 | ({'status': '500'}, ''), |
| 516 | ({'status': '500'}, ''), |
| 517 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 518 | 'content-range': '0-2/5'}, b'123'), |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 519 | ({'status': '503'}, ''), |
| 520 | ({'status': '503'}, ''), |
| 521 | ({'status': '503'}, ''), |
| 522 | ({'status': '200', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 523 | 'content-range': '3-4/5'}, b'45'), |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 524 | ]) |
| 525 | |
| 526 | download = MediaIoBaseDownload( |
| 527 | fd=self.fd, request=self.request, chunksize=3) |
| 528 | |
| 529 | self.assertEqual(self.fd, download._fd) |
| 530 | self.assertEqual(3, download._chunksize) |
| 531 | self.assertEqual(0, download._progress) |
| 532 | self.assertEqual(None, download._total_size) |
| 533 | self.assertEqual(False, download._done) |
| 534 | self.assertEqual(self.request.uri, download._uri) |
| 535 | |
| 536 | # Set time.sleep and random.random stubs. |
| 537 | sleeptimes = [] |
| 538 | download._sleep = lambda x: sleeptimes.append(x) |
| 539 | download._rand = lambda: 10 |
| 540 | |
| 541 | status, done = download.next_chunk(num_retries=3) |
| 542 | |
| 543 | # Check for exponential backoff using the rand function above. |
| 544 | self.assertEqual([20, 40, 80], sleeptimes) |
| 545 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 546 | self.assertEqual(self.fd.getvalue(), b'123') |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 547 | self.assertEqual(False, done) |
| 548 | self.assertEqual(3, download._progress) |
| 549 | self.assertEqual(5, download._total_size) |
| 550 | self.assertEqual(3, status.resumable_progress) |
| 551 | |
| 552 | # Reset time.sleep stub. |
| 553 | del sleeptimes[0:len(sleeptimes)] |
| 554 | |
| 555 | status, done = download.next_chunk(num_retries=3) |
| 556 | |
| 557 | # Check for exponential backoff using the rand function above. |
| 558 | self.assertEqual([20, 40, 80], sleeptimes) |
| 559 | |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 560 | self.assertEqual(self.fd.getvalue(), b'12345') |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 561 | self.assertEqual(True, done) |
| 562 | self.assertEqual(5, download._progress) |
| 563 | self.assertEqual(5, download._total_size) |
| 564 | |
andrewnester | a4a44cf | 2017-03-31 16:09:31 +0300 | [diff] [blame] | 565 | def test_media_io_base_download_empty_file(self): |
| 566 | self.request.http = HttpMockSequence([ |
| 567 | ({'status': '200', |
| 568 | 'content-range': '0-0/0'}, b''), |
| 569 | ]) |
| 570 | |
| 571 | download = MediaIoBaseDownload( |
| 572 | fd=self.fd, request=self.request, chunksize=3) |
| 573 | |
| 574 | self.assertEqual(self.fd, download._fd) |
| 575 | self.assertEqual(0, download._progress) |
| 576 | self.assertEqual(None, download._total_size) |
| 577 | self.assertEqual(False, download._done) |
| 578 | self.assertEqual(self.request.uri, download._uri) |
| 579 | |
| 580 | status, done = download.next_chunk() |
| 581 | |
| 582 | self.assertEqual(True, done) |
| 583 | self.assertEqual(0, download._progress) |
| 584 | self.assertEqual(0, download._total_size) |
| 585 | self.assertEqual(0, status.progress()) |
| 586 | |
Daniel | 4406778 | 2018-01-16 23:17:56 +0100 | [diff] [blame] | 587 | def test_media_io_base_download_unknown_media_size(self): |
| 588 | self.request.http = HttpMockSequence([ |
| 589 | ({'status': '200'}, b'123') |
| 590 | ]) |
| 591 | |
| 592 | download = MediaIoBaseDownload( |
| 593 | fd=self.fd, request=self.request, chunksize=3) |
| 594 | |
| 595 | self.assertEqual(self.fd, download._fd) |
| 596 | self.assertEqual(0, download._progress) |
| 597 | self.assertEqual(None, download._total_size) |
| 598 | self.assertEqual(False, download._done) |
| 599 | self.assertEqual(self.request.uri, download._uri) |
| 600 | |
| 601 | status, done = download.next_chunk() |
| 602 | |
| 603 | self.assertEqual(self.fd.getvalue(), b'123') |
| 604 | self.assertEqual(True, done) |
| 605 | self.assertEqual(3, download._progress) |
| 606 | self.assertEqual(None, download._total_size) |
| 607 | self.assertEqual(0, status.progress()) |
| 608 | |
| 609 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 610 | EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1 |
| 611 | Content-Type: application/json |
| 612 | MIME-Version: 1.0 |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 613 | Host: www.googleapis.com |
| 614 | content-length: 2\r\n\r\n{}""" |
| 615 | |
| 616 | |
| 617 | NO_BODY_EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1 |
| 618 | Content-Type: application/json |
| 619 | MIME-Version: 1.0 |
| 620 | Host: www.googleapis.com |
| 621 | content-length: 0\r\n\r\n""" |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 622 | |
Pepper Lebeck-Jobe | 30ecbd4 | 2015-06-12 11:45:57 -0400 | [diff] [blame] | 623 | NO_BODY_EXPECTED_GET = """GET /someapi/v1/collection/?foo=bar HTTP/1.1 |
| 624 | Content-Type: application/json |
| 625 | MIME-Version: 1.0 |
| 626 | Host: www.googleapis.com\r\n\r\n""" |
| 627 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 628 | |
| 629 | RESPONSE = """HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 630 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 631 | Content-Length: 14 |
| 632 | ETag: "etag/pony"\r\n\r\n{"answer": 42}""" |
| 633 | |
| 634 | |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 635 | BATCH_RESPONSE = b"""--batch_foobarbaz |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 636 | Content-Type: application/http |
| 637 | Content-Transfer-Encoding: binary |
| 638 | Content-ID: <randomness+1> |
| 639 | |
| 640 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 641 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 642 | Content-Length: 14 |
| 643 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 644 | |
| 645 | --batch_foobarbaz |
| 646 | Content-Type: application/http |
| 647 | Content-Transfer-Encoding: binary |
| 648 | Content-ID: <randomness+2> |
| 649 | |
| 650 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 651 | Content-Type: application/json |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 652 | Content-Length: 14 |
| 653 | ETag: "etag/sheep"\r\n\r\n{"baz": "qux"} |
| 654 | --batch_foobarbaz--""" |
| 655 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 656 | |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 657 | BATCH_ERROR_RESPONSE = b"""--batch_foobarbaz |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 658 | Content-Type: application/http |
| 659 | Content-Transfer-Encoding: binary |
| 660 | Content-ID: <randomness+1> |
| 661 | |
| 662 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 663 | Content-Type: application/json |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 664 | Content-Length: 14 |
| 665 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 666 | |
| 667 | --batch_foobarbaz |
| 668 | Content-Type: application/http |
| 669 | Content-Transfer-Encoding: binary |
| 670 | Content-ID: <randomness+2> |
| 671 | |
| 672 | HTTP/1.1 403 Access Not Configured |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 673 | Content-Type: application/json |
| 674 | Content-Length: 245 |
| 675 | ETag: "etag/sheep"\r\n\r\n{ |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 676 | "error": { |
| 677 | "errors": [ |
| 678 | { |
| 679 | "domain": "usageLimits", |
| 680 | "reason": "accessNotConfigured", |
| 681 | "message": "Access Not Configured", |
| 682 | "debugInfo": "QuotaState: BLOCKED" |
| 683 | } |
| 684 | ], |
| 685 | "code": 403, |
| 686 | "message": "Access Not Configured" |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | --batch_foobarbaz--""" |
| 691 | |
| 692 | |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 693 | BATCH_RESPONSE_WITH_401 = b"""--batch_foobarbaz |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 694 | Content-Type: application/http |
| 695 | Content-Transfer-Encoding: binary |
| 696 | Content-ID: <randomness+1> |
| 697 | |
Joe Gregorio | c752e33 | 2012-07-11 14:43:52 -0400 | [diff] [blame] | 698 | HTTP/1.1 401 Authorization Required |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 699 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 700 | Content-Length: 14 |
| 701 | ETag: "etag/pony"\r\n\r\n{"error": {"message": |
| 702 | "Authorizaton failed."}} |
| 703 | |
| 704 | --batch_foobarbaz |
| 705 | Content-Type: application/http |
| 706 | Content-Transfer-Encoding: binary |
| 707 | Content-ID: <randomness+2> |
| 708 | |
| 709 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 710 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 711 | Content-Length: 14 |
| 712 | ETag: "etag/sheep"\r\n\r\n{"baz": "qux"} |
| 713 | --batch_foobarbaz--""" |
| 714 | |
| 715 | |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 716 | BATCH_SINGLE_RESPONSE = b"""--batch_foobarbaz |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 717 | Content-Type: application/http |
| 718 | Content-Transfer-Encoding: binary |
| 719 | Content-ID: <randomness+1> |
| 720 | |
| 721 | HTTP/1.1 200 OK |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 722 | Content-Type: application/json |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 723 | Content-Length: 14 |
| 724 | ETag: "etag/pony"\r\n\r\n{"foo": 42} |
| 725 | --batch_foobarbaz--""" |
| 726 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 727 | |
| 728 | USER_RATE_LIMIT_EXCEEDED_RESPONSE = """{ |
| 729 | "error": { |
| 730 | "errors": [ |
| 731 | { |
| 732 | "domain": "usageLimits", |
| 733 | "reason": "userRateLimitExceeded", |
| 734 | "message": "User Rate Limit Exceeded" |
| 735 | } |
| 736 | ], |
| 737 | "code": 403, |
| 738 | "message": "User Rate Limit Exceeded" |
| 739 | } |
| 740 | }""" |
| 741 | |
| 742 | |
| 743 | RATE_LIMIT_EXCEEDED_RESPONSE = """{ |
| 744 | "error": { |
| 745 | "errors": [ |
| 746 | { |
| 747 | "domain": "usageLimits", |
| 748 | "reason": "rateLimitExceeded", |
| 749 | "message": "Rate Limit Exceeded" |
| 750 | } |
| 751 | ], |
| 752 | "code": 403, |
| 753 | "message": "Rate Limit Exceeded" |
| 754 | } |
| 755 | }""" |
| 756 | |
| 757 | |
| 758 | NOT_CONFIGURED_RESPONSE = """{ |
| 759 | "error": { |
| 760 | "errors": [ |
| 761 | { |
| 762 | "domain": "usageLimits", |
| 763 | "reason": "accessNotConfigured", |
| 764 | "message": "Access Not Configured" |
| 765 | } |
| 766 | ], |
| 767 | "code": 403, |
| 768 | "message": "Access Not Configured" |
| 769 | } |
| 770 | }""" |
| 771 | |
Nilayan Bhattacharya | 90ffb85 | 2017-12-05 15:30:32 -0800 | [diff] [blame] | 772 | LIST_NOT_CONFIGURED_RESPONSE = """[ |
| 773 | "error": { |
| 774 | "errors": [ |
| 775 | { |
| 776 | "domain": "usageLimits", |
| 777 | "reason": "accessNotConfigured", |
| 778 | "message": "Access Not Configured" |
| 779 | } |
| 780 | ], |
| 781 | "code": 403, |
| 782 | "message": "Access Not Configured" |
| 783 | } |
| 784 | ]""" |
| 785 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 786 | class Callbacks(object): |
| 787 | def __init__(self): |
| 788 | self.responses = {} |
| 789 | self.exceptions = {} |
| 790 | |
| 791 | def f(self, request_id, response, exception): |
| 792 | self.responses[request_id] = response |
| 793 | self.exceptions[request_id] = exception |
| 794 | |
| 795 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 796 | class TestHttpRequest(unittest.TestCase): |
| 797 | def test_unicode(self): |
| 798 | http = HttpMock(datafile('zoo.json'), headers={'status': '200'}) |
| 799 | model = JsonModel() |
| 800 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 801 | method = u'POST' |
| 802 | request = HttpRequest( |
| 803 | http, |
| 804 | model.response, |
| 805 | uri, |
| 806 | method=method, |
| 807 | body=u'{}', |
| 808 | headers={'content-type': 'application/json'}) |
| 809 | request.execute() |
| 810 | self.assertEqual(uri, http.uri) |
| 811 | self.assertEqual(str, type(http.uri)) |
| 812 | self.assertEqual(method, http.method) |
| 813 | self.assertEqual(str, type(http.method)) |
| 814 | |
Ondrej Medek | b86bfc9 | 2018-01-19 18:53:50 +0100 | [diff] [blame] | 815 | def test_empty_content_type(self): |
| 816 | """Test for #284""" |
| 817 | http = HttpMock(None, headers={'status': 200}) |
| 818 | uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar' |
| 819 | method = u'POST' |
| 820 | request = HttpRequest( |
| 821 | http, |
| 822 | _postproc_none, |
| 823 | uri, |
| 824 | method=method, |
| 825 | headers={'content-type': ''}) |
| 826 | request.execute() |
| 827 | self.assertEqual('', http.headers.get('content-type')) |
| 828 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 829 | def test_no_retry_connection_errors(self): |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 830 | model = JsonModel() |
| 831 | request = HttpRequest( |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 832 | HttpMockWithNonRetriableErrors(1, {'status': '200'}, '{"foo": "bar"}'), |
| 833 | model.response, |
| 834 | u'https://www.example.com/json_api_endpoint') |
| 835 | request._sleep = lambda _x: 0 # do nothing |
| 836 | request._rand = lambda: 10 |
| 837 | with self.assertRaises(socket.error): |
| 838 | response = request.execute(num_retries=3) |
| 839 | |
| 840 | |
| 841 | def test_retry_connection_errors_non_resumable(self): |
| 842 | model = JsonModel() |
| 843 | request = HttpRequest( |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 844 | HttpMockWithErrors(4, {'status': '200'}, '{"foo": "bar"}'), |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 845 | model.response, |
| 846 | u'https://www.example.com/json_api_endpoint') |
| 847 | request._sleep = lambda _x: 0 # do nothing |
| 848 | request._rand = lambda: 10 |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 849 | response = request.execute(num_retries=4) |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 850 | self.assertEqual({u'foo': u'bar'}, response) |
| 851 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 852 | def test_retry_connection_errors_resumable(self): |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 853 | with open(datafile('small.png'), 'rb') as small_png_file: |
| 854 | small_png_fd = BytesIO(small_png_file.read()) |
| 855 | upload = MediaIoBaseUpload(fd=small_png_fd, mimetype='image/png', |
| 856 | chunksize=500, resumable=True) |
| 857 | model = JsonModel() |
| 858 | |
| 859 | request = HttpRequest( |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 860 | HttpMockWithErrors( |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 861 | 4, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'), |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 862 | model.response, |
| 863 | u'https://www.example.com/file_upload', |
| 864 | method='POST', |
| 865 | resumable=upload) |
| 866 | request._sleep = lambda _x: 0 # do nothing |
| 867 | request._rand = lambda: 10 |
Bashir Sadjad | c35150f | 2018-06-25 11:46:09 -0400 | [diff] [blame^] | 868 | response = request.execute(num_retries=4) |
Sergiy Byelozyorov | 703c92c | 2015-12-21 23:27:48 +0100 | [diff] [blame] | 869 | self.assertEqual({u'foo': u'bar'}, response) |
| 870 | |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 871 | def test_retry(self): |
| 872 | num_retries = 5 |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 873 | resp_seq = [({'status': '500'}, '')] * (num_retries - 3) |
| 874 | resp_seq.append(({'status': '403'}, RATE_LIMIT_EXCEEDED_RESPONSE)) |
| 875 | resp_seq.append(({'status': '403'}, USER_RATE_LIMIT_EXCEEDED_RESPONSE)) |
| 876 | resp_seq.append(({'status': '429'}, '')) |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 877 | resp_seq.append(({'status': '200'}, '{}')) |
| 878 | |
| 879 | http = HttpMockSequence(resp_seq) |
| 880 | model = JsonModel() |
| 881 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 882 | method = u'POST' |
| 883 | request = HttpRequest( |
| 884 | http, |
| 885 | model.response, |
| 886 | uri, |
| 887 | method=method, |
| 888 | body=u'{}', |
| 889 | headers={'content-type': 'application/json'}) |
| 890 | |
| 891 | sleeptimes = [] |
| 892 | request._sleep = lambda x: sleeptimes.append(x) |
| 893 | request._rand = lambda: 10 |
| 894 | |
| 895 | request.execute(num_retries=num_retries) |
| 896 | |
| 897 | self.assertEqual(num_retries, len(sleeptimes)) |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 898 | for retry_num in range(num_retries): |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 899 | self.assertEqual(10 * 2**(retry_num + 1), sleeptimes[retry_num]) |
| 900 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 901 | def test_no_retry_succeeds(self): |
| 902 | num_retries = 5 |
| 903 | resp_seq = [({'status': '200'}, '{}')] * (num_retries) |
| 904 | |
| 905 | http = HttpMockSequence(resp_seq) |
| 906 | model = JsonModel() |
| 907 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 908 | method = u'POST' |
| 909 | request = HttpRequest( |
| 910 | http, |
| 911 | model.response, |
| 912 | uri, |
| 913 | method=method, |
| 914 | body=u'{}', |
| 915 | headers={'content-type': 'application/json'}) |
| 916 | |
| 917 | sleeptimes = [] |
| 918 | request._sleep = lambda x: sleeptimes.append(x) |
| 919 | request._rand = lambda: 10 |
| 920 | |
| 921 | request.execute(num_retries=num_retries) |
| 922 | |
| 923 | self.assertEqual(0, len(sleeptimes)) |
| 924 | |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 925 | def test_no_retry_fails_fast(self): |
| 926 | http = HttpMockSequence([ |
| 927 | ({'status': '500'}, ''), |
| 928 | ({'status': '200'}, '{}') |
| 929 | ]) |
| 930 | model = JsonModel() |
| 931 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 932 | method = u'POST' |
| 933 | request = HttpRequest( |
| 934 | http, |
| 935 | model.response, |
| 936 | uri, |
| 937 | method=method, |
| 938 | body=u'{}', |
| 939 | headers={'content-type': 'application/json'}) |
| 940 | |
| 941 | request._rand = lambda: 1.0 |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 942 | request._sleep = mock.MagicMock() |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 943 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 944 | with self.assertRaises(HttpError): |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 945 | request.execute() |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 946 | request._sleep.assert_not_called() |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 947 | |
eesheesh | c6425a0 | 2016-02-12 15:07:06 +0000 | [diff] [blame] | 948 | def test_no_retry_403_not_configured_fails_fast(self): |
| 949 | http = HttpMockSequence([ |
| 950 | ({'status': '403'}, NOT_CONFIGURED_RESPONSE), |
| 951 | ({'status': '200'}, '{}') |
| 952 | ]) |
| 953 | model = JsonModel() |
| 954 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 955 | method = u'POST' |
| 956 | request = HttpRequest( |
| 957 | http, |
| 958 | model.response, |
| 959 | uri, |
| 960 | method=method, |
| 961 | body=u'{}', |
| 962 | headers={'content-type': 'application/json'}) |
| 963 | |
| 964 | request._rand = lambda: 1.0 |
| 965 | request._sleep = mock.MagicMock() |
| 966 | |
| 967 | with self.assertRaises(HttpError): |
| 968 | request.execute() |
| 969 | request._sleep.assert_not_called() |
| 970 | |
| 971 | def test_no_retry_403_fails_fast(self): |
| 972 | http = HttpMockSequence([ |
| 973 | ({'status': '403'}, ''), |
| 974 | ({'status': '200'}, '{}') |
| 975 | ]) |
| 976 | model = JsonModel() |
| 977 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 978 | method = u'POST' |
| 979 | request = HttpRequest( |
| 980 | http, |
| 981 | model.response, |
| 982 | uri, |
| 983 | method=method, |
| 984 | body=u'{}', |
| 985 | headers={'content-type': 'application/json'}) |
| 986 | |
| 987 | request._rand = lambda: 1.0 |
| 988 | request._sleep = mock.MagicMock() |
| 989 | |
| 990 | with self.assertRaises(HttpError): |
| 991 | request.execute() |
| 992 | request._sleep.assert_not_called() |
| 993 | |
| 994 | def test_no_retry_401_fails_fast(self): |
| 995 | http = HttpMockSequence([ |
| 996 | ({'status': '401'}, ''), |
| 997 | ({'status': '200'}, '{}') |
| 998 | ]) |
| 999 | model = JsonModel() |
| 1000 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 1001 | method = u'POST' |
| 1002 | request = HttpRequest( |
| 1003 | http, |
| 1004 | model.response, |
| 1005 | uri, |
| 1006 | method=method, |
| 1007 | body=u'{}', |
| 1008 | headers={'content-type': 'application/json'}) |
| 1009 | |
| 1010 | request._rand = lambda: 1.0 |
| 1011 | request._sleep = mock.MagicMock() |
| 1012 | |
| 1013 | with self.assertRaises(HttpError): |
| 1014 | request.execute() |
| 1015 | request._sleep.assert_not_called() |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 1016 | |
Nilayan Bhattacharya | 90ffb85 | 2017-12-05 15:30:32 -0800 | [diff] [blame] | 1017 | def test_no_retry_403_list_fails(self): |
| 1018 | http = HttpMockSequence([ |
| 1019 | ({'status': '403'}, LIST_NOT_CONFIGURED_RESPONSE), |
| 1020 | ({'status': '200'}, '{}') |
| 1021 | ]) |
| 1022 | model = JsonModel() |
| 1023 | uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar' |
| 1024 | method = u'POST' |
| 1025 | request = HttpRequest( |
| 1026 | http, |
| 1027 | model.response, |
| 1028 | uri, |
| 1029 | method=method, |
| 1030 | body=u'{}', |
| 1031 | headers={'content-type': 'application/json'}) |
| 1032 | |
| 1033 | request._rand = lambda: 1.0 |
| 1034 | request._sleep = mock.MagicMock() |
| 1035 | |
| 1036 | with self.assertRaises(HttpError): |
| 1037 | request.execute() |
| 1038 | request._sleep.assert_not_called() |
| 1039 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1040 | class TestBatch(unittest.TestCase): |
| 1041 | |
| 1042 | def setUp(self): |
| 1043 | model = JsonModel() |
| 1044 | self.request1 = HttpRequest( |
| 1045 | None, |
| 1046 | model.response, |
| 1047 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1048 | method='POST', |
| 1049 | body='{}', |
| 1050 | headers={'content-type': 'application/json'}) |
| 1051 | |
| 1052 | self.request2 = HttpRequest( |
| 1053 | None, |
| 1054 | model.response, |
| 1055 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1056 | method='GET', |
| 1057 | body='', |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1058 | headers={'content-type': 'application/json'}) |
| 1059 | |
| 1060 | |
| 1061 | def test_id_to_from_content_id_header(self): |
| 1062 | batch = BatchHttpRequest() |
| 1063 | self.assertEquals('12', batch._header_to_id(batch._id_to_header('12'))) |
| 1064 | |
| 1065 | def test_invalid_content_id_header(self): |
| 1066 | batch = BatchHttpRequest() |
| 1067 | self.assertRaises(BatchError, batch._header_to_id, '[foo+x]') |
| 1068 | self.assertRaises(BatchError, batch._header_to_id, 'foo+1') |
| 1069 | self.assertRaises(BatchError, batch._header_to_id, '<foo>') |
| 1070 | |
| 1071 | def test_serialize_request(self): |
| 1072 | batch = BatchHttpRequest() |
| 1073 | request = HttpRequest( |
| 1074 | None, |
| 1075 | None, |
| 1076 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1077 | method='POST', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1078 | body=u'{}', |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1079 | headers={'content-type': 'application/json'}, |
| 1080 | methodId=None, |
| 1081 | resumable=None) |
| 1082 | s = batch._serialize_request(request).splitlines() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1083 | self.assertEqual(EXPECTED.splitlines(), s) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1084 | |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 1085 | def test_serialize_request_media_body(self): |
| 1086 | batch = BatchHttpRequest() |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1087 | f = open(datafile('small.png'), 'rb') |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 1088 | body = f.read() |
| 1089 | f.close() |
| 1090 | |
| 1091 | request = HttpRequest( |
| 1092 | None, |
| 1093 | None, |
| 1094 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1095 | method='POST', |
| 1096 | body=body, |
| 1097 | headers={'content-type': 'application/json'}, |
| 1098 | methodId=None, |
| 1099 | resumable=None) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1100 | # Just testing it shouldn't raise an exception. |
Joe Gregorio | dd81382 | 2012-01-25 10:32:47 -0500 | [diff] [blame] | 1101 | s = batch._serialize_request(request).splitlines() |
| 1102 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1103 | def test_serialize_request_no_body(self): |
| 1104 | batch = BatchHttpRequest() |
| 1105 | request = HttpRequest( |
| 1106 | None, |
| 1107 | None, |
| 1108 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1109 | method='POST', |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1110 | body=b'', |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1111 | headers={'content-type': 'application/json'}, |
| 1112 | methodId=None, |
| 1113 | resumable=None) |
| 1114 | s = batch._serialize_request(request).splitlines() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1115 | self.assertEqual(NO_BODY_EXPECTED.splitlines(), s) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1116 | |
Pepper Lebeck-Jobe | 30ecbd4 | 2015-06-12 11:45:57 -0400 | [diff] [blame] | 1117 | def test_serialize_get_request_no_body(self): |
| 1118 | batch = BatchHttpRequest() |
| 1119 | request = HttpRequest( |
| 1120 | None, |
| 1121 | None, |
| 1122 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1123 | method='GET', |
| 1124 | body=None, |
| 1125 | headers={'content-type': 'application/json'}, |
| 1126 | methodId=None, |
| 1127 | resumable=None) |
| 1128 | s = batch._serialize_request(request).splitlines() |
| 1129 | self.assertEqual(NO_BODY_EXPECTED_GET.splitlines(), s) |
| 1130 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1131 | def test_deserialize_response(self): |
| 1132 | batch = BatchHttpRequest() |
| 1133 | resp, content = batch._deserialize_response(RESPONSE) |
| 1134 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1135 | self.assertEqual(200, resp.status) |
| 1136 | self.assertEqual('OK', resp.reason) |
| 1137 | self.assertEqual(11, resp.version) |
| 1138 | self.assertEqual('{"answer": 42}', content) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1139 | |
| 1140 | def test_new_id(self): |
| 1141 | batch = BatchHttpRequest() |
| 1142 | |
| 1143 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1144 | self.assertEqual('1', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1145 | |
| 1146 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1147 | self.assertEqual('2', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1148 | |
| 1149 | batch.add(self.request1, request_id='3') |
| 1150 | |
| 1151 | id_ = batch._new_id() |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1152 | self.assertEqual('4', id_) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1153 | |
| 1154 | def test_add(self): |
| 1155 | batch = BatchHttpRequest() |
| 1156 | batch.add(self.request1, request_id='1') |
| 1157 | self.assertRaises(KeyError, batch.add, self.request1, request_id='1') |
| 1158 | |
| 1159 | def test_add_fail_for_resumable(self): |
| 1160 | batch = BatchHttpRequest() |
| 1161 | |
| 1162 | upload = MediaFileUpload( |
| 1163 | datafile('small.png'), chunksize=500, resumable=True) |
| 1164 | self.request1.resumable = upload |
ittus | 5f00cad | 2016-10-15 10:32:40 +0800 | [diff] [blame] | 1165 | with self.assertRaises(BatchError) as batch_error: |
| 1166 | batch.add(self.request1, request_id='1') |
| 1167 | str(batch_error.exception) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1168 | |
Mohamed Zenadi | 1b5350d | 2015-07-30 11:52:39 +0200 | [diff] [blame] | 1169 | def test_execute_empty_batch_no_http(self): |
| 1170 | batch = BatchHttpRequest() |
| 1171 | ret = batch.execute() |
| 1172 | self.assertEqual(None, ret) |
| 1173 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1174 | def test_execute(self): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1175 | batch = BatchHttpRequest() |
| 1176 | callbacks = Callbacks() |
| 1177 | |
| 1178 | batch.add(self.request1, callback=callbacks.f) |
| 1179 | batch.add(self.request2, callback=callbacks.f) |
| 1180 | http = HttpMockSequence([ |
| 1181 | ({'status': '200', |
| 1182 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1183 | BATCH_RESPONSE), |
| 1184 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1185 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1186 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 1187 | self.assertEqual(None, callbacks.exceptions['1']) |
| 1188 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
| 1189 | self.assertEqual(None, callbacks.exceptions['2']) |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1190 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1191 | def test_execute_request_body(self): |
| 1192 | batch = BatchHttpRequest() |
| 1193 | |
| 1194 | batch.add(self.request1) |
| 1195 | batch.add(self.request2) |
| 1196 | http = HttpMockSequence([ |
| 1197 | ({'status': '200', |
| 1198 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1199 | 'echo_request_body'), |
| 1200 | ]) |
| 1201 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1202 | batch.execute(http=http) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1203 | self.fail('Should raise exception') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 1204 | except BatchError as e: |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1205 | boundary, _ = e.content.split(None, 1) |
| 1206 | self.assertEqual('--', boundary[:2]) |
| 1207 | parts = e.content.split(boundary) |
| 1208 | self.assertEqual(4, len(parts)) |
| 1209 | self.assertEqual('', parts[0]) |
Craig Citro | 4282aa3 | 2014-06-21 00:33:39 -0700 | [diff] [blame] | 1210 | self.assertEqual('--', parts[3].rstrip()) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 1211 | header = parts[1].splitlines()[1] |
| 1212 | self.assertEqual('Content-Type: application/http', header) |
| 1213 | |
Gabriel Garcia | 23174be | 2016-05-25 17:28:07 +0200 | [diff] [blame] | 1214 | def test_execute_initial_refresh_oauth2(self): |
| 1215 | batch = BatchHttpRequest() |
| 1216 | callbacks = Callbacks() |
Jon Wayne Parrott | d3a5cf4 | 2017-06-19 17:55:04 -0700 | [diff] [blame] | 1217 | cred = MockCredentials('Foo', expired=True) |
Gabriel Garcia | 23174be | 2016-05-25 17:28:07 +0200 | [diff] [blame] | 1218 | |
| 1219 | http = HttpMockSequence([ |
| 1220 | ({'status': '200', |
| 1221 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1222 | BATCH_SINGLE_RESPONSE), |
| 1223 | ]) |
| 1224 | |
| 1225 | cred.authorize(http) |
| 1226 | |
| 1227 | batch.add(self.request1, callback=callbacks.f) |
| 1228 | batch.execute(http=http) |
| 1229 | |
| 1230 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 1231 | self.assertIsNone(callbacks.exceptions['1']) |
| 1232 | |
| 1233 | self.assertEqual(1, cred._refreshed) |
| 1234 | |
| 1235 | self.assertEqual(1, cred._authorized) |
| 1236 | |
| 1237 | self.assertEqual(1, cred._applied) |
| 1238 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1239 | def test_execute_refresh_and_retry_on_401(self): |
| 1240 | batch = BatchHttpRequest() |
| 1241 | callbacks = Callbacks() |
| 1242 | cred_1 = MockCredentials('Foo') |
| 1243 | cred_2 = MockCredentials('Bar') |
| 1244 | |
| 1245 | http = HttpMockSequence([ |
| 1246 | ({'status': '200', |
| 1247 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1248 | BATCH_RESPONSE_WITH_401), |
| 1249 | ({'status': '200', |
| 1250 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1251 | BATCH_SINGLE_RESPONSE), |
| 1252 | ]) |
| 1253 | |
| 1254 | creds_http_1 = HttpMockSequence([]) |
| 1255 | cred_1.authorize(creds_http_1) |
| 1256 | |
| 1257 | creds_http_2 = HttpMockSequence([]) |
| 1258 | cred_2.authorize(creds_http_2) |
| 1259 | |
| 1260 | self.request1.http = creds_http_1 |
| 1261 | self.request2.http = creds_http_2 |
| 1262 | |
| 1263 | batch.add(self.request1, callback=callbacks.f) |
| 1264 | batch.add(self.request2, callback=callbacks.f) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1265 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1266 | |
| 1267 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 1268 | self.assertEqual(None, callbacks.exceptions['1']) |
| 1269 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
| 1270 | self.assertEqual(None, callbacks.exceptions['2']) |
| 1271 | |
| 1272 | self.assertEqual(1, cred_1._refreshed) |
| 1273 | self.assertEqual(0, cred_2._refreshed) |
| 1274 | |
| 1275 | self.assertEqual(1, cred_1._authorized) |
| 1276 | self.assertEqual(1, cred_2._authorized) |
| 1277 | |
| 1278 | self.assertEqual(1, cred_2._applied) |
| 1279 | self.assertEqual(2, cred_1._applied) |
| 1280 | |
| 1281 | def test_http_errors_passed_to_callback(self): |
| 1282 | batch = BatchHttpRequest() |
| 1283 | callbacks = Callbacks() |
| 1284 | cred_1 = MockCredentials('Foo') |
| 1285 | cred_2 = MockCredentials('Bar') |
| 1286 | |
| 1287 | http = HttpMockSequence([ |
| 1288 | ({'status': '200', |
| 1289 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1290 | BATCH_RESPONSE_WITH_401), |
| 1291 | ({'status': '200', |
| 1292 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1293 | BATCH_RESPONSE_WITH_401), |
| 1294 | ]) |
| 1295 | |
| 1296 | creds_http_1 = HttpMockSequence([]) |
| 1297 | cred_1.authorize(creds_http_1) |
| 1298 | |
| 1299 | creds_http_2 = HttpMockSequence([]) |
| 1300 | cred_2.authorize(creds_http_2) |
| 1301 | |
| 1302 | self.request1.http = creds_http_1 |
| 1303 | self.request2.http = creds_http_2 |
| 1304 | |
| 1305 | batch.add(self.request1, callback=callbacks.f) |
| 1306 | batch.add(self.request2, callback=callbacks.f) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1307 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1308 | |
| 1309 | self.assertEqual(None, callbacks.responses['1']) |
| 1310 | self.assertEqual(401, callbacks.exceptions['1'].resp.status) |
Joe Gregorio | c752e33 | 2012-07-11 14:43:52 -0400 | [diff] [blame] | 1311 | self.assertEqual( |
| 1312 | 'Authorization Required', callbacks.exceptions['1'].resp.reason) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1313 | self.assertEqual({u'baz': u'qux'}, callbacks.responses['2']) |
| 1314 | self.assertEqual(None, callbacks.exceptions['2']) |
| 1315 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1316 | def test_execute_global_callback(self): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 1317 | callbacks = Callbacks() |
| 1318 | batch = BatchHttpRequest(callback=callbacks.f) |
| 1319 | |
| 1320 | batch.add(self.request1) |
| 1321 | batch.add(self.request2) |
| 1322 | http = HttpMockSequence([ |
| 1323 | ({'status': '200', |
| 1324 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1325 | BATCH_RESPONSE), |
| 1326 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1327 | batch.execute(http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 1328 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 1329 | self.assertEqual({'baz': 'qux'}, callbacks.responses['2']) |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 1330 | |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 1331 | def test_execute_batch_http_error(self): |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 1332 | callbacks = Callbacks() |
| 1333 | batch = BatchHttpRequest(callback=callbacks.f) |
| 1334 | |
| 1335 | batch.add(self.request1) |
| 1336 | batch.add(self.request2) |
| 1337 | http = HttpMockSequence([ |
| 1338 | ({'status': '200', |
| 1339 | 'content-type': 'multipart/mixed; boundary="batch_foobarbaz"'}, |
| 1340 | BATCH_ERROR_RESPONSE), |
| 1341 | ]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1342 | batch.execute(http=http) |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 1343 | self.assertEqual({'foo': 42}, callbacks.responses['1']) |
| 1344 | expected = ('<HttpError 403 when requesting ' |
| 1345 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar returned ' |
| 1346 | '"Access Not Configured">') |
| 1347 | self.assertEqual(expected, str(callbacks.exceptions['2'])) |
Ali Afshar | 6f11ea1 | 2012-02-07 10:32:14 -0500 | [diff] [blame] | 1348 | |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1349 | |
Joe Gregorio | ba5c790 | 2012-08-03 12:48:16 -0400 | [diff] [blame] | 1350 | class TestRequestUriTooLong(unittest.TestCase): |
| 1351 | |
| 1352 | def test_turn_get_into_post(self): |
| 1353 | |
| 1354 | def _postproc(resp, content): |
| 1355 | return content |
| 1356 | |
| 1357 | http = HttpMockSequence([ |
| 1358 | ({'status': '200'}, |
| 1359 | 'echo_request_body'), |
| 1360 | ({'status': '200'}, |
| 1361 | 'echo_request_headers'), |
| 1362 | ]) |
| 1363 | |
| 1364 | # Send a long query parameter. |
| 1365 | query = { |
| 1366 | 'q': 'a' * MAX_URI_LENGTH + '?&' |
| 1367 | } |
| 1368 | req = HttpRequest( |
| 1369 | http, |
| 1370 | _postproc, |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 1371 | 'http://example.com?' + urlencode(query), |
Joe Gregorio | ba5c790 | 2012-08-03 12:48:16 -0400 | [diff] [blame] | 1372 | method='GET', |
| 1373 | body=None, |
| 1374 | headers={}, |
| 1375 | methodId='foo', |
| 1376 | resumable=None) |
| 1377 | |
| 1378 | # Query parameters should be sent in the body. |
| 1379 | response = req.execute() |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 1380 | self.assertEqual(b'q=' + b'a' * MAX_URI_LENGTH + b'%3F%26', response) |
Joe Gregorio | ba5c790 | 2012-08-03 12:48:16 -0400 | [diff] [blame] | 1381 | |
| 1382 | # Extra headers should be set. |
| 1383 | response = req.execute() |
| 1384 | self.assertEqual('GET', response['x-http-method-override']) |
| 1385 | self.assertEqual(str(MAX_URI_LENGTH + 8), response['content-length']) |
| 1386 | self.assertEqual( |
| 1387 | 'application/x-www-form-urlencoded', response['content-type']) |
| 1388 | |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1389 | |
| 1390 | class TestStreamSlice(unittest.TestCase): |
| 1391 | """Test _StreamSlice.""" |
| 1392 | |
| 1393 | def setUp(self): |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1394 | self.stream = BytesIO(b'0123456789') |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1395 | |
| 1396 | def test_read(self): |
| 1397 | s = _StreamSlice(self.stream, 0, 4) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1398 | self.assertEqual(b'', s.read(0)) |
| 1399 | self.assertEqual(b'0', s.read(1)) |
| 1400 | self.assertEqual(b'123', s.read()) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1401 | |
| 1402 | def test_read_too_much(self): |
| 1403 | s = _StreamSlice(self.stream, 1, 4) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1404 | self.assertEqual(b'1234', s.read(6)) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1405 | |
| 1406 | def test_read_all(self): |
| 1407 | s = _StreamSlice(self.stream, 2, 1) |
Pat Ferate | 2b14022 | 2015-03-03 18:05:11 -0800 | [diff] [blame] | 1408 | self.assertEqual(b'2', s.read(-1)) |
Joe Gregorio | 5c120db | 2012-08-23 09:13:55 -0400 | [diff] [blame] | 1409 | |
Ali Afshar | 164f37e | 2013-01-07 14:05:45 -0800 | [diff] [blame] | 1410 | |
| 1411 | class TestResponseCallback(unittest.TestCase): |
| 1412 | """Test adding callbacks to responses.""" |
| 1413 | |
| 1414 | def test_ensure_response_callback(self): |
| 1415 | m = JsonModel() |
| 1416 | request = HttpRequest( |
| 1417 | None, |
| 1418 | m.response, |
| 1419 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1420 | method='POST', |
| 1421 | body='{}', |
| 1422 | headers={'content-type': 'application/json'}) |
| 1423 | h = HttpMockSequence([ ({'status': 200}, '{}')]) |
| 1424 | responses = [] |
| 1425 | def _on_response(resp, responses=responses): |
| 1426 | responses.append(resp) |
| 1427 | request.add_response_callback(_on_response) |
| 1428 | request.execute(http=h) |
| 1429 | self.assertEqual(1, len(responses)) |
| 1430 | |
| 1431 | |
Craig Gurnik | 8e55b76 | 2015-01-20 15:00:10 -0500 | [diff] [blame] | 1432 | class TestHttpMock(unittest.TestCase): |
| 1433 | def test_default_response_headers(self): |
| 1434 | http = HttpMock(datafile('zoo.json')) |
| 1435 | resp, content = http.request("http://example.com") |
| 1436 | self.assertEqual(resp.status, 200) |
| 1437 | |
Alan Briolat | 26b0100 | 2015-08-14 00:13:57 +0100 | [diff] [blame] | 1438 | def test_error_response(self): |
| 1439 | http = HttpMock(datafile('bad_request.json'), {'status': '400'}) |
| 1440 | model = JsonModel() |
| 1441 | request = HttpRequest( |
| 1442 | http, |
| 1443 | model.response, |
| 1444 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 1445 | method='GET', |
| 1446 | headers={}) |
| 1447 | self.assertRaises(HttpError, request.execute) |
| 1448 | |
Craig Gurnik | 8e55b76 | 2015-01-20 15:00:10 -0500 | [diff] [blame] | 1449 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 1450 | class TestHttpBuild(unittest.TestCase): |
| 1451 | original_socket_default_timeout = None |
| 1452 | |
| 1453 | @classmethod |
| 1454 | def setUpClass(cls): |
| 1455 | cls.original_socket_default_timeout = socket.getdefaulttimeout() |
| 1456 | |
| 1457 | @classmethod |
| 1458 | def tearDownClass(cls): |
| 1459 | socket.setdefaulttimeout(cls.original_socket_default_timeout) |
| 1460 | |
| 1461 | def test_build_http_sets_default_timeout_if_none_specified(self): |
| 1462 | socket.setdefaulttimeout(None) |
| 1463 | http = build_http() |
| 1464 | self.assertIsInstance(http.timeout, int) |
| 1465 | self.assertGreater(http.timeout, 0) |
| 1466 | |
| 1467 | def test_build_http_default_timeout_can_be_overridden(self): |
| 1468 | socket.setdefaulttimeout(1.5) |
| 1469 | http = build_http() |
| 1470 | self.assertAlmostEqual(http.timeout, 1.5, delta=0.001) |
| 1471 | |
| 1472 | def test_build_http_default_timeout_can_be_set_to_zero(self): |
| 1473 | socket.setdefaulttimeout(0) |
| 1474 | http = build_http() |
| 1475 | self.assertEquals(http.timeout, 0) |
| 1476 | |
| 1477 | |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 1478 | if __name__ == '__main__': |
Joe Gregorio | 9086bd3 | 2013-06-14 16:32:05 -0400 | [diff] [blame] | 1479 | logging.getLogger().setLevel(logging.ERROR) |
Joe Gregorio | 6bcbcea | 2011-03-10 15:26:05 -0500 | [diff] [blame] | 1480 | unittest.main() |