forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch.py
More file actions
450 lines (380 loc) · 16.7 KB
/
Copy pathtest_batch.py
File metadata and controls
450 lines (380 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest2
class TestMIMEApplicationHTTP(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.storage.batch import MIMEApplicationHTTP
return MIMEApplicationHTTP
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_body_None(self):
METHOD = 'DELETE'
PATH = '/path/to/api'
LINES = [
"DELETE /path/to/api HTTP/1.1",
"",
]
mah = self._makeOne(METHOD, PATH, {}, None)
self.assertEqual(mah.get_content_type(), 'application/http')
self.assertEqual(mah.get_payload().splitlines(), LINES)
def test_ctor_body_str(self):
METHOD = 'GET'
PATH = '/path/to/api'
BODY = 'ABC'
HEADERS = {'Content-Length': len(BODY), 'Content-Type': 'text/plain'}
LINES = [
"GET /path/to/api HTTP/1.1",
"Content-Length: 3",
"Content-Type: text/plain",
"",
"ABC",
]
mah = self._makeOne(METHOD, PATH, HEADERS, BODY)
self.assertEqual(mah.get_payload().splitlines(), LINES)
def test_ctor_body_dict(self):
METHOD = 'GET'
PATH = '/path/to/api'
BODY = {'foo': 'bar'}
HEADERS = {}
LINES = [
'GET /path/to/api HTTP/1.1',
'Content-Length: 14',
'Content-Type: application/json',
'',
'{"foo": "bar"}',
]
mah = self._makeOne(METHOD, PATH, HEADERS, BODY)
self.assertEqual(mah.get_payload().splitlines(), LINES)
class TestBatch(unittest2.TestCase):
def setUp(self):
from gcloud.storage._testing import _setup_defaults
_setup_defaults(self)
def tearDown(self):
from gcloud.storage._testing import _tear_down_defaults
_tear_down_defaults(self)
def _getTargetClass(self):
from gcloud.storage.batch import Batch
return Batch
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_w_explicit_connection(self):
http = _HTTP()
connection = _Connection(http=http)
batch = self._makeOne(connection)
self.assertTrue(batch._connection is connection)
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._responses), 0)
def test_ctor_w_implicit_connection(self):
from gcloud.storage._testing import _monkey_defaults
http = _HTTP()
connection = _Connection(http=http)
with _monkey_defaults(connection=connection):
batch = self._makeOne()
self.assertTrue(batch._connection is connection)
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._responses), 0)
def test__make_request_GET_forwarded_to_connection(self):
URL = 'http://example.com/api'
expected = _Response()
http = _HTTP((expected, ''))
connection = _Connection(http=http)
batch = self._makeOne(connection)
response, content = batch._make_request('GET', URL)
self.assertTrue(response is expected)
self.assertEqual(content, '')
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 0),
]
self.assertEqual(len(http._requests), 1)
self.assertEqual(http._requests[0][0], 'GET')
self.assertEqual(http._requests[0][1], URL)
headers = http._requests[0][2]
for key, value in EXPECTED_HEADERS:
self.assertEqual(headers[key], value)
self.assertEqual(http._requests[0][3], None)
self.assertEqual(batch._requests, [])
def test__make_request_POST_normal(self):
URL = 'http://example.com/api'
http = _HTTP() # no requests expected
connection = _Connection(http=http)
batch = self._makeOne(connection)
response, content = batch._make_request('POST', URL, data={'foo': 1})
self.assertEqual(response.status, 204)
self.assertEqual(content, '')
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 10),
]
self.assertEqual(len(batch._requests), 1)
self.assertEqual(batch._requests[0][0], 'POST')
self.assertEqual(batch._requests[0][1], URL)
headers = batch._requests[0][2]
for key, value in EXPECTED_HEADERS:
self.assertEqual(headers[key], value)
self.assertEqual(batch._requests[0][3], {'foo': 1})
def test__make_request_PATCH_normal(self):
URL = 'http://example.com/api'
http = _HTTP() # no requests expected
connection = _Connection(http=http)
batch = self._makeOne(connection)
response, content = batch._make_request('PATCH', URL, data={'foo': 1})
self.assertEqual(response.status, 204)
self.assertEqual(content, '')
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 10),
]
self.assertEqual(len(batch._requests), 1)
self.assertEqual(batch._requests[0][0], 'PATCH')
self.assertEqual(batch._requests[0][1], URL)
headers = batch._requests[0][2]
for key, value in EXPECTED_HEADERS:
self.assertEqual(headers[key], value)
self.assertEqual(batch._requests[0][3], {'foo': 1})
def test__make_request_DELETE_normal(self):
URL = 'http://example.com/api'
http = _HTTP() # no requests expected
connection = _Connection(http=http)
batch = self._makeOne(connection)
response, content = batch._make_request('DELETE', URL)
self.assertEqual(response.status, 204)
self.assertEqual(content, '')
self.assertEqual(http._requests, [])
EXPECTED_HEADERS = [
('Accept-Encoding', 'gzip'),
('Content-Length', 0),
]
self.assertEqual(len(batch._requests), 1)
self.assertEqual(batch._requests[0][0], 'DELETE')
self.assertEqual(batch._requests[0][1], URL)
headers = batch._requests[0][2]
for key, value in EXPECTED_HEADERS:
self.assertEqual(headers[key], value)
self.assertEqual(batch._requests[0][3], None)
def test__make_request_POST_too_many_requests(self):
URL = 'http://example.com/api'
http = _HTTP() # no requests expected
connection = _Connection(http=http)
batch = self._makeOne(connection)
batch._MAX_BATCH_SIZE = 1
batch._requests.append(('POST', URL, {}, {'bar': 2}))
self.assertRaises(ValueError,
batch._make_request, 'POST', URL, data={'foo': 1})
self.assertTrue(connection.http is http)
def test_finish_empty(self):
http = _HTTP() # no requests expected
connection = _Connection(http=http)
batch = self._makeOne(connection)
self.assertRaises(ValueError, batch.finish)
self.assertTrue(connection.http is http)
def _check_subrequest_no_payload(self, chunk, method, url):
lines = chunk.splitlines()
# blank + 2 headers + blank + request + blank + blank
self.assertEqual(len(lines), 7)
self.assertEqual(lines[0], '')
self.assertEqual(lines[1], 'Content-Type: application/http')
self.assertEqual(lines[2], 'MIME-Version: 1.0')
self.assertEqual(lines[3], '')
self.assertEqual(lines[4], '%s %s HTTP/1.1' % (method, url))
self.assertEqual(lines[5], '')
self.assertEqual(lines[6], '')
def _check_subrequest_payload(self, chunk, method, url, payload):
import json
lines = chunk.splitlines()
# blank + 2 headers + blank + request + 2 headers + blank + body
payload_str = json.dumps(payload)
self.assertEqual(len(lines), 9)
self.assertEqual(lines[0], '')
self.assertEqual(lines[1], 'Content-Type: application/http')
self.assertEqual(lines[2], 'MIME-Version: 1.0')
self.assertEqual(lines[3], '')
self.assertEqual(lines[4], '%s %s HTTP/1.1' % (method, url))
self.assertEqual(lines[5], 'Content-Length: %d' % len(payload_str))
self.assertEqual(lines[6], 'Content-Type: application/json')
self.assertEqual(lines[7], '')
self.assertEqual(json.loads(lines[8]), payload)
def test_finish_nonempty(self):
URL = 'http://api.example.com/other_api'
expected = _Response()
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _THREE_PART_MIME_RESPONSE))
connection = _Connection(http=http)
batch = self._makeOne(connection)
batch.API_BASE_URL = 'http://api.example.com'
batch._requests.append(('POST', URL, {}, {'foo': 1, 'bar': 2}))
batch._requests.append(('PATCH', URL, {}, {'bar': 3}))
batch._requests.append(('DELETE', URL, {}, None))
result = batch.finish()
self.assertEqual(len(result), len(batch._requests))
self.assertEqual(result[0][0], '200')
self.assertEqual(result[0][1], 'OK')
self.assertEqual(result[0][2], {'foo': 1, 'bar': 2})
self.assertEqual(result[1][0], '200')
self.assertEqual(result[1][1], 'OK')
self.assertEqual(result[1][2], {'foo': 1, 'bar': 3})
self.assertEqual(result[2][0], '204')
self.assertEqual(result[2][1], 'No Content')
self.assertEqual(result[2][2], '')
self.assertEqual(len(http._requests), 1)
method, uri, headers, body = http._requests[0]
self.assertEqual(method, 'POST')
self.assertEqual(uri, 'http://api.example.com/batch')
self.assertEqual(len(headers), 2)
ctype, boundary = [x.strip()
for x in headers['Content-Type'].split(';')]
self.assertEqual(ctype, 'multipart/mixed')
self.assertTrue(boundary.startswith('boundary="=='))
self.assertTrue(boundary.endswith('=="'))
self.assertEqual(headers['MIME-Version'], '1.0')
divider = '--' + boundary[len('boundary="'):-1]
chunks = body.split(divider)[1:-1] # discard prolog / epilog
self.assertEqual(len(chunks), 3)
self._check_subrequest_payload(chunks[0], 'POST', URL,
{'foo': 1, 'bar': 2})
self._check_subrequest_payload(chunks[1], 'PATCH', URL, {'bar': 3})
self._check_subrequest_no_payload(chunks[2], 'DELETE', URL)
def test_finish_nonempty_non_multipart_response(self):
URL = 'http://api.example.com/other_api'
expected = _Response()
expected['content-type'] = 'text/plain'
http = _HTTP((expected, 'NOT A MIME_RESPONSE'))
connection = _Connection(http=http)
batch = self._makeOne(connection)
batch._requests.append(('POST', URL, {}, {'foo': 1, 'bar': 2}))
batch._requests.append(('PATCH', URL, {}, {'bar': 3}))
batch._requests.append(('DELETE', URL, {}, None))
self.assertRaises(ValueError, batch.finish)
def test_as_context_mgr_wo_error(self):
from gcloud.storage.batch import _BATCHES
URL = 'http://example.com/api'
expected = _Response()
expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="'
http = _HTTP((expected, _THREE_PART_MIME_RESPONSE))
connection = _Connection(http=http)
self.assertEqual(list(_BATCHES), [])
with self._makeOne(connection) as batch:
self.assertEqual(list(_BATCHES), [batch])
batch._make_request('POST', URL, {'foo': 1, 'bar': 2})
batch._make_request('PATCH', URL, {'bar': 3})
batch._make_request('DELETE', URL)
self.assertEqual(list(_BATCHES), [])
self.assertEqual(len(batch._requests), 3)
self.assertEqual(batch._requests[0][0], 'POST')
self.assertEqual(batch._requests[1][0], 'PATCH')
self.assertEqual(batch._requests[2][0], 'DELETE')
self.assertEqual(len(batch._responses), 3)
self.assertEqual(
batch._responses[0],
('200', 'OK', {'foo': 1, 'bar': 2}))
self.assertEqual(
batch._responses[1],
('200', 'OK', {'foo': 1, 'bar': 3}))
self.assertEqual(
batch._responses[2],
('204', 'No Content', ''))
def test_as_context_mgr_w_error(self):
from gcloud.storage.batch import _BATCHES
URL = 'http://example.com/api'
http = _HTTP()
connection = _Connection(http=http)
self.assertEqual(list(_BATCHES), [])
try:
with self._makeOne(connection) as batch:
self.assertEqual(list(_BATCHES), [batch])
batch._make_request('POST', URL, {'foo': 1, 'bar': 2})
batch._make_request('PATCH', URL, {'bar': 3})
batch._make_request('DELETE', URL)
raise ValueError()
except ValueError:
pass
self.assertEqual(list(_BATCHES), [])
self.assertEqual(len(http._requests), 0)
self.assertEqual(len(batch._requests), 3)
self.assertEqual(len(batch._responses), 0)
class Test__unpack_batch_response(unittest2.TestCase):
def _callFUT(self, response, content):
from gcloud.storage.batch import _unpack_batch_response
return _unpack_batch_response(response, content)
def test_bytes(self):
RESPONSE = {'content-type': b'multipart/mixed; boundary="DEADBEEF="'}
CONTENT = _THREE_PART_MIME_RESPONSE.encode('utf-8')
result = list(self._callFUT(RESPONSE, CONTENT))
self.assertEqual(len(result), 3)
self.assertEqual(result[0], ('200', 'OK', {u'bar': 2, u'foo': 1}))
self.assertEqual(result[1], ('200', 'OK', {u'foo': 1, u'bar': 3}))
self.assertEqual(result[2], ('204', 'No Content', ''))
def test_unicode(self):
import six
RESPONSE = {'content-type': u'multipart/mixed; boundary="DEADBEEF="'}
CONTENT = _THREE_PART_MIME_RESPONSE
if isinstance(CONTENT, six.binary_type): # pragma: NO COVER Python3
CONTENT = CONTENT.decode('utf-8')
result = list(self._callFUT(RESPONSE, CONTENT))
self.assertEqual(len(result), 3)
self.assertEqual(result[0], ('200', 'OK', {u'bar': 2, u'foo': 1}))
self.assertEqual(result[1], ('200', 'OK', {u'foo': 1, u'bar': 3}))
self.assertEqual(result[2], ('204', 'No Content', ''))
_THREE_PART_MIME_RESPONSE = """\
--DEADBEEF=
Content-Type: application/http
Content-ID: <response-8a09ca85-8d1d-4f45-9eb0-da8e8b07ec83+1>
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 20
{"foo": 1, "bar": 2}
--DEADBEEF=
Content-Type: application/http
Content-ID: <response-8a09ca85-8d1d-4f45-9eb0-da8e8b07ec83+2>
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 20
{"foo": 1, "bar": 3}
--DEADBEEF=
Content-Type: application/http
Content-ID: <response-8a09ca85-8d1d-4f45-9eb0-da8e8b07ec83+3>
HTTP/1.1 204 No Content
Content-Length: 0
--DEADBEEF=--
"""
class _Connection(object):
project = 'TESTING'
def __init__(self, **kw):
self.__dict__.update(kw)
def build_api_url(self, path, **_): # pragma: NO COVER
return 'http://api.example.com%s' % path
def _make_request(self, method, url, data=None, content_type=None,
headers=None):
if content_type is not None: # pragma: NO COVER
headers['Content-Type'] = content_type
return self.http.request(method, uri=url, headers=headers, body=data)
def api_request(self, method, path, query_params=None,
data=None, content_type=None,
api_base_url=None, api_version=None,
expect_json=True): # pragma: NO COVER
pass
class _Response(dict):
def __init__(self, status=200, **kw):
self.status = status
super(_Response, self).__init__(**kw)
class _HTTP(object):
def __init__(self, *responses):
self._requests = []
self._responses = list(responses)
def request(self, method, uri, headers, body):
self._requests.append((method, uri, headers, body))
response, self._responses = self._responses[0], self._responses[1:]
return response