[Python-checkins] bpo-36019: Use pythontest.net instead of example.com in network tests (GH-11941)

Miss Islington (bot) webhook-mailer at python.org
Fri Feb 22 09:03:35 EST 2019


https://github.com/python/cpython/commit/6163210089148ad31c270695f7273fc3561a211a
commit: 6163210089148ad31c270695f7273fc3561a211a
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-02-22T06:03:26-08:00
summary:

bpo-36019: Use pythontest.net instead of example.com in network tests (GH-11941)

(cherry picked from commit a40681dd5db8deaf05a635eecb91498dac882aa4)

Co-authored-by: Stéphane Wirtel <stephane at wirtel.be>

files:
A Misc/NEWS.d/next/Tests/2019-02-21-14-23-51.bpo-36019.zS_OUi.rst
M Doc/library/test.rst
M Lib/test/support/__init__.py
M Lib/test/test_urllib.py
M Lib/test/test_urllib2net.py
M Lib/test/test_urllibnet.py

diff --git a/Doc/library/test.rst b/Doc/library/test.rst
index 95d7f54ba442..de79cdfc5fa9 100644
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -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:
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index a9cfa2a39540..9d6fd44cbc10 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -815,6 +815,10 @@ def dec(*args, **kwargs):
 # 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
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index c292d74f84a9..2ac73b58d832 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -712,7 +712,7 @@ def _reporthook(par1, par2, par3):
 
         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 @@ def test_short_content_raises_ContentTooShortError_without_reporthook(self):
 ''')
         with self.assertRaises(urllib.error.ContentTooShortError):
             try:
-                urllib.request.urlretrieve('http://example.com/')
+                urllib.request.urlretrieve(support.TEST_HTTP_URL)
             finally:
                 self.unfakehttp()
 
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 1aa64cbee1ca..0f43d71ea67b 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -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 @@ def test_redirect_url_withfrag(self):
                     "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 @@ def _extra_handlers(self):
 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 @@ def test_http_basic(self):
 
     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 @@ def test_http_default_timeout(self):
 
     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 @@ def test_http_no_timeout(self):
             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)
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index 4103b6c07505..d394ceddd080 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -3,6 +3,7 @@
 
 import contextlib
 import socket
+import urllib.parse
 import urllib.request
 import os
 import email.message
@@ -24,8 +25,9 @@ def tearDown(self):
         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
index 000000000000..b14d157ff492
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-02-21-14-23-51.bpo-36019.zS_OUi.rst
@@ -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.



More information about the Python-checkins mailing list