Added Content-Range parsing tests.

#104
This commit is contained in:
Jakub Roztocil 2013-04-11 03:49:01 -03:00
parent 486657afa3
commit e53dcba03e
2 changed files with 35 additions and 9 deletions

View File

@ -59,18 +59,21 @@ def _parse_content_range(content_range, resumed_from):
# last-byte-pos value, is invalid. The recipient of an invalid
# byte-content-range- spec MUST ignore it and any content
# transferred along with it."
if (first_byte_pos <= last_byte_pos
and (instance_length is None
or instance_length <= last_byte_pos)):
if (first_byte_pos >= last_byte_pos
or (instance_length is not None
and instance_length <= last_byte_pos)):
raise ContentRangeError(
'Invalid Content-Range returned: %r' % content_range)
if (first_byte_pos != resumed_from
or (instance_length is None
or last_byte_pos + 1 != instance_length)):
or (instance_length is not None
and last_byte_pos + 1 != instance_length)):
# Not what we asked for.
raise ContentRangeError(
'Unexpected Content-Range returned: %r' % content_range)
'Unexpected Content-Range returned (%r)'
' for the requested Range ("bytes=%d")'
% (content_range, resumed_from)
)
return last_byte_pos + 1

View File

@ -62,7 +62,7 @@ from httpie.core import main
from httpie.output import BINARY_SUPPRESSED_NOTICE
from httpie.input import ParseError
from httpie.compat import is_windows, is_py26, bytes, str
from httpie.downloads import _parse_content_range, ContentRangeError
CRLF = '\r\n'
HTTPBIN_URL = os.environ.get('HTTPBIN_URL',
@ -1421,9 +1421,32 @@ class SessionTest(BaseTestCase):
class DownloadsTest(BaseTestCase):
# TODO: tests for downloads
pass
# TODO: Download tests
def test_Content_Range_parsing(self):
self.assertEqual(_parse_content_range('bytes 100-199/200', 100), 200)
self.assertEqual(_parse_content_range('bytes 100-199/*', 100), 200)
with self.assertRaises(ContentRangeError):
# syntax error
_parse_content_range('beers 100-199/*', 100)
with self.assertRaises(ContentRangeError):
# unexpected range
_parse_content_range('bytes 100-199/*', 99)
with self.assertRaises(ContentRangeError):
# invalid instance-length
_parse_content_range('bytes 100-199/199', 100)
with self.assertRaises(ContentRangeError):
# invalid byte-range-resp-spec
_parse_content_range('bytes 100-99/199', 100)
with self.assertRaises(ContentRangeError):
# invalid byte-range-resp-spec
_parse_content_range('bytes 100-100/*', 100)
if __name__ == '__main__':
unittest.main()