diff options
Diffstat (limited to 'Lib/test/test_urllib2net.py')
| -rw-r--r-- | Lib/test/test_urllib2net.py | 123 |
1 files changed, 46 insertions, 77 deletions
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index f618ecf..17f9d1b 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -7,10 +7,8 @@ import socket import urllib.error import urllib.request import sys -try: - import ssl -except ImportError: - ssl = None + +support.requires("network") TIMEOUT = 60 # seconds @@ -81,7 +79,7 @@ class CloseSocketTest(unittest.TestCase): def test_close(self): # calling .close() on urllib2's response objects should close the # underlying socket - url = "http://www.python.org/" + url = "http://www.example.com/" with support.transient_internet(url): response = _urlopen_with_retry(url) sock = response.fp @@ -101,11 +99,9 @@ class OtherNetworkTests(unittest.TestCase): def test_ftp(self): urls = [ - 'ftp://ftp.kernel.org/pub/linux/kernel/README', - 'ftp://ftp.kernel.org/pub/linux/kernel/non-existent-file', - #'ftp://ftp.kernel.org/pub/leenox/kernel/test', - 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC' - '/research-reports/00README-Legal-Rules-Regs', + 'ftp://ftp.debian.org/debian/README', + ('ftp://ftp.debian.org/debian/non-existent-file', + None, urllib.error.URLError), ] self._test_urls(urls, self._extra_handlers()) @@ -162,6 +158,14 @@ class OtherNetworkTests(unittest.TestCase): self.assertEqual(res.geturl(), "http://www.pythontest.net/index.html#frag") + def test_redirect_url_withfrag(self): + redirect_url_with_frag = "http://www.pythontest.net/redir/with_frag/" + with support.transient_internet(redirect_url_with_frag): + req = urllib.request.Request(redirect_url_with_frag) + res = urllib.request.urlopen(req) + self.assertEqual(res.geturl(), + "http://www.pythontest.net/elsewhere/#frag") + def test_custom_headers(self): url = "http://www.example.com" with support.transient_internet(url): @@ -205,39 +209,34 @@ class OtherNetworkTests(unittest.TestCase): urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError) for url in urls: - if isinstance(url, tuple): - url, req, expected_err = url - else: - req = expected_err = None - - with support.transient_internet(url): - debug(url) - try: - f = urlopen(url, req, TIMEOUT) - except EnvironmentError as err: - debug(err) - if expected_err: - msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % - (expected_err, url, req, type(err), err)) - self.assertIsInstance(err, expected_err, msg) - except urllib.error.URLError as err: - if isinstance(err[0], socket.timeout): - print("<timeout: %s>" % url, file=sys.stderr) - continue - else: - raise + with self.subTest(url=url): + if isinstance(url, tuple): + url, req, expected_err = url else: + req = expected_err = None + + with support.transient_internet(url): try: - with support.time_out, \ - support.socket_peer_reset, \ - support.ioerror_peer_reset: - buf = f.read() - debug("read %d bytes" % len(buf)) - except socket.timeout: - print("<timeout: %s>" % url, file=sys.stderr) - f.close() - debug("******** next url coming up...") - time.sleep(0.1) + f = urlopen(url, req, TIMEOUT) + # urllib.error.URLError is a subclass of OSError + except OSError as err: + if expected_err: + msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % + (expected_err, url, req, type(err), err)) + self.assertIsInstance(err, expected_err, msg) + else: + raise + else: + try: + with support.time_out, \ + support.socket_peer_reset, \ + support.ioerror_peer_reset: + buf = f.read() + debug("read %d bytes" % len(buf)) + except socket.timeout: + print("<timeout: %s>" % url, file=sys.stderr) + f.close() + time.sleep(0.1) def _extra_handlers(self): handlers = [] @@ -253,7 +252,7 @@ class OtherNetworkTests(unittest.TestCase): class TimeoutTest(unittest.TestCase): def test_http_basic(self): self.assertIsNone(socket.getdefaulttimeout()) - url = "http://www.python.org" + url = "http://www.example.com" with support.transient_internet(url, timeout=None): u = _urlopen_with_retry(url) self.addCleanup(u.close) @@ -261,7 +260,7 @@ class TimeoutTest(unittest.TestCase): def test_http_default_timeout(self): self.assertIsNone(socket.getdefaulttimeout()) - url = "http://www.python.org" + url = "http://www.example.com" with support.transient_internet(url): socket.setdefaulttimeout(60) try: @@ -273,7 +272,7 @@ class TimeoutTest(unittest.TestCase): def test_http_no_timeout(self): self.assertIsNone(socket.getdefaulttimeout()) - url = "http://www.python.org" + url = "http://www.example.com" with support.transient_internet(url): socket.setdefaulttimeout(60) try: @@ -284,13 +283,13 @@ class TimeoutTest(unittest.TestCase): self.assertIsNone(u.fp.raw._sock.gettimeout()) def test_http_timeout(self): - url = "http://www.python.org" + url = "http://www.example.com" with support.transient_internet(url): u = _urlopen_with_retry(url, timeout=120) self.addCleanup(u.close) self.assertEqual(u.fp.raw._sock.gettimeout(), 120) - FTP_HOST = "ftp://ftp.mirror.nl/pub/gnu/" + FTP_HOST = 'ftp://ftp.debian.org/debian/' def test_ftp_basic(self): self.assertIsNone(socket.getdefaulttimeout()) @@ -328,35 +327,5 @@ class TimeoutTest(unittest.TestCase): self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60) -@unittest.skipUnless(ssl, "requires SSL support") -class HTTPSTests(unittest.TestCase): - - def test_sni(self): - self.skipTest("test disabled - test server needed") - # Checks that Server Name Indication works, if supported by the - # OpenSSL linked to. - # The ssl module itself doesn't have server-side support for SNI, - # so we rely on a third-party test site. - expect_sni = ssl.HAS_SNI - with support.transient_internet("XXX"): - u = urllib.request.urlopen("XXX") - contents = u.readall() - if expect_sni: - self.assertIn(b"Great", contents) - self.assertNotIn(b"Unfortunately", contents) - else: - self.assertNotIn(b"Great", contents) - self.assertIn(b"Unfortunately", contents) - - -def test_main(): - support.requires("network") - support.run_unittest(AuthTests, - HTTPSTests, - OtherNetworkTests, - CloseSocketTest, - TimeoutTest, - ) - if __name__ == "__main__": - test_main() + unittest.main() |
