diff --git a/httpie/downloads.py b/httpie/downloads.py index cf9bf44c..5d6ab50e 100644 --- a/httpie/downloads.py +++ b/httpie/downloads.py @@ -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 diff --git a/tests/tests.py b/tests/tests.py index 0b1b9939..913d340a 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -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()