mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 10:26:02 +00:00 
			
		
		
		
	 45df820591
			
		
	
	
		45df820591
		
	
	
	
	
		
			
			svn+ssh://pythondev@svn.python.org/python/trunk ........ r80552 | victor.stinner | 2010-04-27 23:46:03 +0200 (mar., 27 avril 2010) | 3 lines Issue #7449, part 1: fix test_support.py for Python compiled without thread ........ r80553 | victor.stinner | 2010-04-27 23:47:01 +0200 (mar., 27 avril 2010) | 1 line Issue #7449, part 2: regrtest.py -j option requires thread support ........ r80554 | victor.stinner | 2010-04-27 23:51:26 +0200 (mar., 27 avril 2010) | 9 lines Issue #7449 part 3, test_doctest: import trace module in test_coverage() Import trace module fail if the threading module is missing. test_coverage() is only used if test_doctest.py is used with the -c option. This commit allows to execute the test suite without thread support. Move "import trace" in test_coverage() and use test_support.import_module('trace'). ........ r80555 | victor.stinner | 2010-04-27 23:56:26 +0200 (mar., 27 avril 2010) | 6 lines Issue #7449, part 4: skip test_multiprocessing if thread support is disabled import threading after _multiprocessing to raise a more revelant error message: "No module named _multiprocessing". _multiprocessing is not compiled without thread support. ........ r80556 | victor.stinner | 2010-04-28 00:01:24 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 5: split Test.test_open() of ctypes/test/test_errno.py * Split Test.test_open() in 2 functions: test_open() and test_thread_open() * Skip test_open() and test_thread_open() if we are unable to find the C library * Skip test_thread_open() if thread support is disabled * Use unittest.skipUnless(os.name == "nt", ...) on test_GetLastError() ........ r80564 | victor.stinner | 2010-04-28 00:59:35 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 6: fix test_hashlib for missing threading module Move @test_support.reap_thread decorator from test_main() to test_threaded_hashing(). ........ r80565 | victor.stinner | 2010-04-28 01:01:29 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 7: simplify threading detection in test_capi * Skip TestPendingCalls if threading module is missing * Test if threading module is present or not, instead of test the presence of _testcapi._test_thread_state ........ r80566 | victor.stinner | 2010-04-28 01:03:16 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 8: don't skip the whole test_asynchat if threading is missing TestFifo can be executed without the threading module ........ r80568 | victor.stinner | 2010-04-28 01:14:58 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 9: fix test_xmlrpclib for missing threading module * Skip testcases using threads if threading module is missing * Use "http://" instead of URL in ServerProxyTestCase if threading is missing because URL is not set in this case ........ r80569 | victor.stinner | 2010-04-28 01:33:58 +0200 (mer., 28 avril 2010) | 6 lines Partial revert of r80556 (Issue #7449, part 5, fix ctypes test) Rewrite r80556: the thread test have to be executed just after the test on libc_open() and so the test cannot be splitted in two functions (without duplicating code, and I don't want to duplicate code). ........ r80570 | victor.stinner | 2010-04-28 01:51:16 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 10: test_cmd imports trace module using test_support.import_module() Use test_support.import_module() instead of import to raise a SkipTest exception if the import fail. Import trace fails if the threading module is missing. See also part 3: test_doctest: import trace module in test_coverage(). ........ r80571 | victor.stinner | 2010-04-28 01:55:59 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, last part (11): fix many tests if thread support is disabled * Use try/except ImportError or test_support.import_module() to import thread and threading modules * Add @unittest.skipUnless(threading, ...) to testcases using threads ........
		
			
				
	
	
		
			508 lines
		
	
	
	
		
			18 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			508 lines
		
	
	
	
		
			18 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/env python3
 | |
| 
 | |
| import email
 | |
| import urllib.parse
 | |
| import urllib.request
 | |
| import http.server
 | |
| import unittest
 | |
| import hashlib
 | |
| from test import support
 | |
| threading = support.import_module('threading')
 | |
| 
 | |
| # Loopback http server infrastructure
 | |
| 
 | |
| class LoopbackHttpServer(http.server.HTTPServer):
 | |
|     """HTTP server w/ a few modifications that make it useful for
 | |
|     loopback testing purposes.
 | |
|     """
 | |
| 
 | |
|     def __init__(self, server_address, RequestHandlerClass):
 | |
|         http.server.HTTPServer.__init__(self,
 | |
|                                         server_address,
 | |
|                                         RequestHandlerClass)
 | |
| 
 | |
|         # Set the timeout of our listening socket really low so
 | |
|         # that we can stop the server easily.
 | |
|         self.socket.settimeout(1.0)
 | |
| 
 | |
|     def get_request(self):
 | |
|         """HTTPServer method, overridden."""
 | |
| 
 | |
|         request, client_address = self.socket.accept()
 | |
| 
 | |
|         # It's a loopback connection, so setting the timeout
 | |
|         # really low shouldn't affect anything, but should make
 | |
|         # deadlocks less likely to occur.
 | |
|         request.settimeout(10.0)
 | |
| 
 | |
|         return (request, client_address)
 | |
| 
 | |
| class LoopbackHttpServerThread(threading.Thread):
 | |
|     """Stoppable thread that runs a loopback http server."""
 | |
| 
 | |
|     def __init__(self, request_handler):
 | |
|         threading.Thread.__init__(self)
 | |
|         self._stop_server = False
 | |
|         self.ready = threading.Event()
 | |
|         request_handler.protocol_version = "HTTP/1.0"
 | |
|         self.httpd = LoopbackHttpServer(("127.0.0.1", 0),
 | |
|                                         request_handler)
 | |
|         #print "Serving HTTP on %s port %s" % (self.httpd.server_name,
 | |
|         #                                      self.httpd.server_port)
 | |
|         self.port = self.httpd.server_port
 | |
| 
 | |
|     def stop(self):
 | |
|         """Stops the webserver if it's currently running."""
 | |
| 
 | |
|         # Set the stop flag.
 | |
|         self._stop_server = True
 | |
| 
 | |
|         self.join()
 | |
| 
 | |
|     def run(self):
 | |
|         self.ready.set()
 | |
|         while not self._stop_server:
 | |
|             self.httpd.handle_request()
 | |
| 
 | |
| # Authentication infrastructure
 | |
| 
 | |
| class DigestAuthHandler:
 | |
|     """Handler for performing digest authentication."""
 | |
| 
 | |
|     def __init__(self):
 | |
|         self._request_num = 0
 | |
|         self._nonces = []
 | |
|         self._users = {}
 | |
|         self._realm_name = "Test Realm"
 | |
|         self._qop = "auth"
 | |
| 
 | |
|     def set_qop(self, qop):
 | |
|         self._qop = qop
 | |
| 
 | |
|     def set_users(self, users):
 | |
|         assert isinstance(users, dict)
 | |
|         self._users = users
 | |
| 
 | |
|     def set_realm(self, realm):
 | |
|         self._realm_name = realm
 | |
| 
 | |
|     def _generate_nonce(self):
 | |
|         self._request_num += 1
 | |
|         nonce = hashlib.md5(str(self._request_num).encode("ascii")).hexdigest()
 | |
|         self._nonces.append(nonce)
 | |
|         return nonce
 | |
| 
 | |
|     def _create_auth_dict(self, auth_str):
 | |
|         first_space_index = auth_str.find(" ")
 | |
|         auth_str = auth_str[first_space_index+1:]
 | |
| 
 | |
|         parts = auth_str.split(",")
 | |
| 
 | |
|         auth_dict = {}
 | |
|         for part in parts:
 | |
|             name, value = part.split("=")
 | |
|             name = name.strip()
 | |
|             if value[0] == '"' and value[-1] == '"':
 | |
|                 value = value[1:-1]
 | |
|             else:
 | |
|                 value = value.strip()
 | |
|             auth_dict[name] = value
 | |
|         return auth_dict
 | |
| 
 | |
|     def _validate_auth(self, auth_dict, password, method, uri):
 | |
|         final_dict = {}
 | |
|         final_dict.update(auth_dict)
 | |
|         final_dict["password"] = password
 | |
|         final_dict["method"] = method
 | |
|         final_dict["uri"] = uri
 | |
|         HA1_str = "%(username)s:%(realm)s:%(password)s" % final_dict
 | |
|         HA1 = hashlib.md5(HA1_str.encode("ascii")).hexdigest()
 | |
|         HA2_str = "%(method)s:%(uri)s" % final_dict
 | |
|         HA2 = hashlib.md5(HA2_str.encode("ascii")).hexdigest()
 | |
|         final_dict["HA1"] = HA1
 | |
|         final_dict["HA2"] = HA2
 | |
|         response_str = "%(HA1)s:%(nonce)s:%(nc)s:" \
 | |
|                        "%(cnonce)s:%(qop)s:%(HA2)s" % final_dict
 | |
|         response = hashlib.md5(response_str.encode("ascii")).hexdigest()
 | |
| 
 | |
|         return response == auth_dict["response"]
 | |
| 
 | |
|     def _return_auth_challenge(self, request_handler):
 | |
|         request_handler.send_response(407, "Proxy Authentication Required")
 | |
|         request_handler.send_header("Content-Type", "text/html")
 | |
|         request_handler.send_header(
 | |
|             'Proxy-Authenticate', 'Digest realm="%s", '
 | |
|             'qop="%s",'
 | |
|             'nonce="%s", ' % \
 | |
|             (self._realm_name, self._qop, self._generate_nonce()))
 | |
|         # XXX: Not sure if we're supposed to add this next header or
 | |
|         # not.
 | |
|         #request_handler.send_header('Connection', 'close')
 | |
|         request_handler.end_headers()
 | |
|         request_handler.wfile.write(b"Proxy Authentication Required.")
 | |
|         return False
 | |
| 
 | |
|     def handle_request(self, request_handler):
 | |
|         """Performs digest authentication on the given HTTP request
 | |
|         handler.  Returns True if authentication was successful, False
 | |
|         otherwise.
 | |
| 
 | |
|         If no users have been set, then digest auth is effectively
 | |
|         disabled and this method will always return True.
 | |
|         """
 | |
| 
 | |
|         if len(self._users) == 0:
 | |
|             return True
 | |
| 
 | |
|         if "Proxy-Authorization" not in request_handler.headers:
 | |
|             return self._return_auth_challenge(request_handler)
 | |
|         else:
 | |
|             auth_dict = self._create_auth_dict(
 | |
|                 request_handler.headers["Proxy-Authorization"]
 | |
|                 )
 | |
|             if auth_dict["username"] in self._users:
 | |
|                 password = self._users[ auth_dict["username"] ]
 | |
|             else:
 | |
|                 return self._return_auth_challenge(request_handler)
 | |
|             if not auth_dict.get("nonce") in self._nonces:
 | |
|                 return self._return_auth_challenge(request_handler)
 | |
|             else:
 | |
|                 self._nonces.remove(auth_dict["nonce"])
 | |
| 
 | |
|             auth_validated = False
 | |
| 
 | |
|             # MSIE uses short_path in its validation, but Python's
 | |
|             # urllib2 uses the full path, so we're going to see if
 | |
|             # either of them works here.
 | |
| 
 | |
|             for path in [request_handler.path, request_handler.short_path]:
 | |
|                 if self._validate_auth(auth_dict,
 | |
|                                        password,
 | |
|                                        request_handler.command,
 | |
|                                        path):
 | |
|                     auth_validated = True
 | |
| 
 | |
|             if not auth_validated:
 | |
|                 return self._return_auth_challenge(request_handler)
 | |
|             return True
 | |
| 
 | |
| # Proxy test infrastructure
 | |
| 
 | |
| class FakeProxyHandler(http.server.BaseHTTPRequestHandler):
 | |
|     """This is a 'fake proxy' that makes it look like the entire
 | |
|     internet has gone down due to a sudden zombie invasion.  It main
 | |
|     utility is in providing us with authentication support for
 | |
|     testing.
 | |
|     """
 | |
| 
 | |
|     def __init__(self, digest_auth_handler, *args, **kwargs):
 | |
|         # This has to be set before calling our parent's __init__(), which will
 | |
|         # try to call do_GET().
 | |
|         self.digest_auth_handler = digest_auth_handler
 | |
|         http.server.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
 | |
| 
 | |
|     def log_message(self, format, *args):
 | |
|         # Uncomment the next line for debugging.
 | |
|         # sys.stderr.write(format % args)
 | |
|         pass
 | |
| 
 | |
|     def do_GET(self):
 | |
|         (scm, netloc, path, params, query, fragment) = urllib.parse.urlparse(
 | |
|             self.path, "http")
 | |
|         self.short_path = path
 | |
|         if self.digest_auth_handler.handle_request(self):
 | |
|             self.send_response(200, "OK")
 | |
|             self.send_header("Content-Type", "text/html")
 | |
|             self.end_headers()
 | |
|             self.wfile.write(bytes("You've reached %s!<BR>" % self.path,
 | |
|                                    "ascii"))
 | |
|             self.wfile.write(b"Our apologies, but our server is down due to "
 | |
|                              b"a sudden zombie invasion.")
 | |
| 
 | |
| # Test cases
 | |
| 
 | |
| class BaseTestCase(unittest.TestCase):
 | |
|     def setUp(self):
 | |
|         self._threads = support.threading_setup()
 | |
| 
 | |
|     def tearDown(self):
 | |
|         support.threading_cleanup(*self._threads)
 | |
| 
 | |
| 
 | |
| class ProxyAuthTests(BaseTestCase):
 | |
|     URL = "http://localhost"
 | |
| 
 | |
|     USER = "tester"
 | |
|     PASSWD = "test123"
 | |
|     REALM = "TestRealm"
 | |
| 
 | |
|     def setUp(self):
 | |
|         super(ProxyAuthTests, self).setUp()
 | |
|         self.digest_auth_handler = DigestAuthHandler()
 | |
|         self.digest_auth_handler.set_users({self.USER: self.PASSWD})
 | |
|         self.digest_auth_handler.set_realm(self.REALM)
 | |
|         def create_fake_proxy_handler(*args, **kwargs):
 | |
|             return FakeProxyHandler(self.digest_auth_handler, *args, **kwargs)
 | |
| 
 | |
|         self.server = LoopbackHttpServerThread(create_fake_proxy_handler)
 | |
|         self.server.start()
 | |
|         self.server.ready.wait()
 | |
|         proxy_url = "http://127.0.0.1:%d" % self.server.port
 | |
|         handler = urllib.request.ProxyHandler({"http" : proxy_url})
 | |
|         self.proxy_digest_handler = urllib.request.ProxyDigestAuthHandler()
 | |
|         self.opener = urllib.request.build_opener(
 | |
|             handler, self.proxy_digest_handler)
 | |
| 
 | |
|     def tearDown(self):
 | |
|         self.server.stop()
 | |
|         super(ProxyAuthTests, self).tearDown()
 | |
| 
 | |
|     def test_proxy_with_bad_password_raises_httperror(self):
 | |
|         self.proxy_digest_handler.add_password(self.REALM, self.URL,
 | |
|                                                self.USER, self.PASSWD+"bad")
 | |
|         self.digest_auth_handler.set_qop("auth")
 | |
|         self.assertRaises(urllib.error.HTTPError,
 | |
|                           self.opener.open,
 | |
|                           self.URL)
 | |
| 
 | |
|     def test_proxy_with_no_password_raises_httperror(self):
 | |
|         self.digest_auth_handler.set_qop("auth")
 | |
|         self.assertRaises(urllib.error.HTTPError,
 | |
|                           self.opener.open,
 | |
|                           self.URL)
 | |
| 
 | |
|     def test_proxy_qop_auth_works(self):
 | |
|         self.proxy_digest_handler.add_password(self.REALM, self.URL,
 | |
|                                                self.USER, self.PASSWD)
 | |
|         self.digest_auth_handler.set_qop("auth")
 | |
|         result = self.opener.open(self.URL)
 | |
|         while result.read():
 | |
|             pass
 | |
|         result.close()
 | |
| 
 | |
|     def test_proxy_qop_auth_int_works_or_throws_urlerror(self):
 | |
|         self.proxy_digest_handler.add_password(self.REALM, self.URL,
 | |
|                                                self.USER, self.PASSWD)
 | |
|         self.digest_auth_handler.set_qop("auth-int")
 | |
|         try:
 | |
|             result = self.opener.open(self.URL)
 | |
|         except urllib.error.URLError:
 | |
|             # It's okay if we don't support auth-int, but we certainly
 | |
|             # shouldn't receive any kind of exception here other than
 | |
|             # a URLError.
 | |
|             result = None
 | |
|         if result:
 | |
|             while result.read():
 | |
|                 pass
 | |
|             result.close()
 | |
| 
 | |
| 
 | |
| def GetRequestHandler(responses):
 | |
| 
 | |
|     class FakeHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
 | |
| 
 | |
|         server_version = "TestHTTP/"
 | |
|         requests = []
 | |
|         headers_received = []
 | |
|         port = 80
 | |
| 
 | |
|         def do_GET(self):
 | |
|             body = self.send_head()
 | |
|             if body:
 | |
|                 self.wfile.write(body)
 | |
| 
 | |
|         def do_POST(self):
 | |
|             content_length = self.headers["Content-Length"]
 | |
|             post_data = self.rfile.read(int(content_length))
 | |
|             self.do_GET()
 | |
|             self.requests.append(post_data)
 | |
| 
 | |
|         def send_head(self):
 | |
|             FakeHTTPRequestHandler.headers_received = self.headers
 | |
|             self.requests.append(self.path)
 | |
|             response_code, headers, body = responses.pop(0)
 | |
| 
 | |
|             self.send_response(response_code)
 | |
| 
 | |
|             for (header, value) in headers:
 | |
|                 self.send_header(header, value % {'port':self.port})
 | |
|             if body:
 | |
|                 self.send_header("Content-type", "text/plain")
 | |
|                 self.end_headers()
 | |
|                 return body
 | |
|             self.end_headers()
 | |
| 
 | |
|         def log_message(self, *args):
 | |
|             pass
 | |
| 
 | |
| 
 | |
|     return FakeHTTPRequestHandler
 | |
| 
 | |
| 
 | |
| class TestUrlopen(BaseTestCase):
 | |
|     """Tests urllib2.urlopen using the network.
 | |
| 
 | |
|     These tests are not exhaustive.  Assuming that testing using files does a
 | |
|     good job overall of some of the basic interface features.  There are no
 | |
|     tests exercising the optional 'data' and 'proxies' arguments.  No tests
 | |
|     for transparent redirection have been written.
 | |
|     """
 | |
| 
 | |
|     def setUp(self):
 | |
|         super(TestUrlopen, self).setUp()
 | |
|         self.server = None
 | |
| 
 | |
|     def tearDown(self):
 | |
|         if self.server is not None:
 | |
|             self.server.stop()
 | |
|         super(TestUrlopen, self).tearDown()
 | |
| 
 | |
|     def urlopen(self, url, data=None):
 | |
|         l = []
 | |
|         f = urllib.request.urlopen(url, data)
 | |
|         try:
 | |
|             # Exercise various methods
 | |
|             l.extend(f.readlines(200))
 | |
|             l.append(f.readline())
 | |
|             l.append(f.read(1024))
 | |
|             l.append(f.read())
 | |
|         finally:
 | |
|             f.close()
 | |
|         return b"".join(l)
 | |
| 
 | |
|     def start_server(self, responses=None):
 | |
|         if responses is None:
 | |
|             responses = [(200, [], b"we don't care")]
 | |
|         handler = GetRequestHandler(responses)
 | |
| 
 | |
|         self.server = LoopbackHttpServerThread(handler)
 | |
|         self.server.start()
 | |
|         self.server.ready.wait()
 | |
|         port = self.server.port
 | |
|         handler.port = port
 | |
|         return handler
 | |
| 
 | |
|     def test_redirection(self):
 | |
|         expected_response = b"We got here..."
 | |
|         responses = [
 | |
|             (302, [("Location", "http://localhost:%(port)s/somewhere_else")],
 | |
|              ""),
 | |
|             (200, [], expected_response)
 | |
|         ]
 | |
| 
 | |
|         handler = self.start_server(responses)
 | |
|         data = self.urlopen("http://localhost:%s/" % handler.port)
 | |
|         self.assertEquals(data, expected_response)
 | |
|         self.assertEquals(handler.requests, ["/", "/somewhere_else"])
 | |
| 
 | |
|     def test_chunked(self):
 | |
|         expected_response = b"hello world"
 | |
|         chunked_start = (
 | |
|                         b'a\r\n'
 | |
|                         b'hello worl\r\n'
 | |
|                         b'1\r\n'
 | |
|                         b'd\r\n'
 | |
|                         b'0\r\n'
 | |
|                         )
 | |
|         response = [(200, [("Transfer-Encoding", "chunked")], chunked_start)]
 | |
|         handler = self.start_server(response)
 | |
|         data = self.urlopen("http://localhost:%s/" % handler.port)
 | |
|         self.assertEquals(data, expected_response)
 | |
| 
 | |
|     def test_404(self):
 | |
|         expected_response = b"Bad bad bad..."
 | |
|         handler = self.start_server([(404, [], expected_response)])
 | |
| 
 | |
|         try:
 | |
|             self.urlopen("http://localhost:%s/weeble" % handler.port)
 | |
|         except urllib.error.URLError as f:
 | |
|             data = f.read()
 | |
|             f.close()
 | |
|         else:
 | |
|             self.fail("404 should raise URLError")
 | |
| 
 | |
|         self.assertEquals(data, expected_response)
 | |
|         self.assertEquals(handler.requests, ["/weeble"])
 | |
| 
 | |
|     def test_200(self):
 | |
|         expected_response = b"pycon 2008..."
 | |
|         handler = self.start_server([(200, [], expected_response)])
 | |
|         data = self.urlopen("http://localhost:%s/bizarre" % handler.port)
 | |
|         self.assertEquals(data, expected_response)
 | |
|         self.assertEquals(handler.requests, ["/bizarre"])
 | |
| 
 | |
|     def test_200_with_parameters(self):
 | |
|         expected_response = b"pycon 2008..."
 | |
|         handler = self.start_server([(200, [], expected_response)])
 | |
|         data = self.urlopen("http://localhost:%s/bizarre" % handler.port,
 | |
|                              b"get=with_feeling")
 | |
|         self.assertEquals(data, expected_response)
 | |
|         self.assertEquals(handler.requests, ["/bizarre", b"get=with_feeling"])
 | |
| 
 | |
|     def test_sending_headers(self):
 | |
|         handler = self.start_server()
 | |
|         req = urllib.request.Request("http://localhost:%s/" % handler.port,
 | |
|                                      headers={"Range": "bytes=20-39"})
 | |
|         urllib.request.urlopen(req)
 | |
|         self.assertEqual(handler.headers_received["Range"], "bytes=20-39")
 | |
| 
 | |
|     def test_basic(self):
 | |
|         handler = self.start_server()
 | |
|         open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
 | |
|         for attr in ("read", "close", "info", "geturl"):
 | |
|             self.assertTrue(hasattr(open_url, attr), "object returned from "
 | |
|                          "urlopen lacks the %s attribute" % attr)
 | |
|         try:
 | |
|             self.assertTrue(open_url.read(), "calling 'read' failed")
 | |
|         finally:
 | |
|             open_url.close()
 | |
| 
 | |
|     def test_info(self):
 | |
|         handler = self.start_server()
 | |
|         try:
 | |
|             open_url = urllib.request.urlopen(
 | |
|                 "http://localhost:%s" % handler.port)
 | |
|             info_obj = open_url.info()
 | |
|             self.assertIsInstance(info_obj, email.message.Message,
 | |
|                                   "object returned by 'info' is not an "
 | |
|                                   "instance of email.message.Message")
 | |
|             self.assertEqual(info_obj.get_content_subtype(), "plain")
 | |
|         finally:
 | |
|             self.server.stop()
 | |
| 
 | |
|     def test_geturl(self):
 | |
|         # Make sure same URL as opened is returned by geturl.
 | |
|         handler = self.start_server()
 | |
|         open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
 | |
|         url = open_url.geturl()
 | |
|         self.assertEqual(url, "http://localhost:%s" % handler.port)
 | |
| 
 | |
|     def test_bad_address(self):
 | |
|         # Make sure proper exception is raised when connecting to a bogus
 | |
|         # address.
 | |
|         self.assertRaises(IOError,
 | |
|                           # 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.
 | |
|                           urllib.request.urlopen,
 | |
|                           "http://sadflkjsasf.i.nvali.d./")
 | |
| 
 | |
| def test_main():
 | |
|     support.run_unittest(ProxyAuthTests, TestUrlopen)
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     test_main()
 |