[Python-checkins] [2.7] bpo-36019: Use pythontest.net in urllib network tests (GH-11941) (GH-12177)

Victor Stinner webhook-mailer at python.org
Tue Mar 5 09:19:03 EST 2019


https://github.com/python/cpython/commit/84772e0ab49ee09acb44e30551aa5cfc1eafe5dc
commit: 84772e0ab49ee09acb44e30551aa5cfc1eafe5dc
branch: 2.7
author: Stéphane Wirtel <stephane at wirtel.be>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-03-05T15:18:58+01:00
summary:

[2.7] bpo-36019: Use pythontest.net in urllib network tests (GH-11941) (GH-12177)

Use test_support.TEST_HTTP_URL (pythontest.net) instead of http://www.example.com/.

files:
A Misc/NEWS.d/next/Tests/2019-03-05-13-27-36.bpo-36019.ebUjCm.rst
M Doc/library/test.rst
M Lib/test/support/__init__.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 eef5d16c2404..9d78c90f55d5 100644
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -246,6 +246,11 @@ The :mod:`test.support` module defines the following constants:
    Set to a name that is safe to use as the name of a temporary file.  Any
    temporary file that is created should be closed and unlinked (removed).
 
+
+.. 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 9effdddd27dc..ccc11c1b4b0a 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -720,6 +720,10 @@ def u(s):
 # 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"
+
 # Save the initial cwd
 SAVEDCWD = os.getcwd()
 
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index ee0b7fe453e6..1cd80f283cd8 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -85,7 +85,7 @@ def test_close(self):
         # underlying socket
 
         # delve deep into response to fetch socket._socketobject
-        response = _urlopen_with_retry("http://www.example.com/")
+        response = _urlopen_with_retry(test_support.TEST_HTTP_URL)
         abused_fileobject = response.fp
         self.assertIs(abused_fileobject.__class__, socket._fileobject)
         httpresponse = abused_fileobject._sock
@@ -169,7 +169,7 @@ def test_urlwithfrag(self):
                     "http://www.pythontest.net/index.html#frag")
 
     def test_fileno(self):
-        req = urllib2.Request("http://www.example.com")
+        req = urllib2.Request(test_support.TEST_HTTP_URL)
         opener = urllib2.build_opener()
         res = opener.open(req)
         try:
@@ -180,7 +180,7 @@ def test_fileno(self):
             res.close()
 
     def test_custom_headers(self):
-        url = "http://www.example.com"
+        url = test_support.TEST_HTTP_URL
         with test_support.transient_internet(url):
             opener = urllib2.build_opener()
             request = urllib2.Request(url)
@@ -258,14 +258,14 @@ def _extra_handlers(self):
 class TimeoutTest(unittest.TestCase):
     def test_http_basic(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = test_support.TEST_HTTP_URL
         with test_support.transient_internet(url, timeout=None):
             u = _urlopen_with_retry(url)
             self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
 
     def test_http_default_timeout(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = test_support.TEST_HTTP_URL
         with test_support.transient_internet(url):
             socket.setdefaulttimeout(60)
             try:
@@ -276,7 +276,7 @@ def test_http_default_timeout(self):
 
     def test_http_no_timeout(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        url = "http://www.example.com"
+        url = test_support.TEST_HTTP_URL
         with test_support.transient_internet(url):
             socket.setdefaulttimeout(60)
             try:
@@ -286,7 +286,7 @@ def test_http_no_timeout(self):
             self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
 
     def test_http_timeout(self):
-        url = "http://www.example.com"
+        url = test_support.TEST_HTTP_URL
         with test_support.transient_internet(url):
             u = _urlopen_with_retry(url, timeout=120)
             self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index a4b4d9250394..3f2d8dcd43b0 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -43,7 +43,7 @@ def tearDown(self):
         socket.setdefaulttimeout(None)
 
     def testURLread(self):
-        f = _open_with_retry(urllib.urlopen, "http://www.example.com/")
+        f = _open_with_retry(urllib.urlopen, test_support.TEST_HTTP_URL)
         x = f.read()
 
 class urlopenNetworkTests(unittest.TestCase):
@@ -66,7 +66,7 @@ def urlopen(self, *args):
 
     def test_basic(self):
         # Simple test expected to pass.
-        open_url = self.urlopen("http://www.example.com/")
+        open_url = self.urlopen(test_support.TEST_HTTP_URL)
         for attr in ("read", "readline", "readlines", "fileno", "close",
                      "info", "geturl"):
             self.assertTrue(hasattr(open_url, attr), "object returned from "
@@ -78,7 +78,7 @@ def test_basic(self):
 
     def test_readlines(self):
         # Test both readline and readlines.
-        open_url = self.urlopen("http://www.example.com/")
+        open_url = self.urlopen(test_support.TEST_HTTP_URL)
         try:
             self.assertIsInstance(open_url.readline(), basestring,
                                   "readline did not return a string")
@@ -89,7 +89,7 @@ def test_readlines(self):
 
     def test_info(self):
         # Test 'info'.
-        open_url = self.urlopen("http://www.example.com/")
+        open_url = self.urlopen(test_support.TEST_HTTP_URL)
         try:
             info_obj = open_url.info()
         finally:
@@ -101,13 +101,12 @@ def test_info(self):
 
     def test_geturl(self):
         # Make sure same URL as opened is returned by geturl.
-        URL = "http://www.example.com/"
-        open_url = self.urlopen(URL)
+        open_url = self.urlopen(test_support.TEST_HTTP_URL)
         try:
             gotten_url = open_url.geturl()
         finally:
             open_url.close()
-        self.assertEqual(gotten_url, URL)
+        self.assertEqual(gotten_url, test_support.TEST_HTTP_URL)
 
     def test_getcode(self):
         # test getcode() with the fancy opener to get 404 error codes
@@ -123,12 +122,13 @@ def test_getcode(self):
     @unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available')
     def test_fileno(self):
         # Make sure fd returned by fileno is valid.
-        open_url = self.urlopen("http://www.example.com/")
+        open_url = self.urlopen(test_support.TEST_HTTP_URL)
         fd = open_url.fileno()
         FILE = os.fdopen(fd)
         try:
-            self.assertTrue(FILE.read(), "reading from file created using fd "
-                                      "returned by fileno failed")
+            self.assertTrue(FILE.read(),
+                            "reading from file created using fd "
+                            "returned by fileno failed")
         finally:
             FILE.close()
 
@@ -161,7 +161,7 @@ def urlretrieve(self, *args):
 
     def test_basic(self):
         # Test basic functionality.
-        file_location,info = self.urlretrieve("http://www.example.com/")
+        file_location,info = self.urlretrieve(test_support.TEST_HTTP_URL)
         self.assertTrue(os.path.exists(file_location), "file location returned by"
                         " urlretrieve is not a valid path")
         FILE = file(file_location)
@@ -174,7 +174,7 @@ def test_basic(self):
 
     def test_specified_path(self):
         # Make sure that specifying the location of the file to write to works.
-        file_location,info = self.urlretrieve("http://www.example.com/",
+        file_location,info = self.urlretrieve(test_support.TEST_HTTP_URL,
                                               test_support.TESTFN)
         self.assertEqual(file_location, test_support.TESTFN)
         self.assertTrue(os.path.exists(file_location))
@@ -187,13 +187,13 @@ def test_specified_path(self):
 
     def test_header(self):
         # Make sure header returned as 2nd value from urlretrieve is good.
-        file_location, header = self.urlretrieve("http://www.example.com/")
+        file_location, header = self.urlretrieve(test_support.TEST_HTTP_URL)
         os.unlink(file_location)
         self.assertIsInstance(header, mimetools.Message,
                               "header is not an instance of mimetools.Message")
 
     def test_data_header(self):
-        logo = "http://www.example.com/"
+        logo = test_support.TEST_HTTP_URL
         file_location, fileheaders = self.urlretrieve(logo)
         os.unlink(file_location)
         datevalue = fileheaders.getheader('Date')
diff --git a/Misc/NEWS.d/next/Tests/2019-03-05-13-27-36.bpo-36019.ebUjCm.rst b/Misc/NEWS.d/next/Tests/2019-03-05-13-27-36.bpo-36019.ebUjCm.rst
new file mode 100644
index 000000000000..c4abaf1fb476
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-03-05-13-27-36.bpo-36019.ebUjCm.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