Commit 84772e0a authored by Stéphane Wirtel's avatar Stéphane Wirtel Committed by Victor Stinner

[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/.
parent d9bf7f41
...@@ -246,6 +246,11 @@ The :mod:`test.support` module defines the following constants: ...@@ -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 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). 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: The :mod:`test.support` module defines the following functions:
......
...@@ -720,6 +720,10 @@ else: ...@@ -720,6 +720,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"
# Save the initial cwd # Save the initial cwd
SAVEDCWD = os.getcwd() SAVEDCWD = os.getcwd()
......
...@@ -85,7 +85,7 @@ class CloseSocketTest(unittest.TestCase): ...@@ -85,7 +85,7 @@ class CloseSocketTest(unittest.TestCase):
# underlying socket # underlying socket
# delve deep into response to fetch socket._socketobject # 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 abused_fileobject = response.fp
self.assertIs(abused_fileobject.__class__, socket._fileobject) self.assertIs(abused_fileobject.__class__, socket._fileobject)
httpresponse = abused_fileobject._sock httpresponse = abused_fileobject._sock
...@@ -169,7 +169,7 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -169,7 +169,7 @@ class OtherNetworkTests(unittest.TestCase):
"http://www.pythontest.net/index.html#frag") "http://www.pythontest.net/index.html#frag")
def test_fileno(self): def test_fileno(self):
req = urllib2.Request("http://www.example.com") req = urllib2.Request(test_support.TEST_HTTP_URL)
opener = urllib2.build_opener() opener = urllib2.build_opener()
res = opener.open(req) res = opener.open(req)
try: try:
...@@ -180,7 +180,7 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -180,7 +180,7 @@ class OtherNetworkTests(unittest.TestCase):
res.close() res.close()
def test_custom_headers(self): def test_custom_headers(self):
url = "http://www.example.com" url = test_support.TEST_HTTP_URL
with test_support.transient_internet(url): with test_support.transient_internet(url):
opener = urllib2.build_opener() opener = urllib2.build_opener()
request = urllib2.Request(url) request = urllib2.Request(url)
...@@ -258,14 +258,14 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -258,14 +258,14 @@ 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 = test_support.TEST_HTTP_URL
with test_support.transient_internet(url, timeout=None): with test_support.transient_internet(url, timeout=None):
u = _urlopen_with_retry(url) u = _urlopen_with_retry(url)
self.assertIsNone(u.fp._sock.fp._sock.gettimeout()) self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
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 = test_support.TEST_HTTP_URL
with test_support.transient_internet(url): with test_support.transient_internet(url):
socket.setdefaulttimeout(60) socket.setdefaulttimeout(60)
try: try:
...@@ -276,7 +276,7 @@ class TimeoutTest(unittest.TestCase): ...@@ -276,7 +276,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 = test_support.TEST_HTTP_URL
with test_support.transient_internet(url): with test_support.transient_internet(url):
socket.setdefaulttimeout(60) socket.setdefaulttimeout(60)
try: try:
...@@ -286,7 +286,7 @@ class TimeoutTest(unittest.TestCase): ...@@ -286,7 +286,7 @@ class TimeoutTest(unittest.TestCase):
self.assertIsNone(u.fp._sock.fp._sock.gettimeout()) self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
def test_http_timeout(self): def test_http_timeout(self):
url = "http://www.example.com" url = test_support.TEST_HTTP_URL
with test_support.transient_internet(url): with test_support.transient_internet(url):
u = _urlopen_with_retry(url, timeout=120) u = _urlopen_with_retry(url, timeout=120)
self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120) self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)
......
...@@ -43,7 +43,7 @@ class URLTimeoutTest(unittest.TestCase): ...@@ -43,7 +43,7 @@ class URLTimeoutTest(unittest.TestCase):
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
def testURLread(self): 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() x = f.read()
class urlopenNetworkTests(unittest.TestCase): class urlopenNetworkTests(unittest.TestCase):
...@@ -66,7 +66,7 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -66,7 +66,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_basic(self): def test_basic(self):
# Simple test expected to pass. # 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", for attr in ("read", "readline", "readlines", "fileno", "close",
"info", "geturl"): "info", "geturl"):
self.assertTrue(hasattr(open_url, attr), "object returned from " self.assertTrue(hasattr(open_url, attr), "object returned from "
...@@ -78,7 +78,7 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -78,7 +78,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_readlines(self): def test_readlines(self):
# Test both readline and readlines. # Test both readline and readlines.
open_url = self.urlopen("http://www.example.com/") open_url = self.urlopen(test_support.TEST_HTTP_URL)
try: try:
self.assertIsInstance(open_url.readline(), basestring, self.assertIsInstance(open_url.readline(), basestring,
"readline did not return a string") "readline did not return a string")
...@@ -89,7 +89,7 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -89,7 +89,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_info(self): def test_info(self):
# Test 'info'. # Test 'info'.
open_url = self.urlopen("http://www.example.com/") open_url = self.urlopen(test_support.TEST_HTTP_URL)
try: try:
info_obj = open_url.info() info_obj = open_url.info()
finally: finally:
...@@ -101,13 +101,12 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -101,13 +101,12 @@ class urlopenNetworkTests(unittest.TestCase):
def test_geturl(self): def test_geturl(self):
# Make sure same URL as opened is returned by geturl. # Make sure same URL as opened is returned by geturl.
URL = "http://www.example.com/" open_url = self.urlopen(test_support.TEST_HTTP_URL)
open_url = self.urlopen(URL)
try: try:
gotten_url = open_url.geturl() gotten_url = open_url.geturl()
finally: finally:
open_url.close() open_url.close()
self.assertEqual(gotten_url, URL) self.assertEqual(gotten_url, test_support.TEST_HTTP_URL)
def test_getcode(self): def test_getcode(self):
# test getcode() with the fancy opener to get 404 error codes # test getcode() with the fancy opener to get 404 error codes
...@@ -123,12 +122,13 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -123,12 +122,13 @@ class urlopenNetworkTests(unittest.TestCase):
@unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available') @unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available')
def test_fileno(self): def test_fileno(self):
# Make sure fd returned by fileno is valid. # 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() fd = open_url.fileno()
FILE = os.fdopen(fd) FILE = os.fdopen(fd)
try: try:
self.assertTrue(FILE.read(), "reading from file created using fd " self.assertTrue(FILE.read(),
"returned by fileno failed") "reading from file created using fd "
"returned by fileno failed")
finally: finally:
FILE.close() FILE.close()
...@@ -161,7 +161,7 @@ class urlretrieveNetworkTests(unittest.TestCase): ...@@ -161,7 +161,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
def test_basic(self): def test_basic(self):
# Test basic functionality. # 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" self.assertTrue(os.path.exists(file_location), "file location returned by"
" urlretrieve is not a valid path") " urlretrieve is not a valid path")
FILE = file(file_location) FILE = file(file_location)
...@@ -174,7 +174,7 @@ class urlretrieveNetworkTests(unittest.TestCase): ...@@ -174,7 +174,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
def test_specified_path(self): def test_specified_path(self):
# Make sure that specifying the location of the file to write to works. # 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) test_support.TESTFN)
self.assertEqual(file_location, test_support.TESTFN) self.assertEqual(file_location, test_support.TESTFN)
self.assertTrue(os.path.exists(file_location)) self.assertTrue(os.path.exists(file_location))
...@@ -187,13 +187,13 @@ class urlretrieveNetworkTests(unittest.TestCase): ...@@ -187,13 +187,13 @@ class urlretrieveNetworkTests(unittest.TestCase):
def test_header(self): def test_header(self):
# Make sure header returned as 2nd value from urlretrieve is good. # 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) os.unlink(file_location)
self.assertIsInstance(header, mimetools.Message, self.assertIsInstance(header, mimetools.Message,
"header is not an instance of mimetools.Message") "header is not an instance of mimetools.Message")
def test_data_header(self): def test_data_header(self):
logo = "http://www.example.com/" logo = test_support.TEST_HTTP_URL
file_location, fileheaders = self.urlretrieve(logo) file_location, fileheaders = self.urlretrieve(logo)
os.unlink(file_location) os.unlink(file_location)
datevalue = fileheaders.getheader('Date') datevalue = fileheaders.getheader('Date')
......
Add test.support.TEST_HTTP_URL and replace references of
http://www.example.com by this new constant. Contributed by Stéphane Wirtel.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment