Skip to content

Commit a40681d

Browse files
matrixisevstinner
authored andcommitted
bpo-36019: Use pythontest.net instead of example.com in network tests (GH-11941)
1 parent 3208880 commit a40681d

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

Doc/library/test.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ The :mod:`test.support` module defines the following constants:
356356

357357
Check for presence of docstrings.
358358

359+
.. data:: TEST_HTTP_URL
360+
361+
Define the URL of a dedicated HTTP server for the network tests.
362+
359363

360364

361365
The :mod:`test.support` module defines the following functions:

Lib/test/support/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,10 @@ def dec(*args, **kwargs):
835835
# module name.
836836
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
837837

838+
# Define the URL of a dedicated HTTP server for the network tests.
839+
# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
840+
TEST_HTTP_URL = "http://www.pythontest.net"
841+
838842
# FS_NONASCII: non-ASCII character encodable by os.fsencode(),
839843
# or None if there is no such character.
840844
FS_NONASCII = None

Lib/test/test_urllib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def _reporthook(par1, par2, par3):
712712

713713
with self.assertRaises(urllib.error.ContentTooShortError):
714714
try:
715-
urllib.request.urlretrieve('http://example.com/',
715+
urllib.request.urlretrieve(support.TEST_HTTP_URL,
716716
reporthook=_reporthook)
717717
finally:
718718
self.unfakehttp()
@@ -729,7 +729,7 @@ def test_short_content_raises_ContentTooShortError_without_reporthook(self):
729729
''')
730730
with self.assertRaises(urllib.error.ContentTooShortError):
731731
try:
732-
urllib.request.urlretrieve('http://example.com/')
732+
urllib.request.urlretrieve(support.TEST_HTTP_URL)
733733
finally:
734734
self.unfakehttp()
735735

Lib/test/test_urllib2net.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CloseSocketTest(unittest.TestCase):
8484
def test_close(self):
8585
# calling .close() on urllib2's response objects should close the
8686
# underlying socket
87-
url = "http://www.example.com/"
87+
url = support.TEST_HTTP_URL
8888
with support.transient_internet(url):
8989
response = _urlopen_with_retry(url)
9090
sock = response.fp
@@ -173,7 +173,7 @@ def test_redirect_url_withfrag(self):
173173
"http://www.pythontest.net/elsewhere/#frag")
174174

175175
def test_custom_headers(self):
176-
url = "http://www.example.com"
176+
url = support.TEST_HTTP_URL
177177
with support.transient_internet(url):
178178
opener = urllib.request.build_opener()
179179
request = urllib.request.Request(url)
@@ -259,15 +259,15 @@ def _extra_handlers(self):
259259
class TimeoutTest(unittest.TestCase):
260260
def test_http_basic(self):
261261
self.assertIsNone(socket.getdefaulttimeout())
262-
url = "http://www.example.com"
262+
url = support.TEST_HTTP_URL
263263
with support.transient_internet(url, timeout=None):
264264
u = _urlopen_with_retry(url)
265265
self.addCleanup(u.close)
266266
self.assertIsNone(u.fp.raw._sock.gettimeout())
267267

268268
def test_http_default_timeout(self):
269269
self.assertIsNone(socket.getdefaulttimeout())
270-
url = "http://www.example.com"
270+
url = support.TEST_HTTP_URL
271271
with support.transient_internet(url):
272272
socket.setdefaulttimeout(60)
273273
try:
@@ -279,7 +279,7 @@ def test_http_default_timeout(self):
279279

280280
def test_http_no_timeout(self):
281281
self.assertIsNone(socket.getdefaulttimeout())
282-
url = "http://www.example.com"
282+
url = support.TEST_HTTP_URL
283283
with support.transient_internet(url):
284284
socket.setdefaulttimeout(60)
285285
try:
@@ -290,7 +290,7 @@ def test_http_no_timeout(self):
290290
self.assertIsNone(u.fp.raw._sock.gettimeout())
291291

292292
def test_http_timeout(self):
293-
url = "http://www.example.com"
293+
url = support.TEST_HTTP_URL
294294
with support.transient_internet(url):
295295
u = _urlopen_with_retry(url, timeout=120)
296296
self.addCleanup(u.close)

Lib/test/test_urllibnet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import contextlib
55
import socket
6+
import urllib.parse
67
import urllib.request
78
import os
89
import email.message
@@ -24,8 +25,9 @@ def tearDown(self):
2425
socket.setdefaulttimeout(None)
2526

2627
def testURLread(self):
27-
with support.transient_internet("www.example.com"):
28-
f = urllib.request.urlopen("http://www.example.com/")
28+
domain = urllib.parse.urlparse(support.TEST_HTTP_URL).netloc
29+
with support.transient_internet(domain):
30+
f = urllib.request.urlopen(support.TEST_HTTP_URL)
2931
f.read()
3032

3133

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add test.support.TEST_HTTP_URL and replace references of http://www.example.com
2+
by this new constant. Contributed by Stéphane Wirtel.

0 commit comments

Comments
 (0)