Back to index

python3.2  3.2.2
Classes | Functions | Variables
test.test_urllib2net Namespace Reference

Classes

class  AuthTests
class  CloseSocketTest
 Disabled at the moment since there is no page under python.org which could be used to HTTP authentication. More...
class  OtherNetworkTests
class  TimeoutTest
class  HTTPSTests

Functions

def _retry_thrice
def _wrap_with_retry_thrice
def test_urlwithfrag
 def test_cnri(self): if socket.gethostname() == 'bitdiddle': localhost = 'bitdiddle.cnri.reston.va.us' elif socket.gethostname() == 'bitdiddle.concentric.net': localhost = 'localhost' else: localhost = None if localhost is not None: urls = [ 'file://%s/etc/passwd' % localhost, 'http://%s/simple/' % localhost, 'http://%s/digest/' % localhost, 'http://%s/not/found.h' % localhost, ]
def test_custom_headers
def test_sites_no_connection_close
def _test_urls
def _extra_handlers
def test_main

Variables

 ssl = None
int TIMEOUT = 60
tuple _urlopen_with_retry

Class Documentation

class test::test_urllib2net::AuthTests
Tests urllib2 authentication features.

Definition at line 42 of file test_urllib2net.py.


Function Documentation

def test.test_urllib2net._extra_handlers (   self) [private]

Definition at line 241 of file test_urllib2net.py.

00241 
00242     def _extra_handlers(self):
00243         handlers = []
00244 
00245         cfh = urllib.request.CacheFTPHandler()
00246         self.addCleanup(cfh.clear_cache)
00247         cfh.setTimeout(1)
00248         handlers.append(cfh)
00249 
00250         return handlers
00251 

def test.test_urllib2net._retry_thrice (   func,
  exc,
  args,
  kwargs 
) [private]

Definition at line 20 of file test_urllib2net.py.

00020 
00021 def _retry_thrice(func, exc, *args, **kwargs):
00022     for i in range(3):
00023         try:
00024             return func(*args, **kwargs)
00025         except exc as e:
00026             last_exc = e
00027             continue
00028         except:
00029             raise
00030     raise last_exc

Here is the caller graph for this function:

def test.test_urllib2net._test_urls (   self,
  urls,
  handlers,
  retry = True 
) [private]

Definition at line 197 of file test_urllib2net.py.

00197 
00198     def _test_urls(self, urls, handlers, retry=True):
00199         import time
00200         import logging
00201         debug = logging.getLogger("test_urllib2").debug
00202 
00203         urlopen = urllib.request.build_opener(*handlers).open
00204         if retry:
00205             urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
00206 
00207         for url in urls:
00208             if isinstance(url, tuple):
00209                 url, req, expected_err = url
00210             else:
00211                 req = expected_err = None
00212 
00213             with support.transient_internet(url):
00214                 debug(url)
00215                 try:
00216                     f = urlopen(url, req, TIMEOUT)
00217                 except EnvironmentError as err:
00218                     debug(err)
00219                     if expected_err:
00220                         msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
00221                                (expected_err, url, req, type(err), err))
00222                         self.assertIsInstance(err, expected_err, msg)
00223                 except urllib.error.URLError as err:
00224                     if isinstance(err[0], socket.timeout):
00225                         print("<timeout: %s>" % url, file=sys.stderr)
00226                         continue
00227                     else:
00228                         raise
00229                 else:
00230                     try:
00231                         with support.time_out, \
00232                              support.socket_peer_reset, \
00233                              support.ioerror_peer_reset:
00234                             buf = f.read()
00235                             debug("read %d bytes" % len(buf))
00236                     except socket.timeout:
00237                         print("<timeout: %s>" % url, file=sys.stderr)
00238                     f.close()
00239             debug("******** next url coming up...")
00240             time.sleep(0.1)

Here is the call graph for this function:

def test.test_urllib2net._wrap_with_retry_thrice (   func,
  exc 
) [private]

Definition at line 31 of file test_urllib2net.py.

00031 
00032 def _wrap_with_retry_thrice(func, exc):
00033     def wrapped(*args, **kwargs):
00034         return _retry_thrice(func, exc, *args, **kwargs)
00035     return wrapped
00036 
00037 # Connecting to remote hosts is flaky.  Make it more robust by retrying
# the connection several times.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 164 of file test_urllib2net.py.

00164 
00165     def test_custom_headers(self):
00166         url = "http://www.example.com"
00167         with support.transient_internet(url):
00168             opener = urllib.request.build_opener()
00169             request = urllib.request.Request(url)
00170             self.assertFalse(request.header_items())
00171             opener.open(request)
00172             self.assertTrue(request.header_items())
00173             self.assertTrue(request.has_header('User-agent'))
00174             request.add_header('User-Agent','Test-Agent')
00175             opener.open(request)
00176             self.assertEqual(request.get_header('User-agent'),'Test-Agent')

Here is the call graph for this function:

Definition at line 351 of file test_urllib2net.py.

00351 
00352 def test_main():
00353     support.requires("network")
00354     support.run_unittest(AuthTests,
00355                          HTTPSTests,
00356                          OtherNetworkTests,
00357                          CloseSocketTest,
00358                          TimeoutTest,
00359                          )

Definition at line 177 of file test_urllib2net.py.

00177 
00178     def test_sites_no_connection_close(self):
00179         # Some sites do not send Connection: close header.
00180         # Verify that those work properly. (#issue12576)
00181 
00182         URL = 'http://www.imdb.com' # mangles Connection:close
00183 
00184         with support.transient_internet(URL):
00185             try:
00186                 with urllib.request.urlopen(URL) as res:
00187                     pass
00188             except ValueError as e:
00189                 self.fail("urlopen failed for site not sending \
00190                            Connection:close")
00191             else:
00192                 self.assertTrue(res)
00193 
00194             req = urllib.request.urlopen(URL)
00195             res = req.read()
00196             self.assertTrue(res)

Here is the call graph for this function:

def test_cnri(self): if socket.gethostname() == 'bitdiddle': localhost = 'bitdiddle.cnri.reston.va.us' elif socket.gethostname() == 'bitdiddle.concentric.net': localhost = 'localhost' else: localhost = None if localhost is not None: urls = [ 'file://%s/etc/passwd' % localhost, 'http://%s/simple/' % localhost, 'http://%s/digest/' % localhost, 'http://%s/not/found.h' % localhost, ]

bauth = HTTPBasicAuthHandler() bauth.add_password('basic_test_realm', localhost, 'jhylton', 'password') dauth = HTTPDigestAuthHandler() dauth.add_password('digest_test_realm', localhost, 'jhylton', 'password') self._test_urls(urls, self._extra_handlers()+[bauth, dauth])

Definition at line 156 of file test_urllib2net.py.

00156 
00157     def test_urlwithfrag(self):
00158         urlwith_frag = "http://docs.python.org/glossary.html#glossary"
00159         with support.transient_internet(urlwith_frag):
00160             req = urllib.request.Request(urlwith_frag)
00161             res = urllib.request.urlopen(req)
00162             self.assertEqual(res.geturl(),
00163                     "http://docs.python.org/glossary.html#glossary")

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 _wrap_with_retry_thrice(urllib.request.urlopen,
00002                                               urllib.error.URLError)

Definition at line 38 of file test_urllib2net.py.

Definition at line 15 of file test_urllib2net.py.

Definition at line 17 of file test_urllib2net.py.