cpython: 2ca415cbf2ac (original) (raw)
--- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -36,6 +36,44 @@ def urlopen(url, data=None, proxies=None else: return opener.open(url, data) + +class FakeHTTPMixin(object):
def sendall(self, str):[](#l1.13)
pass[](#l1.14)
def makefile(self, *args, **kwds):[](#l1.16)
self.io_refs += 1[](#l1.17)
return self[](#l1.18)
def read(self, amt=None):[](#l1.20)
if self.closed:[](#l1.21)
return b""[](#l1.22)
return io.BytesIO.read(self, amt)[](#l1.23)
def readline(self, length=None):[](#l1.25)
if self.closed:[](#l1.26)
return b""[](#l1.27)
return io.BytesIO.readline(self, length)[](#l1.28)
def close(self):[](#l1.30)
self.io_refs -= 1[](#l1.31)
if self.io_refs == 0:[](#l1.32)
io.BytesIO.close(self)[](#l1.33)
class FakeHTTPConnection(http.client.HTTPConnection):[](#l1.35)
def connect(self):[](#l1.36)
self.sock = FakeSocket(fakedata)[](#l1.37)
self._connection_class = http.client.HTTPConnection[](#l1.38)
http.client.HTTPConnection = FakeHTTPConnection[](#l1.39)
+ + class urlopen_FileTests(unittest.TestCase): """Test urlopen() opening a temporary file. @@ -139,35 +177,9 @@ class ProxyTests(unittest.TestCase): self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com') self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com')) -class urlopen_HttpTests(unittest.TestCase): +class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin): """Test urlopen() opening a fake http connection."""
- def fakehttp(self, fakedata):
class FakeSocket(io.BytesIO):[](#l1.57)
io_refs = 1[](#l1.58)
def sendall(self, str): pass[](#l1.59)
def makefile(self, *args, **kwds):[](#l1.60)
self.io_refs += 1[](#l1.61)
return self[](#l1.62)
def read(self, amt=None):[](#l1.63)
if self.closed: return b""[](#l1.64)
return io.BytesIO.read(self, amt)[](#l1.65)
def readline(self, length=None):[](#l1.66)
if self.closed: return b""[](#l1.67)
return io.BytesIO.readline(self, length)[](#l1.68)
def close(self):[](#l1.69)
self.io_refs -= 1[](#l1.70)
if self.io_refs == 0:[](#l1.71)
io.BytesIO.close(self)[](#l1.72)
class FakeHTTPConnection(http.client.HTTPConnection):[](#l1.73)
def connect(self):[](#l1.74)
self.sock = FakeSocket(fakedata)[](#l1.75)
self._connection_class = http.client.HTTPConnection[](#l1.76)
http.client.HTTPConnection = FakeHTTPConnection[](#l1.77)
- def check_read(self, ver): self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!") try: @@ -394,6 +406,48 @@ class urlretrieve_FileTests(unittest.Tes self.assertEqual(report[0][1], 8192) self.assertEqual(report[0][2], 8193) + +class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
- def test_short_content_raises_ContentTooShortError(self):
self.fakehttp(b'''HTTP/1.1 200 OK[](#l1.94)
+Date: Wed, 02 Jan 2008 03:03:54 GMT +Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e +Connection: close +Content-Length: 100 +Content-Type: text/html; charset=iso-8859-1 + +FF +''') +
def _reporthook(par1, par2, par3):[](#l1.104)
pass[](#l1.105)
with self.assertRaises(urllib.error.ContentTooShortError):[](#l1.107)
try:[](#l1.108)
urllib.request.urlretrieve('http://example.com/',[](#l1.109)
reporthook=_reporthook)[](#l1.110)
finally:[](#l1.111)
self.unfakehttp()[](#l1.112)
- def test_short_content_raises_ContentTooShortError_without_reporthook(self):
self.fakehttp(b'''HTTP/1.1 200 OK[](#l1.115)
+Date: Wed, 02 Jan 2008 03:03:54 GMT +Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e +Connection: close +Content-Length: 100 +Content-Type: text/html; charset=iso-8859-1 + +FF +''')
with self.assertRaises(urllib.error.ContentTooShortError):[](#l1.124)
try:[](#l1.125)
urllib.request.urlretrieve('http://example.com/')[](#l1.126)
finally:[](#l1.127)
self.unfakehttp()[](#l1.128)
+ + class QuotingTests(unittest.TestCase): """Tests for urllib.quote() and urllib.quote_plus() @@ -1164,6 +1218,7 @@ def test_main(): urlopen_FileTests, urlopen_HttpTests, urlretrieve_FileTests,
urlretrieve_HttpTests,[](#l1.138) ProxyTests,[](#l1.139) QuotingTests,[](#l1.140) UnquotingTests,[](#l1.141)
--- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1589,9 +1589,9 @@ class URLopener: size = -1 read = 0 blocknum = 0
if "content-length" in headers:[](#l2.7)
size = int(headers["Content-Length"])[](#l2.8) if reporthook:[](#l2.9)
if "content-length" in headers:[](#l2.10)
size = int(headers["Content-Length"])[](#l2.11) reporthook(blocknum, bs, size)[](#l2.12) while 1:[](#l2.13) block = fp.read(bs)[](#l2.14)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -66,6 +66,9 @@ Core and Builtins Library ------- +- Issue 10817: Fix urlretrieve function to raise ContentTooShortError even