]> granicus.if.org Git - python/commitdiff
bpo-36019: Use pythontest.net instead of example.com in network tests (GH-11941)
authorStéphane Wirtel <stephane@wirtel.be>
Fri, 22 Feb 2019 13:45:36 +0000 (14:45 +0100)
committerVictor Stinner <vstinner@redhat.com>
Fri, 22 Feb 2019 13:45:36 +0000 (14:45 +0100)
Doc/library/test.rst
Lib/test/support/__init__.py
Lib/test/test_urllib.py
Lib/test/test_urllib2net.py
Lib/test/test_urllibnet.py
Misc/NEWS.d/next/Tests/2019-02-21-14-23-51.bpo-36019.zS_OUi.rst [new file with mode: 0644]

index b5057c0ace562ce43294fdbc80c6a1a45d158568..054521dcc5d3ebaf40cf7d5a97e38ba90ddbfc5c 100644 (file)
@@ -356,6 +356,10 @@ The :mod:`test.support` module defines the following constants:
 
    Check for presence of docstrings.
 
+.. data:: TEST_HTTP_URL
+
+   Define the URL of a dedicated HTTP server for the network tests.
+
 
 
 The :mod:`test.support` module defines the following functions:
index 9b75a211bea02f9de51938acf2cb8e5a49509dfe..c0938d90010a99b049e28af1f3592feb64d0ff9a 100644 (file)
@@ -835,6 +835,10 @@ else:
 # module name.
 TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
 
+# Define the URL of a dedicated HTTP server for the network tests.
+# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
+TEST_HTTP_URL = "http://www.pythontest.net"
+
 # FS_NONASCII: non-ASCII character encodable by os.fsencode(),
 # or None if there is no such character.
 FS_NONASCII = None
index c292d74f84a93c12fadc03c99beb09d7c76f3b9f..2ac73b58d832064f8328d6d06eb90135056c7c59 100644 (file)
@@ -712,7 +712,7 @@ FF
 
         with self.assertRaises(urllib.error.ContentTooShortError):
             try:
-                urllib.request.urlretrieve('http://example.com/',
+                urllib.request.urlretrieve(support.TEST_HTTP_URL,
                                            reporthook=_reporthook)
             finally:
                 self.unfakehttp()
@@ -729,7 +729,7 @@ FF
 ''')
         with self.assertRaises(urllib.error.ContentTooShortError):
             try:
-                urllib.request.urlretrieve('http://example.com/')
+                urllib.request.urlretrieve(support.TEST_HTTP_URL)
             finally:
                 self.unfakehttp()
 
index 1aa64cbee1ca4b562dc78642522b4b32f1669939..0f43d71ea67b55078a547db02f9424190f545acf 100644 (file)
@@ -84,7 +84,7 @@ class CloseSocketTest(unittest.TestCase):
     def test_close(self):
         # calling .close() on urllib2's response objects should close the
         # underlying socket
-        url = "http://www.example.com/"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url):
             response = _urlopen_with_retry(url)
             sock = response.fp
@@ -173,7 +173,7 @@ class OtherNetworkTests(unittest.TestCase):
                     "http://www.pythontest.net/elsewhere/#frag")
 
     def test_custom_headers(self):
-        url = "http://www.example.com"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url):
             opener = urllib.request.build_opener()
             request = urllib.request.Request(url)
@@ -259,7 +259,7 @@ class OtherNetworkTests(unittest.TestCase):
 class TimeoutTest(unittest.TestCase):
     def test_http_basic(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url, timeout=None):
             u = _urlopen_with_retry(url)
             self.addCleanup(u.close)
@@ -267,7 +267,7 @@ class TimeoutTest(unittest.TestCase):
 
     def test_http_default_timeout(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url):
             socket.setdefaulttimeout(60)
             try:
@@ -279,7 +279,7 @@ class TimeoutTest(unittest.TestCase):
 
     def test_http_no_timeout(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url):
             socket.setdefaulttimeout(60)
             try:
@@ -290,7 +290,7 @@ class TimeoutTest(unittest.TestCase):
             self.assertIsNone(u.fp.raw._sock.gettimeout())
 
     def test_http_timeout(self):
-        url = "http://www.example.com"
+        url = support.TEST_HTTP_URL
         with support.transient_internet(url):
             u = _urlopen_with_retry(url, timeout=120)
             self.addCleanup(u.close)
index 4103b6c07505d53bd88db487508dba9f207d75a7..d394ceddd080e7d4ef696df397b3a5950fa1f644 100644 (file)
@@ -3,6 +3,7 @@ from test import support
 
 import contextlib
 import socket
+import urllib.parse
 import urllib.request
 import os
 import email.message
@@ -24,8 +25,9 @@ class URLTimeoutTest(unittest.TestCase):
         socket.setdefaulttimeout(None)
 
     def testURLread(self):
-        with support.transient_internet("www.example.com"):
-            f = urllib.request.urlopen("http://www.example.com/")
+        domain = urllib.parse.urlparse(support.TEST_HTTP_URL).netloc
+        with support.transient_internet(domain):
+            f = urllib.request.urlopen(support.TEST_HTTP_URL)
             f.read()
 
 
diff --git a/Misc/NEWS.d/next/Tests/2019-02-21-14-23-51.bpo-36019.zS_OUi.rst b/Misc/NEWS.d/next/Tests/2019-02-21-14-23-51.bpo-36019.zS_OUi.rst
new file mode 100644 (file)
index 0000000..b14d157
--- /dev/null
@@ -0,0 +1,2 @@
+Add test.support.TEST_HTTP_URL and replace references of http://www.example.com
+by this new constant. Contributed by Stéphane Wirtel.