Skip to content

Commit a0be1dd

Browse files
authored
Merge pull request gtsystem#4 from gtsystem/range_required
Explicit failure in case range request is not supported
2 parents 6df73d3 + d8301c4 commit a0be1dd

5 files changed

Lines changed: 28 additions & 6 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ python:
88
- "3.8"
99

1010
# command to install dependencies
11-
install: "python setup.py install"
11+
install:
12+
- pip install requests-mock
13+
- python setup.py install
1214
# command to run tests
1315
script: py.test

bin/remotezip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from remotezip import RemoteZip
66

77

88
def list_files(url, filenames):
9-
with RemoteZip(url) as zip:
9+
with RemoteZip(url, headers={'User-Agent': 'remotezip'}) as zip:
1010
if len(filenames) == 0:
1111
filenames = zip.namelist()
1212
data = [('Length', 'DateTime', 'Name')]

remotezip.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
__all__ = ['RemoteIOError', 'RemoteZip']
77

8+
class RemoteZipError(Exception):
9+
pass
10+
11+
class OutOfBound(RemoteZipError):
12+
pass
13+
814

9-
class OutOfBound(Exception):
15+
class RemoteIOError(RemoteZipError):
1016
pass
1117

1218

13-
class RemoteIOError(Exception):
19+
class RangeNotSupported(RemoteZipError):
1420
pass
1521

1622

@@ -160,6 +166,8 @@ def request(url, range_header, kwargs):
160166
headers['Range'] = range_header
161167
res = requests.get(url, stream=True, **kwargs)
162168
res.raise_for_status()
169+
if 'Content-Range' not in res.headers:
170+
raise RangeNotSupported("The server doesn't support range requests")
163171
return res.raw, res.headers
164172

165173
def fetch_fun(self, data_range, stream=False):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='remotezip',
8-
version='0.9.2',
8+
version='0.9.3',
99
author='Giuseppe Tribulato',
1010
author_email='[email protected]',
1111
py_modules=['remotezip'],

test_remotezip.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shutil
66
import io
77

8+
import requests_mock
9+
810
import remotezip as rz
911

1012

@@ -41,6 +43,10 @@ def fetch_fun(self, data_range, stream=False):
4143

4244

4345
class TestPartialBuffer(unittest.TestCase):
46+
def setUp(self):
47+
if not hasattr(self, 'assertRaisesRegex'):
48+
self.assertRaisesRegex = self.assertRaisesRegexp
49+
4450
def verify(self, stream):
4551
pb = rz.PartialBuffer(io.BytesIO(b'aaaabbcccdd'), 10, 11, stream=stream)
4652
self.assertEqual(pb.position, 10)
@@ -89,7 +95,7 @@ def test_stream_forward_seek(self):
8995
self.assertEqual(pb.seek(2, 1), 17)
9096
self.assertEqual(pb.read(), b'ccdd')
9197

92-
with self.assertRaisesRegexp(rz.OutOfBound, "Negative seek not supported"):
98+
with self.assertRaisesRegex(rz.OutOfBound, "Negative seek not supported"):
9399
pb.seek(12, 0)
94100
self.assertEqual(pb.position, 12)
95101

@@ -251,6 +257,12 @@ def test_make_header(self):
251257
header = LocalRemoteZip.make_header(-123, None)
252258
self.assertEqual(header, 'bytes=-123')
253259

260+
def test_range_not_supported(self):
261+
with requests_mock.Mocker() as m:
262+
m.get("http://test.com/file.zip")
263+
with self.assertRaises(rz.RangeNotSupported):
264+
rz.RemoteZip("http://test.com/file.zip")
265+
254266
# TODO: test get_position2size
255267

256268
if __name__ == '__main__':

0 commit comments

Comments
 (0)