Skip to content

Commit aab7c07

Browse files
committed
test: update test_urllib
1 parent 27b637a commit aab7c07

File tree

1 file changed

+63
-87
lines changed

1 file changed

+63
-87
lines changed

Lib/test/test_urllib.py

Lines changed: 63 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def setUp(self):
152152
finally:
153153
f.close()
154154
self.pathname = os_helper.TESTFN
155-
self.returned_obj = urlopen("file:%s" % self.pathname)
155+
self.quoted_pathname = urllib.parse.quote(self.pathname)
156+
self.returned_obj = urlopen("file:%s" % self.quoted_pathname)
156157

157158
def tearDown(self):
158159
"""Shut down the open object"""
@@ -195,11 +196,21 @@ def test_close(self):
195196
# by the tearDown() method for the test
196197
self.returned_obj.close()
197198

199+
def test_headers(self):
200+
self.assertIsInstance(self.returned_obj.headers, email.message.Message)
201+
202+
def test_url(self):
203+
self.assertEqual(self.returned_obj.url, self.quoted_pathname)
204+
205+
@unittest.skip("TODO: RUSTPYTHON (AttributeError: 'BufferedReader' object has no attribute 'status')")
206+
def test_status(self):
207+
self.assertIsNone(self.returned_obj.status)
208+
198209
def test_info(self):
199210
self.assertIsInstance(self.returned_obj.info(), email.message.Message)
200211

201212
def test_geturl(self):
202-
self.assertEqual(self.returned_obj.geturl(), self.pathname)
213+
self.assertEqual(self.returned_obj.geturl(), self.quoted_pathname)
203214

204215
def test_getcode(self):
205216
self.assertIsNone(self.returned_obj.getcode())
@@ -609,6 +620,9 @@ class urlopen_DataTests(unittest.TestCase):
609620
"""Test urlopen() opening a data URL."""
610621

611622
def setUp(self):
623+
# clear _opener global variable
624+
self.addCleanup(urllib.request.urlcleanup)
625+
612626
# text containing URL special- and unicode-characters
613627
self.text = "test data URLs :;,%=& \u00f6 \u00c4 "
614628
# 2x1 pixel RGB PNG image with one black and one white pixel
@@ -683,6 +697,9 @@ class urlretrieve_FileTests(unittest.TestCase):
683697
"""Test urllib.urlretrieve() on local files"""
684698

685699
def setUp(self):
700+
# clear _opener global variable
701+
self.addCleanup(urllib.request.urlcleanup)
702+
686703
# Create a list of temporary files. Each item in the list is a file
687704
# name (absolute path or relative to the current working directory).
688705
# All files in this list will be deleted in the tearDown method. Note,
@@ -823,6 +840,8 @@ class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
823840
"""Test urllib.urlretrieve() using fake http connections"""
824841

825842
def test_short_content_raises_ContentTooShortError(self):
843+
self.addCleanup(urllib.request.urlcleanup)
844+
826845
self.fakehttp(b'''HTTP/1.1 200 OK
827846
Date: Wed, 02 Jan 2008 03:03:54 GMT
828847
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
@@ -844,6 +863,8 @@ def _reporthook(par1, par2, par3):
844863
self.unfakehttp()
845864

846865
def test_short_content_raises_ContentTooShortError_without_reporthook(self):
866+
self.addCleanup(urllib.request.urlcleanup)
867+
847868
self.fakehttp(b'''HTTP/1.1 200 OK
848869
Date: Wed, 02 Jan 2008 03:03:54 GMT
849870
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
@@ -1094,8 +1115,6 @@ def test_unquoting(self):
10941115
"%s" % result)
10951116
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
10961117
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
1097-
with support.check_warnings(('', BytesWarning), quiet=True):
1098-
self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')
10991118

11001119
def test_unquoting_badpercent(self):
11011120
# Test unquoting on bad percent-escapes
@@ -1255,11 +1274,29 @@ def test_unquote_with_unicode(self):
12551274
self.assertEqual(expect, result,
12561275
"using unquote(): %r != %r" % (expect, result))
12571276

1277+
@unittest.skip("TODO: RUSTPYTHON (TypeError: Expected str, got bytes)")
12581278
def test_unquoting_with_bytes_input(self):
1259-
# Bytes not supported yet
1260-
with self.assertRaisesRegex(TypeError, 'Expected str, got bytes'):
1261-
given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
1262-
urllib.parse.unquote(given)
1279+
# ASCII characters decoded to a string
1280+
given = b'blueberryjam'
1281+
expect = 'blueberryjam'
1282+
result = urllib.parse.unquote(given)
1283+
self.assertEqual(expect, result,
1284+
"using unquote(): %r != %r" % (expect, result))
1285+
1286+
# A mix of non-ASCII hex-encoded characters and ASCII characters
1287+
given = b'bl\xc3\xa5b\xc3\xa6rsyltet\xc3\xb8y'
1288+
expect = 'bl\u00e5b\u00e6rsyltet\u00f8y'
1289+
result = urllib.parse.unquote(given)
1290+
self.assertEqual(expect, result,
1291+
"using unquote(): %r != %r" % (expect, result))
1292+
1293+
# A mix of non-ASCII percent-encoded characters and ASCII characters
1294+
given = b'bl%c3%a5b%c3%a6rsyltet%c3%b8j'
1295+
expect = 'bl\u00e5b\u00e6rsyltet\u00f8j'
1296+
result = urllib.parse.unquote(given)
1297+
self.assertEqual(expect, result,
1298+
"using unquote(): %r != %r" % (expect, result))
1299+
12631300

12641301
class urlencode_Tests(unittest.TestCase):
12651302
"""Tests for urlencode()"""
@@ -1499,6 +1536,24 @@ def test_quoting(self):
14991536
"url2pathname() failed; %s != %s" %
15001537
(expect, result))
15011538

1539+
@unittest.skipUnless(sys.platform == 'win32',
1540+
'test specific to the nturl2path functions.')
1541+
def test_prefixes(self):
1542+
# Test special prefixes are correctly handled in pathname2url()
1543+
given = '\\\\?\\C:\\dir'
1544+
expect = '///C:/dir'
1545+
result = urllib.request.pathname2url(given)
1546+
self.assertEqual(expect, result,
1547+
"pathname2url() failed; %s != %s" %
1548+
(expect, result))
1549+
given = '\\\\?\\unc\\server\\share\\dir'
1550+
expect = '/server/share/dir'
1551+
result = urllib.request.pathname2url(given)
1552+
self.assertEqual(expect, result,
1553+
"pathname2url() failed; %s != %s" %
1554+
(expect, result))
1555+
1556+
15021557
@unittest.skipUnless(sys.platform == 'win32',
15031558
'test specific to the urllib.url2path function.')
15041559
def test_ntpath(self):
@@ -1574,85 +1629,6 @@ def open_local_file(self, url):
15741629
self.assertRaises(OSError, DummyURLopener().retrieve, url)
15751630

15761631

1577-
# Just commented them out.
1578-
# Can't really tell why keep failing in windows and sparc.
1579-
# Everywhere else they work ok, but on those machines, sometimes
1580-
# fail in one of the tests, sometimes in other. I have a linux, and
1581-
# the tests go ok.
1582-
# If anybody has one of the problematic environments, please help!
1583-
# . Facundo
1584-
#
1585-
# def server(evt):
1586-
# import socket, time
1587-
# serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1588-
# serv.settimeout(3)
1589-
# serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1590-
# serv.bind(("", 9093))
1591-
# serv.listen()
1592-
# try:
1593-
# conn, addr = serv.accept()
1594-
# conn.send("1 Hola mundo\n")
1595-
# cantdata = 0
1596-
# while cantdata < 13:
1597-
# data = conn.recv(13-cantdata)
1598-
# cantdata += len(data)
1599-
# time.sleep(.3)
1600-
# conn.send("2 No more lines\n")
1601-
# conn.close()
1602-
# except socket.timeout:
1603-
# pass
1604-
# finally:
1605-
# serv.close()
1606-
# evt.set()
1607-
#
1608-
# class FTPWrapperTests(unittest.TestCase):
1609-
#
1610-
# def setUp(self):
1611-
# import ftplib, time, threading
1612-
# ftplib.FTP.port = 9093
1613-
# self.evt = threading.Event()
1614-
# threading.Thread(target=server, args=(self.evt,)).start()
1615-
# time.sleep(.1)
1616-
#
1617-
# def tearDown(self):
1618-
# self.evt.wait()
1619-
#
1620-
# def testBasic(self):
1621-
# # connects
1622-
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1623-
# ftp.close()
1624-
#
1625-
# def testTimeoutNone(self):
1626-
# # global default timeout is ignored
1627-
# import socket
1628-
# self.assertIsNone(socket.getdefaulttimeout())
1629-
# socket.setdefaulttimeout(30)
1630-
# try:
1631-
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1632-
# finally:
1633-
# socket.setdefaulttimeout(None)
1634-
# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1635-
# ftp.close()
1636-
#
1637-
# def testTimeoutDefault(self):
1638-
# # global default timeout is used
1639-
# import socket
1640-
# self.assertIsNone(socket.getdefaulttimeout())
1641-
# socket.setdefaulttimeout(30)
1642-
# try:
1643-
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
1644-
# finally:
1645-
# socket.setdefaulttimeout(None)
1646-
# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1647-
# ftp.close()
1648-
#
1649-
# def testTimeoutValue(self):
1650-
# ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
1651-
# timeout=30)
1652-
# self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
1653-
# ftp.close()
1654-
1655-
16561632
class RequestTests(unittest.TestCase):
16571633
"""Unit tests for urllib.request.Request."""
16581634

0 commit comments

Comments
 (0)