Skip to content

Commit 84772e0

Browse files
matrixisevstinner
authored andcommitted
[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/.
1 parent d9bf7f4 commit 84772e0

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

Doc/library/test.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ The :mod:`test.support` module defines the following constants:
246246
Set to a name that is safe to use as the name of a temporary file. Any
247247
temporary file that is created should be closed and unlinked (removed).
248248

249+
250+
.. data:: TEST_HTTP_URL
251+
252+
Define the URL of a dedicated HTTP server for the network tests.
253+
249254
The :mod:`test.support` module defines the following functions:
250255

251256

Lib/test/support/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ def u(s):
720720
# module name.
721721
TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
722722

723+
# Define the URL of a dedicated HTTP server for the network tests.
724+
# The URL must use clear-text HTTP: no redirection to encrypted HTTPS.
725+
TEST_HTTP_URL = "http://www.pythontest.net"
726+
723727
# Save the initial cwd
724728
SAVEDCWD = os.getcwd()
725729

Lib/test/test_urllib2net.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_close(self):
8585
# underlying socket
8686

8787
# delve deep into response to fetch socket._socketobject
88-
response = _urlopen_with_retry("http://www.example.com/")
88+
response = _urlopen_with_retry(test_support.TEST_HTTP_URL)
8989
abused_fileobject = response.fp
9090
self.assertIs(abused_fileobject.__class__, socket._fileobject)
9191
httpresponse = abused_fileobject._sock
@@ -169,7 +169,7 @@ def test_urlwithfrag(self):
169169
"http://www.pythontest.net/index.html#frag")
170170

171171
def test_fileno(self):
172-
req = urllib2.Request("http://www.example.com")
172+
req = urllib2.Request(test_support.TEST_HTTP_URL)
173173
opener = urllib2.build_opener()
174174
res = opener.open(req)
175175
try:
@@ -180,7 +180,7 @@ def test_fileno(self):
180180
res.close()
181181

182182
def test_custom_headers(self):
183-
url = "http://www.example.com"
183+
url = test_support.TEST_HTTP_URL
184184
with test_support.transient_internet(url):
185185
opener = urllib2.build_opener()
186186
request = urllib2.Request(url)
@@ -258,14 +258,14 @@ def _extra_handlers(self):
258258
class TimeoutTest(unittest.TestCase):
259259
def test_http_basic(self):
260260
self.assertIsNone(socket.getdefaulttimeout())
261-
url = "http://www.example.com"
261+
url = test_support.TEST_HTTP_URL
262262
with test_support.transient_internet(url, timeout=None):
263263
u = _urlopen_with_retry(url)
264264
self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
265265

266266
def test_http_default_timeout(self):
267267
self.assertIsNone(socket.getdefaulttimeout())
268-
url = "http://www.example.com"
268+
url = test_support.TEST_HTTP_URL
269269
with test_support.transient_internet(url):
270270
socket.setdefaulttimeout(60)
271271
try:
@@ -276,7 +276,7 @@ def test_http_default_timeout(self):
276276

277277
def test_http_no_timeout(self):
278278
self.assertIsNone(socket.getdefaulttimeout())
279-
url = "http://www.example.com"
279+
url = test_support.TEST_HTTP_URL
280280
with test_support.transient_internet(url):
281281
socket.setdefaulttimeout(60)
282282
try:
@@ -286,7 +286,7 @@ def test_http_no_timeout(self):
286286
self.assertIsNone(u.fp._sock.fp._sock.gettimeout())
287287

288288
def test_http_timeout(self):
289-
url = "http://www.example.com"
289+
url = test_support.TEST_HTTP_URL
290290
with test_support.transient_internet(url):
291291
u = _urlopen_with_retry(url, timeout=120)
292292
self.assertEqual(u.fp._sock.fp._sock.gettimeout(), 120)

Lib/test/test_urllibnet.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def tearDown(self):
4343
socket.setdefaulttimeout(None)
4444

4545
def testURLread(self):
46-
f = _open_with_retry(urllib.urlopen, "http://www.example.com/")
46+
f = _open_with_retry(urllib.urlopen, test_support.TEST_HTTP_URL)
4747
x = f.read()
4848

4949
class urlopenNetworkTests(unittest.TestCase):
@@ -66,7 +66,7 @@ def urlopen(self, *args):
6666

6767
def test_basic(self):
6868
# Simple test expected to pass.
69-
open_url = self.urlopen("http://www.example.com/")
69+
open_url = self.urlopen(test_support.TEST_HTTP_URL)
7070
for attr in ("read", "readline", "readlines", "fileno", "close",
7171
"info", "geturl"):
7272
self.assertTrue(hasattr(open_url, attr), "object returned from "
@@ -78,7 +78,7 @@ def test_basic(self):
7878

7979
def test_readlines(self):
8080
# Test both readline and readlines.
81-
open_url = self.urlopen("http://www.example.com/")
81+
open_url = self.urlopen(test_support.TEST_HTTP_URL)
8282
try:
8383
self.assertIsInstance(open_url.readline(), basestring,
8484
"readline did not return a string")
@@ -89,7 +89,7 @@ def test_readlines(self):
8989

9090
def test_info(self):
9191
# Test 'info'.
92-
open_url = self.urlopen("http://www.example.com/")
92+
open_url = self.urlopen(test_support.TEST_HTTP_URL)
9393
try:
9494
info_obj = open_url.info()
9595
finally:
@@ -101,13 +101,12 @@ def test_info(self):
101101

102102
def test_geturl(self):
103103
# Make sure same URL as opened is returned by geturl.
104-
URL = "http://www.example.com/"
105-
open_url = self.urlopen(URL)
104+
open_url = self.urlopen(test_support.TEST_HTTP_URL)
106105
try:
107106
gotten_url = open_url.geturl()
108107
finally:
109108
open_url.close()
110-
self.assertEqual(gotten_url, URL)
109+
self.assertEqual(gotten_url, test_support.TEST_HTTP_URL)
111110

112111
def test_getcode(self):
113112
# test getcode() with the fancy opener to get 404 error codes
@@ -123,12 +122,13 @@ def test_getcode(self):
123122
@unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available')
124123
def test_fileno(self):
125124
# Make sure fd returned by fileno is valid.
126-
open_url = self.urlopen("http://www.example.com/")
125+
open_url = self.urlopen(test_support.TEST_HTTP_URL)
127126
fd = open_url.fileno()
128127
FILE = os.fdopen(fd)
129128
try:
130-
self.assertTrue(FILE.read(), "reading from file created using fd "
131-
"returned by fileno failed")
129+
self.assertTrue(FILE.read(),
130+
"reading from file created using fd "
131+
"returned by fileno failed")
132132
finally:
133133
FILE.close()
134134

@@ -161,7 +161,7 @@ def urlretrieve(self, *args):
161161

162162
def test_basic(self):
163163
# Test basic functionality.
164-
file_location,info = self.urlretrieve("http://www.example.com/")
164+
file_location,info = self.urlretrieve(test_support.TEST_HTTP_URL)
165165
self.assertTrue(os.path.exists(file_location), "file location returned by"
166166
" urlretrieve is not a valid path")
167167
FILE = file(file_location)
@@ -174,7 +174,7 @@ def test_basic(self):
174174

175175
def test_specified_path(self):
176176
# Make sure that specifying the location of the file to write to works.
177-
file_location,info = self.urlretrieve("http://www.example.com/",
177+
file_location,info = self.urlretrieve(test_support.TEST_HTTP_URL,
178178
test_support.TESTFN)
179179
self.assertEqual(file_location, test_support.TESTFN)
180180
self.assertTrue(os.path.exists(file_location))
@@ -187,13 +187,13 @@ def test_specified_path(self):
187187

188188
def test_header(self):
189189
# Make sure header returned as 2nd value from urlretrieve is good.
190-
file_location, header = self.urlretrieve("http://www.example.com/")
190+
file_location, header = self.urlretrieve(test_support.TEST_HTTP_URL)
191191
os.unlink(file_location)
192192
self.assertIsInstance(header, mimetools.Message,
193193
"header is not an instance of mimetools.Message")
194194

195195
def test_data_header(self):
196-
logo = "http://www.example.com/"
196+
logo = test_support.TEST_HTTP_URL
197197
file_location, fileheaders = self.urlretrieve(logo)
198198
os.unlink(file_location)
199199
datevalue = fileheaders.getheader('Date')
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
2+
http://www.example.com by this new constant. Contributed by Stéphane Wirtel.

0 commit comments

Comments
 (0)