Skip to content

Commit 94dcbcd

Browse files
authored
fix: Update exception file get download URL (#866)
* fix: Update exception file get download URL * fix: Update exception file get download URL
1 parent 2225327 commit 94dcbcd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

boxsdk/object/file.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from datetime import datetime
44
from typing import TYPE_CHECKING, Optional, Tuple, Union, IO, Iterable, List, Any
5+
from boxsdk.exception import BoxException
56

67
from boxsdk.util.datetime_formatter import normalize_date_to_rfc3339_format
78
from .item import Item
@@ -177,6 +178,9 @@ def get_download_url(self, file_version: Optional['FileVersion'] = None) -> str:
177178
expect_json_response=False,
178179
allow_redirects=False,
179180
)
181+
if 'location' not in box_response.headers:
182+
raise BoxException('Download URL is not present in the response.')
183+
180184
return box_response.headers['location']
181185

182186
@api_call

test/unit/object/test_file.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pytest_lazyfixture import lazy_fixture
99

1010
from boxsdk.config import API
11-
from boxsdk.exception import BoxAPIException
11+
from boxsdk.exception import BoxAPIException, BoxException
1212
from boxsdk.object.comment import Comment
1313
from boxsdk.object.file import File
1414
from boxsdk.object.file_version import FileVersion
@@ -237,6 +237,20 @@ def test_get_download_url(test_file, mock_box_session):
237237
assert url == download_url
238238

239239

240+
def test_get_download_url_failed(test_file, mock_box_session):
241+
expected_url = f'{API.BASE_API_URL}/files/{test_file.object_id}/content'
242+
mock_box_session.get.return_value.headers = {}
243+
with pytest.raises(BoxException) as exc_info:
244+
test_file.get_download_url()
245+
assert exc_info.value.args[0] == 'Download URL is not present in the response.'
246+
mock_box_session.get.assert_called_once_with(
247+
expected_url,
248+
params=None,
249+
expect_json_response=False,
250+
allow_redirects=False
251+
)
252+
253+
240254
def test_get_download_url_file_version(test_file, test_file_version, mock_box_session):
241255
expected_url = f'{API.BASE_API_URL}/files/{test_file.object_id}/content'
242256
download_url = 'https://dl.boxcloud.com/sdjhfgksdjfgshdbg'

0 commit comments

Comments
 (0)