|
8 | 8 | SSLError, |
9 | 9 | ConnectionError as RequestsConnectionError, |
10 | 10 | ) |
11 | | -from requests_toolbelt import MultipartEncoder |
12 | | - |
13 | 11 | import pytest |
14 | 12 |
|
15 | 13 | from boxsdk import CCGAuth |
|
19 | 17 | from boxsdk.network.default_network import DefaultNetwork, DefaultNetworkResponse |
20 | 18 | from boxsdk.session.box_response import BoxResponse |
21 | 19 | from boxsdk.session.session import Session, Translator, AuthorizedSession |
| 20 | +from boxsdk.util.multipart_stream import MultipartStream |
22 | 21 |
|
23 | 22 |
|
24 | 23 | @pytest.fixture(scope='function', params=[False, True]) |
@@ -265,7 +264,9 @@ def test_box_session_seeks_file_after_retry( |
265 | 264 | mock_file_2.tell.return_value = 3 |
266 | 265 | files = {'file': ('unused', mock_file_1), 'f2': ('unused', mock_file_2)} |
267 | 266 |
|
268 | | - box_response = box_session.post(url=test_url, files=files) |
| 267 | + # the multipart encoder sizes the streams itself, so only count the session's seeks |
| 268 | + with patch('boxsdk.session.session.MultipartStream'): |
| 269 | + box_response = box_session.post(url=test_url, files=files) |
269 | 270 | assert box_response.status_code == 200 |
270 | 271 | assert box_response.json() == generic_successful_response.json() |
271 | 272 | assert box_response.ok == generic_successful_response.ok |
@@ -645,7 +646,14 @@ def test_multipart_request_with_enabled_streaming_file_content( |
645 | 646 | assert call_args[1] == test_url |
646 | 647 | assert call_kwargs['access_token'] == 'fake_access_token' |
647 | 648 | assert call_kwargs['log_response_content'] is True |
648 | | - assert isinstance(call_kwargs['data'], MultipartEncoder) |
649 | | - assert call_kwargs['data'].fields['attributes'] == '{"name": "test_file"}' |
650 | | - assert call_kwargs['data'].fields['file'][0] == 'unused' |
651 | | - assert isinstance(call_kwargs['data'].fields['file'][1], BytesIO) |
| 649 | + multipart_stream = call_kwargs['data'] |
| 650 | + assert isinstance(multipart_stream, MultipartStream) |
| 651 | + assert call_kwargs['headers']['Content-Type'] == multipart_stream.content_type |
| 652 | + body = multipart_stream.read() |
| 653 | + assert ( |
| 654 | + b'name="attributes"\r\n\r\n{"name": "test_file"}\r\n' |
| 655 | + + f'--{multipart_stream.boundary}\r\n'.encode() |
| 656 | + + b'Content-Disposition: form-data; name="file"; filename="unused"\r\n\r\n' |
| 657 | + + file_bytes |
| 658 | + + b'\r\n' |
| 659 | + ) in body |
0 commit comments