|
1 | 1 | from functools import partial
|
2 |
| -from io import IOBase |
| 2 | +from io import IOBase, BytesIO |
3 | 3 | from numbers import Number
|
| 4 | +import os |
4 | 5 | from unittest.mock import MagicMock, Mock, PropertyMock, call, patch, ANY
|
5 | 6 | from requests.exceptions import RequestException, SSLError, ConnectionError as RequestsConnectionError
|
| 7 | +from requests_toolbelt import MultipartEncoder |
6 | 8 |
|
7 | 9 | import pytest
|
8 | 10 |
|
@@ -449,3 +451,48 @@ def test_proxy_malformed_dict_does_not_attach(box_session, monkeypatch, mock_net
|
449 | 451 |
|
450 | 452 | def test_proxy_network_config_property(box_session):
|
451 | 453 | assert isinstance(box_session.proxy_config, Proxy)
|
| 454 | + |
| 455 | + |
| 456 | +def test_multipart_request_with_disabled_streaming_file_content( |
| 457 | + box_session, mock_network_layer, generic_successful_response): |
| 458 | + test_url = 'https://example.com' |
| 459 | + file_bytes = os.urandom(1024) |
| 460 | + mock_network_layer.request.side_effect = [generic_successful_response] |
| 461 | + box_session.post( |
| 462 | + url=test_url, |
| 463 | + files={'file': ('unused', BytesIO(file_bytes))}, |
| 464 | + data={'attributes': '{"name": "test_file"}'}, |
| 465 | + stream_file_content=False |
| 466 | + ) |
| 467 | + mock_network_layer.request.assert_called_once_with( |
| 468 | + 'POST', |
| 469 | + test_url, |
| 470 | + access_token='fake_access_token', |
| 471 | + headers=ANY, |
| 472 | + log_response_content=True, |
| 473 | + files={'file': ('unused', ANY)}, |
| 474 | + data={'attributes': '{"name": "test_file"}'}, |
| 475 | + ) |
| 476 | + |
| 477 | + |
| 478 | +def test_multipart_request_with_enabled_streaming_file_content( |
| 479 | + box_session, mock_network_layer, generic_successful_response): |
| 480 | + test_url = 'https://example.com' |
| 481 | + file_bytes = os.urandom(1024) |
| 482 | + mock_network_layer.request.side_effect = [generic_successful_response] |
| 483 | + box_session.post( |
| 484 | + url=test_url, |
| 485 | + files={'file': ('unused', BytesIO(file_bytes))}, |
| 486 | + data={'attributes': '{"name": "test_file"}'}, |
| 487 | + stream_file_content=True |
| 488 | + ) |
| 489 | + call_args = mock_network_layer.request.call_args[0] |
| 490 | + call_kwargs = mock_network_layer.request.call_args[1] |
| 491 | + assert call_args[0] == 'POST' |
| 492 | + assert call_args[1] == test_url |
| 493 | + assert call_kwargs['access_token'] == 'fake_access_token' |
| 494 | + assert call_kwargs['log_response_content'] is True |
| 495 | + assert isinstance(call_kwargs['data'], MultipartEncoder) |
| 496 | + assert call_kwargs['data'].fields['attributes'] == '{"name": "test_file"}' |
| 497 | + assert call_kwargs['data'].fields['file'][0] == 'unused' |
| 498 | + assert isinstance(call_kwargs['data'].fields['file'][1], BytesIO) |
0 commit comments