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

This commit is contained in:
Stéphane Wirtel 2019-02-22 14:45:36 +01:00 committed by Victor Stinner
parent 3208880f1c
commit a40681dd5d
6 changed files with 22 additions and 10 deletions

View file

@ -356,6 +356,10 @@ The :mod:`test.support` module defines the following constants:
Check for presence of docstrings. 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: The :mod:`test.support` module defines the following functions:

View file

@ -835,6 +835,10 @@ else:
# module name. # module name.
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid()) 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(), # FS_NONASCII: non-ASCII character encodable by os.fsencode(),
# or None if there is no such character. # or None if there is no such character.
FS_NONASCII = None FS_NONASCII = None

View file

@ -712,7 +712,7 @@ FF
with self.assertRaises(urllib.error.ContentTooShortError): with self.assertRaises(urllib.error.ContentTooShortError):
try: try:
urllib.request.urlretrieve('http://example.com/', urllib.request.urlretrieve(support.TEST_HTTP_URL,
reporthook=_reporthook) reporthook=_reporthook)
finally: finally:
self.unfakehttp() self.unfakehttp()
@ -729,7 +729,7 @@ FF
''') ''')
with self.assertRaises(urllib.error.ContentTooShortError): with self.assertRaises(urllib.error.ContentTooShortError):
try: try:
urllib.request.urlretrieve('http://example.com/') urllib.request.urlretrieve(support.TEST_HTTP_URL)
finally: finally:
self.unfakehttp() self.unfakehttp()

View file

@ -84,7 +84,7 @@ class CloseSocketTest(unittest.TestCase):
def test_close(self): def test_close(self):
# calling .close() on urllib2's response objects should close the # calling .close() on urllib2's response objects should close the
# underlying socket # underlying socket
url = "http://www.example.com/" url = support.TEST_HTTP_URL
with support.transient_internet(url): with support.transient_internet(url):
response = _urlopen_with_retry(url) response = _urlopen_with_retry(url)
sock = response.fp sock = response.fp
@ -173,7 +173,7 @@ class OtherNetworkTests(unittest.TestCase):
"http://www.pythontest.net/elsewhere/#frag") "http://www.pythontest.net/elsewhere/#frag")
def test_custom_headers(self): def test_custom_headers(self):
url = "http://www.example.com" url = support.TEST_HTTP_URL
with support.transient_internet(url): with support.transient_internet(url):
opener = urllib.request.build_opener() opener = urllib.request.build_opener()
request = urllib.request.Request(url) request = urllib.request.Request(url)
@ -259,7 +259,7 @@ class OtherNetworkTests(unittest.TestCase):
class TimeoutTest(unittest.TestCase): class TimeoutTest(unittest.TestCase):
def test_http_basic(self): def test_http_basic(self):
self.assertIsNone(socket.getdefaulttimeout()) self.assertIsNone(socket.getdefaulttimeout())
url = "http://www.example.com" url = support.TEST_HTTP_URL
with support.transient_internet(url, timeout=None): with support.transient_internet(url, timeout=None):
u = _urlopen_with_retry(url) u = _urlopen_with_retry(url)
self.addCleanup(u.close) self.addCleanup(u.close)
@ -267,7 +267,7 @@ class TimeoutTest(unittest.TestCase):
def test_http_default_timeout(self): def test_http_default_timeout(self):
self.assertIsNone(socket.getdefaulttimeout()) self.assertIsNone(socket.getdefaulttimeout())
url = "http://www.example.com" url = support.TEST_HTTP_URL
with support.transient_internet(url): with support.transient_internet(url):
socket.setdefaulttimeout(60) socket.setdefaulttimeout(60)
try: try:
@ -279,7 +279,7 @@ class TimeoutTest(unittest.TestCase):
def test_http_no_timeout(self): def test_http_no_timeout(self):
self.assertIsNone(socket.getdefaulttimeout()) self.assertIsNone(socket.getdefaulttimeout())
url = "http://www.example.com" url = support.TEST_HTTP_URL
with support.transient_internet(url): with support.transient_internet(url):
socket.setdefaulttimeout(60) socket.setdefaulttimeout(60)
try: try:
@ -290,7 +290,7 @@ class TimeoutTest(unittest.TestCase):
self.assertIsNone(u.fp.raw._sock.gettimeout()) self.assertIsNone(u.fp.raw._sock.gettimeout())
def test_http_timeout(self): def test_http_timeout(self):
url = "http://www.example.com" url = support.TEST_HTTP_URL
with support.transient_internet(url): with support.transient_internet(url):
u = _urlopen_with_retry(url, timeout=120) u = _urlopen_with_retry(url, timeout=120)
self.addCleanup(u.close) self.addCleanup(u.close)

View file

@ -3,6 +3,7 @@ from test import support
import contextlib import contextlib
import socket import socket
import urllib.parse
import urllib.request import urllib.request
import os import os
import email.message import email.message
@ -24,8 +25,9 @@ class URLTimeoutTest(unittest.TestCase):
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
def testURLread(self): def testURLread(self):
with support.transient_internet("www.example.com"): domain = urllib.parse.urlparse(support.TEST_HTTP_URL).netloc
f = urllib.request.urlopen("http://www.example.com/") with support.transient_internet(domain):
f = urllib.request.urlopen(support.TEST_HTTP_URL)
f.read() f.read()

View file

@ -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.