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