diff options
Diffstat (limited to 'Lib/test/test_urllibnet.py')
| -rw-r--r-- | Lib/test/test_urllibnet.py | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py index 42ebb6e..b811930 100644 --- a/Lib/test/test_urllibnet.py +++ b/Lib/test/test_urllibnet.py @@ -38,12 +38,14 @@ class urlopenNetworkTests(unittest.TestCase): for transparent redirection have been written. setUp is not used for always constructing a connection to - http://www.example.com/ since there a few tests that don't use that address + http://www.pythontest.net/ since there a few tests that don't use that address and making a connection is expensive enough to warrant minimizing unneeded connections. """ + url = 'http://www.pythontest.net/' + @contextlib.contextmanager def urlopen(self, *args, **kwargs): resource = args[0] @@ -56,7 +58,7 @@ class urlopenNetworkTests(unittest.TestCase): def test_basic(self): # Simple test expected to pass. - with self.urlopen("http://www.example.com/") as open_url: + with self.urlopen(self.url) as open_url: for attr in ("read", "readline", "readlines", "fileno", "close", "info", "geturl"): self.assertTrue(hasattr(open_url, attr), "object returned from " @@ -65,7 +67,7 @@ class urlopenNetworkTests(unittest.TestCase): def test_readlines(self): # Test both readline and readlines. - with self.urlopen("http://www.example.com/") as open_url: + with self.urlopen(self.url) as open_url: self.assertIsInstance(open_url.readline(), bytes, "readline did not return a string") self.assertIsInstance(open_url.readlines(), list, @@ -73,7 +75,7 @@ class urlopenNetworkTests(unittest.TestCase): def test_info(self): # Test 'info'. - with self.urlopen("http://www.example.com/") as open_url: + with self.urlopen(self.url) as open_url: info_obj = open_url.info() self.assertIsInstance(info_obj, email.message.Message, "object returned by 'info' is not an " @@ -82,14 +84,13 @@ class urlopenNetworkTests(unittest.TestCase): def test_geturl(self): # Make sure same URL as opened is returned by geturl. - URL = "http://www.example.com/" - with self.urlopen(URL) as open_url: + with self.urlopen(self.url) as open_url: gotten_url = open_url.geturl() - self.assertEqual(gotten_url, URL) + self.assertEqual(gotten_url, self.url) def test_getcode(self): # test getcode() with the fancy opener to get 404 error codes - URL = "http://www.example.com/XXXinvalidXXX" + URL = self.url + "XXXinvalidXXX" with support.transient_internet(URL): with self.assertWarns(DeprecationWarning): open_url = urllib.request.FancyURLopener().open(URL) @@ -99,21 +100,28 @@ class urlopenNetworkTests(unittest.TestCase): open_url.close() self.assertEqual(code, 404) - # On Windows, socket handles are not file descriptors; this - # test can't pass on Windows. - @unittest.skipIf(sys.platform in ('win32',), 'not appropriate for Windows') - def test_fileno(self): - # Make sure fd returned by fileno is valid. - with self.urlopen("http://www.google.com/", timeout=None) as open_url: - fd = open_url.fileno() - with os.fdopen(fd, 'rb') as f: - self.assertTrue(f.read(), "reading from file created using fd " - "returned by fileno failed") - def test_bad_address(self): # Make sure proper exception is raised when connecting to a bogus # address. - bogus_domain = "sadflkjsasf.i.nvali.d" + + # Given that both VeriSign and various ISPs have in + # the past or are presently hijacking various invalid + # domain name requests in an attempt to boost traffic + # to their own sites, finding a domain name to use + # for this test is difficult. RFC2606 leads one to + # believe that '.invalid' should work, but experience + # seemed to indicate otherwise. Single character + # TLDs are likely to remain invalid, so this seems to + # be the best choice. The trailing '.' prevents a + # related problem: The normal DNS resolver appends + # the domain names from the search path if there is + # no '.' the end and, and if one of those domains + # implements a '*' rule a result is returned. + # However, none of this will prevent the test from + # failing if the ISP hijacks all invalid domain + # requests. The real solution would be to be able to + # parameterize the framework with a mock resolver. + bogus_domain = "sadflkjsasf.i.nvali.d." try: socket.gethostbyname(bogus_domain) except OSError: @@ -128,11 +136,7 @@ class urlopenNetworkTests(unittest.TestCase): 'can be caused by a broken DNS server ' '(e.g. returns 404 or hijacks page)') with self.assertRaises(OSError, msg=failure_explanation): - # SF patch 809915: In Sep 2003, VeriSign started highjacking - # invalid .com and .net addresses to boost traffic to their own - # site. This test started failing then. One hopes the .invalid - # domain will be spared to serve its defined purpose. - urllib.request.urlopen("http://sadflkjsasf.i.nvali.d/") + urllib.request.urlopen("http://{}/".format(bogus_domain)) class urlretrieveNetworkTests(unittest.TestCase): @@ -150,7 +154,7 @@ class urlretrieveNetworkTests(unittest.TestCase): def test_basic(self): # Test basic functionality. - with self.urlretrieve("http://www.example.com/") as (file_location, info): + with self.urlretrieve(self.logo) as (file_location, info): self.assertTrue(os.path.exists(file_location), "file location returned by" " urlretrieve is not a valid path") with open(file_location, 'rb') as f: @@ -159,7 +163,7 @@ class urlretrieveNetworkTests(unittest.TestCase): def test_specified_path(self): # Make sure that specifying the location of the file to write to works. - with self.urlretrieve("http://www.example.com/", + with self.urlretrieve(self.logo, support.TESTFN) as (file_location, info): self.assertEqual(file_location, support.TESTFN) self.assertTrue(os.path.exists(file_location)) @@ -168,11 +172,11 @@ class urlretrieveNetworkTests(unittest.TestCase): def test_header(self): # Make sure header returned as 2nd value from urlretrieve is good. - with self.urlretrieve("http://www.example.com/") as (file_location, info): + with self.urlretrieve(self.logo) as (file_location, info): self.assertIsInstance(info, email.message.Message, "info is not an instance of email.message.Message") - logo = "http://www.example.com/" + logo = "http://www.pythontest.net/" def test_data_header(self): with self.urlretrieve(self.logo) as (file_location, fileheaders): @@ -181,7 +185,7 @@ class urlretrieveNetworkTests(unittest.TestCase): try: time.strptime(datevalue, dateformat) except ValueError: - self.fail('Date value not in %r format', dateformat) + self.fail('Date value not in %r format' % dateformat) def test_reporthook(self): records = [] |
