Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
test.test_urllib2.HandlerTests Class Reference

List of all members.

Public Member Functions

def test_ftp
def test_file
def test_http
def test_http_doubleslash
def test_fixpath_in_weirdurls
def test_errors
def test_cookies
def test_redirect
def test_invalid_redirect
def test_cookie_redirect
def test_redirect_fragment
def test_proxy
def test_proxy_no_proxy
def test_proxy_no_proxy_all
def test_proxy_https
def test_proxy_https_proxy_authorization
def test_osx_proxy_bypass
def test_basic_auth
def test_basic_auth_with_single_quoted_realm
def test_proxy_basic_auth
def test_basic_and_digest_auth_handlers

Public Attributes

 data
 filetype
 passwd
 port
 dirs
 ftpwrapper
 recorded

Private Member Functions

def _test_basic_auth

Detailed Description

Definition at line 617 of file test_urllib2.py.


Member Function Documentation

def test.test_urllib2.HandlerTests._test_basic_auth (   self,
  opener,
  auth_handler,
  auth_header,
  realm,
  http_handler,
  password_manager,
  request_url,
  protected_url 
) [private]

Definition at line 1274 of file test_urllib2.py.

01274 
01275                          request_url, protected_url):
01276         import base64
01277         user, password = "wile", "coyote"
01278 
01279         # .add_password() fed through to password manager
01280         auth_handler.add_password(realm, request_url, user, password)
01281         self.assertEqual(realm, password_manager.realm)
01282         self.assertEqual(request_url, password_manager.url)
01283         self.assertEqual(user, password_manager.user)
01284         self.assertEqual(password, password_manager.password)
01285 
01286         r = opener.open(request_url)
01287 
01288         # should have asked the password manager for the username/password
01289         self.assertEqual(password_manager.target_realm, realm)
01290         self.assertEqual(password_manager.target_url, protected_url)
01291 
01292         # expect one request without authorization, then one with
01293         self.assertEqual(len(http_handler.requests), 2)
01294         self.assertFalse(http_handler.requests[0].has_header(auth_header))
01295         userpass = bytes('%s:%s' % (user, password), "ascii")
01296         auth_hdr_value = ('Basic ' +
01297             base64.encodebytes(userpass).strip().decode())
01298         self.assertEqual(http_handler.requests[1].get_header(auth_header),
01299                          auth_hdr_value)
01300         self.assertEqual(http_handler.requests[1].unredirected_hdrs[auth_header],
01301                          auth_hdr_value)
01302         # if the password manager can't find a password, the handler won't
01303         # handle the HTTP auth error
01304         password_manager.user = password_manager.password = None
01305         http_handler.reset()
01306         r = opener.open(request_url)
01307         self.assertEqual(len(http_handler.requests), 1)
01308         self.assertFalse(http_handler.requests[0].has_header(auth_header))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1225 of file test_urllib2.py.

01225 
01226     def test_basic_and_digest_auth_handlers(self):
01227         # HTTPDigestAuthHandler threw an exception if it couldn't handle a 40*
01228         # response (http://python.org/sf/1479302), where it should instead
01229         # return None to allow another handler (especially
01230         # HTTPBasicAuthHandler) to handle the response.
01231 
01232         # Also (http://python.org/sf/14797027, RFC 2617 section 1.2), we must
01233         # try digest first (since it's the strongest auth scheme), so we record
01234         # order of calls here to check digest comes first:
01235         class RecordingOpenerDirector(OpenerDirector):
01236             def __init__(self):
01237                 OpenerDirector.__init__(self)
01238                 self.recorded = []
01239             def record(self, info):
01240                 self.recorded.append(info)
01241         class TestDigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
01242             def http_error_401(self, *args, **kwds):
01243                 self.parent.record("digest")
01244                 urllib.request.HTTPDigestAuthHandler.http_error_401(self,
01245                                                              *args, **kwds)
01246         class TestBasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
01247             def http_error_401(self, *args, **kwds):
01248                 self.parent.record("basic")
01249                 urllib.request.HTTPBasicAuthHandler.http_error_401(self,
01250                                                             *args, **kwds)
01251 
01252         opener = RecordingOpenerDirector()
01253         password_manager = MockPasswordManager()
01254         digest_handler = TestDigestAuthHandler(password_manager)
01255         basic_handler = TestBasicAuthHandler(password_manager)
01256         realm = "ACME Networks"
01257         http_handler = MockHTTPHandler(
01258             401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
01259         opener.add_handler(basic_handler)
01260         opener.add_handler(digest_handler)
01261         opener.add_handler(http_handler)
01262 
01263         # check basic auth isn't blocked by digest handler failing
01264         self._test_basic_auth(opener, basic_handler, "Authorization",
01265                               realm, http_handler, password_manager,
01266                               "http://acme.example.com/protected",
01267                               "http://acme.example.com/protected",
01268                               )
01269         # check digest was tried before basic (twice, because
01270         # _test_basic_auth called .open() twice)
01271         self.assertEqual(opener.recorded, ["digest", "basic"]*2)

Here is the call graph for this function:

def test.test_urllib2.HandlerTests.test_basic_auth (   self,
  quote_char = '"' 
)

Definition at line 1189 of file test_urllib2.py.

01189 
01190     def test_basic_auth(self, quote_char='"'):
01191         opener = OpenerDirector()
01192         password_manager = MockPasswordManager()
01193         auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
01194         realm = "ACME Widget Store"
01195         http_handler = MockHTTPHandler(
01196             401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
01197             (quote_char, realm, quote_char) )
01198         opener.add_handler(auth_handler)
01199         opener.add_handler(http_handler)
01200         self._test_basic_auth(opener, auth_handler, "Authorization",
01201                               realm, http_handler, password_manager,
01202                               "http://acme.example.com/protected",
01203                               "http://acme.example.com/protected",
01204                               )

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1205 of file test_urllib2.py.

01205 
01206     def test_basic_auth_with_single_quoted_realm(self):
01207         self.test_basic_auth(quote_char="'")

Here is the call graph for this function:

Definition at line 1062 of file test_urllib2.py.

01062 
01063     def test_cookie_redirect(self):
01064         # cookies shouldn't leak into redirected requests
01065         from http.cookiejar import CookieJar
01066         from test.test_http_cookiejar import interact_netscape
01067 
01068         cj = CookieJar()
01069         interact_netscape(cj, "http://www.example.com/", "spam=eggs")
01070         hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
01071         hdeh = urllib.request.HTTPDefaultErrorHandler()
01072         hrh = urllib.request.HTTPRedirectHandler()
01073         cp = urllib.request.HTTPCookieProcessor(cj)
01074         o = build_test_opener(hh, hdeh, hrh, cp)
01075         o.open("http://www.example.com/")
01076         self.assertFalse(hh.req.has_header("Cookie"))

Here is the call graph for this function:

Definition at line 950 of file test_urllib2.py.

00950 
00951     def test_cookies(self):
00952         cj = MockCookieJar()
00953         h = urllib.request.HTTPCookieProcessor(cj)
00954         o = h.parent = MockOpener()
00955 
00956         req = Request("http://example.com/")
00957         r = MockResponse(200, "OK", {}, "")
00958         newreq = h.http_request(req)
00959         self.assertIs(cj.ach_req, req)
00960         self.assertIs(cj.ach_req, newreq)
00961         self.assertEqual(req.get_origin_req_host(), "example.com")
00962         self.assertFalse(req.is_unverifiable())
00963         newr = h.http_response(req, r)
00964         self.assertIs(cj.ec_req, req)
00965         self.assertIs(cj.ec_r, r)
00966         self.assertIs(r, newr)

Here is the call graph for this function:

Definition at line 925 of file test_urllib2.py.

00925 
00926     def test_errors(self):
00927         h = urllib.request.HTTPErrorProcessor()
00928         o = h.parent = MockOpener()
00929 
00930         url = "http://example.com/"
00931         req = Request(url)
00932         # all 2xx are passed through
00933         r = MockResponse(200, "OK", {}, "", url)
00934         newr = h.http_response(req, r)
00935         self.assertIs(r, newr)
00936         self.assertFalse(hasattr(o, "proto"))  # o.error not called
00937         r = MockResponse(202, "Accepted", {}, "", url)
00938         newr = h.http_response(req, r)
00939         self.assertIs(r, newr)
00940         self.assertFalse(hasattr(o, "proto"))  # o.error not called
00941         r = MockResponse(206, "Partial content", {}, "", url)
00942         newr = h.http_response(req, r)
00943         self.assertIs(r, newr)
00944         self.assertFalse(hasattr(o, "proto"))  # o.error not called
00945         # anything else calls o.error (and MockOpener returns None, here)
00946         r = MockResponse(502, "Bad gateway", {}, "", url)
00947         self.assertIsNone(h.http_response(req, r))
00948         self.assertEqual(o.proto, "http")  # o.error called
00949         self.assertEqual(o.args, (req, r, 502, "Bad gateway", {}))

Here is the call graph for this function:

Definition at line 677 of file test_urllib2.py.

00677 
00678     def test_file(self):
00679         import email.utils, socket
00680         h = urllib.request.FileHandler()
00681         o = h.parent = MockOpener()
00682 
00683         TESTFN = support.TESTFN
00684         urlpath = sanepathname2url(os.path.abspath(TESTFN))
00685         towrite = b"hello, world\n"
00686         urls = [
00687             "file://localhost%s" % urlpath,
00688             "file://%s" % urlpath,
00689             "file://%s%s" % (socket.gethostbyname('localhost'), urlpath),
00690             ]
00691         try:
00692             localaddr = socket.gethostbyname(socket.gethostname())
00693         except socket.gaierror:
00694             localaddr = ''
00695         if localaddr:
00696             urls.append("file://%s%s" % (localaddr, urlpath))
00697 
00698         for url in urls:
00699             f = open(TESTFN, "wb")
00700             try:
00701                 try:
00702                     f.write(towrite)
00703                 finally:
00704                     f.close()
00705 
00706                 r = h.file_open(Request(url))
00707                 try:
00708                     data = r.read()
00709                     headers = r.info()
00710                     respurl = r.geturl()
00711                 finally:
00712                     r.close()
00713                 stats = os.stat(TESTFN)
00714                 modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
00715             finally:
00716                 os.remove(TESTFN)
00717             self.assertEqual(data, towrite)
00718             self.assertEqual(headers["Content-type"], "text/plain")
00719             self.assertEqual(headers["Content-length"], "13")
00720             self.assertEqual(headers["Last-modified"], modified)
00721             self.assertEqual(respurl, url)
00722 
00723         for url in [
00724             "file://localhost:80%s" % urlpath,
00725             "file:///file_does_not_exist.txt",
00726             "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
00727                                    os.getcwd(), TESTFN),
00728             "file://somerandomhost.ontheinternet.com%s/%s" %
00729             (os.getcwd(), TESTFN),
00730             ]:
00731             try:
00732                 f = open(TESTFN, "wb")
00733                 try:
00734                     f.write(towrite)
00735                 finally:
00736                     f.close()
00737 
00738                 self.assertRaises(urllib.error.URLError,
00739                                   h.file_open, Request(url))
00740             finally:
00741                 os.remove(TESTFN)
00742 
00743         h = urllib.request.FileHandler()
00744         o = h.parent = MockOpener()
00745         # XXXX why does // mean ftp (and /// mean not ftp!), and where
00746         #  is file: scheme specified?  I think this is really a bug, and
00747         #  what was intended was to distinguish between URLs like:
00748         # file:/blah.txt (a file)
00749         # file://localhost/blah.txt (a file)
00750         # file:///blah.txt (a file)
00751         # file://ftp.example.com/blah.txt (an ftp URL)
00752         for url, ftp in [
00753             ("file://ftp.example.com//foo.txt", False),
00754             ("file://ftp.example.com///foo.txt", False),
00755 # XXXX bug: fails with OSError, should be URLError
00756             ("file://ftp.example.com/foo.txt", False),
00757             ("file://somehost//foo/something.txt", False),
00758             ("file://localhost//foo/something.txt", False),
00759             ]:
00760             req = Request(url)
00761             try:
00762                 h.file_open(req)
00763             # XXXX remove OSError when bug fixed
00764             except (urllib.error.URLError, OSError):
00765                 self.assertFalse(ftp)
00766             else:
00767                 self.assertIs(o.req, req)
00768                 self.assertEqual(req.type, "ftp")
00769             self.assertEqual(req.type == "ftp", ftp)

Here is the call graph for this function:

Definition at line 905 of file test_urllib2.py.

00905 
00906     def test_fixpath_in_weirdurls(self):
00907         # Issue4493: urllib2 to supply '/' when to urls where path does not
00908         # start with'/'
00909 
00910         h = urllib.request.AbstractHTTPHandler()
00911         o = h.parent = MockOpener()
00912 
00913         weird_url = 'http://www.python.org?getspam'
00914         req = Request(weird_url)
00915         newreq = h.do_request_(req)
00916         self.assertEqual(newreq.host,'www.python.org')
00917         self.assertEqual(newreq.selector,'/?getspam')
00918 
00919         url_without_path = 'http://www.python.org'
00920         req = Request(url_without_path)
00921         newreq = h.do_request_(req)
00922         self.assertEqual(newreq.host,'www.python.org')
00923         self.assertEqual(newreq.selector,'')
00924 

Here is the call graph for this function:

Definition at line 619 of file test_urllib2.py.

00619 
00620     def test_ftp(self):
00621         class MockFTPWrapper:
00622             def __init__(self, data): self.data = data
00623             def retrfile(self, filename, filetype):
00624                 self.filename, self.filetype = filename, filetype
00625                 return io.StringIO(self.data), len(self.data)
00626             def close(self): pass
00627 
00628         class NullFTPHandler(urllib.request.FTPHandler):
00629             def __init__(self, data): self.data = data
00630             def connect_ftp(self, user, passwd, host, port, dirs,
00631                             timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
00632                 self.user, self.passwd = user, passwd
00633                 self.host, self.port = host, port
00634                 self.dirs = dirs
00635                 self.ftpwrapper = MockFTPWrapper(self.data)
00636                 return self.ftpwrapper
00637 
00638         import ftplib
00639         data = "rheum rhaponicum"
00640         h = NullFTPHandler(data)
00641         o = h.parent = MockOpener()
00642 
00643         for url, host, port, user, passwd, type_, dirs, filename, mimetype in [
00644             ("ftp://localhost/foo/bar/baz.html",
00645              "localhost", ftplib.FTP_PORT, "", "", "I",
00646              ["foo", "bar"], "baz.html", "text/html"),
00647             ("ftp://parrot@localhost/foo/bar/baz.html",
00648              "localhost", ftplib.FTP_PORT, "parrot", "", "I",
00649              ["foo", "bar"], "baz.html", "text/html"),
00650             ("ftp://%25parrot@localhost/foo/bar/baz.html",
00651              "localhost", ftplib.FTP_PORT, "%parrot", "", "I",
00652              ["foo", "bar"], "baz.html", "text/html"),
00653             ("ftp://%2542parrot@localhost/foo/bar/baz.html",
00654              "localhost", ftplib.FTP_PORT, "%42parrot", "", "I",
00655              ["foo", "bar"], "baz.html", "text/html"),
00656             ("ftp://localhost:80/foo/bar/",
00657              "localhost", 80, "", "", "D",
00658              ["foo", "bar"], "", None),
00659             ("ftp://localhost/baz.gif;type=a",
00660              "localhost", ftplib.FTP_PORT, "", "", "A",
00661              [], "baz.gif", None),  # XXX really this should guess image/gif
00662             ]:
00663             req = Request(url)
00664             req.timeout = None
00665             r = h.ftp_open(req)
00666             # ftp authentication not yet implemented by FTPHandler
00667             self.assertEqual(h.user, user)
00668             self.assertEqual(h.passwd, passwd)
00669             self.assertEqual(h.host, socket.gethostbyname(host))
00670             self.assertEqual(h.port, port)
00671             self.assertEqual(h.dirs, dirs)
00672             self.assertEqual(h.ftpwrapper.filename, filename)
00673             self.assertEqual(h.ftpwrapper.filetype, type_)
00674             headers = r.info()
00675             self.assertEqual(headers.get("Content-type"), mimetype)
00676             self.assertEqual(int(headers["Content-length"]), len(data))

Definition at line 770 of file test_urllib2.py.

00770 
00771     def test_http(self):
00772 
00773         h = urllib.request.AbstractHTTPHandler()
00774         o = h.parent = MockOpener()
00775 
00776         url = "http://example.com/"
00777         for method, data in [("GET", None), ("POST", b"blah")]:
00778             req = Request(url, data, {"Foo": "bar"})
00779             req.timeout = None
00780             req.add_unredirected_header("Spam", "eggs")
00781             http = MockHTTPClass()
00782             r = h.do_open(http, req)
00783 
00784             # result attributes
00785             r.read; r.readline  # wrapped MockFile methods
00786             r.info; r.geturl  # addinfourl methods
00787             r.code, r.msg == 200, "OK"  # added from MockHTTPClass.getreply()
00788             hdrs = r.info()
00789             hdrs.get; hdrs.__contains__  # r.info() gives dict from .getreply()
00790             self.assertEqual(r.geturl(), url)
00791 
00792             self.assertEqual(http.host, "example.com")
00793             self.assertEqual(http.level, 0)
00794             self.assertEqual(http.method, method)
00795             self.assertEqual(http.selector, "/")
00796             self.assertEqual(http.req_headers,
00797                              [("Connection", "close"),
00798                               ("Foo", "bar"), ("Spam", "eggs")])
00799             self.assertEqual(http.data, data)
00800 
00801         # check socket.error converted to URLError
00802         http.raise_on_endheaders = True
00803         self.assertRaises(urllib.error.URLError, h.do_open, http, req)
00804 
00805         # Check for TypeError on POST data which is str.
00806         req = Request("http://example.com/","badpost")
00807         self.assertRaises(TypeError, h.do_request_, req)
00808 
00809         # check adding of standard headers
00810         o.addheaders = [("Spam", "eggs")]
00811         for data in b"", None:  # POST, GET
00812             req = Request("http://example.com/", data)
00813             r = MockResponse(200, "OK", {}, "")
00814             newreq = h.do_request_(req)
00815             if data is None:  # GET
00816                 self.assertNotIn("Content-length", req.unredirected_hdrs)
00817                 self.assertNotIn("Content-type", req.unredirected_hdrs)
00818             else:  # POST
00819                 self.assertEqual(req.unredirected_hdrs["Content-length"], "0")
00820                 self.assertEqual(req.unredirected_hdrs["Content-type"],
00821                              "application/x-www-form-urlencoded")
00822             # XXX the details of Host could be better tested
00823             self.assertEqual(req.unredirected_hdrs["Host"], "example.com")
00824             self.assertEqual(req.unredirected_hdrs["Spam"], "eggs")
00825 
00826             # don't clobber existing headers
00827             req.add_unredirected_header("Content-length", "foo")
00828             req.add_unredirected_header("Content-type", "bar")
00829             req.add_unredirected_header("Host", "baz")
00830             req.add_unredirected_header("Spam", "foo")
00831             newreq = h.do_request_(req)
00832             self.assertEqual(req.unredirected_hdrs["Content-length"], "foo")
00833             self.assertEqual(req.unredirected_hdrs["Content-type"], "bar")
00834             self.assertEqual(req.unredirected_hdrs["Host"], "baz")
00835             self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
00836 
00837         # Check iterable body support
00838         def iterable_body():
00839             yield b"one"
00840             yield b"two"
00841             yield b"three"
00842 
00843         for headers in {}, {"Content-Length": 11}:
00844             req = Request("http://example.com/", iterable_body(), headers)
00845             if not headers:
00846                 # Having an iterable body without a Content-Length should
00847                 # raise an exception
00848                 self.assertRaises(ValueError, h.do_request_, req)
00849             else:
00850                 newreq = h.do_request_(req)
00851 
00852         # A file object.
00853         # Test only Content-Length attribute of request.
00854 
00855         file_obj = io.BytesIO()
00856         file_obj.write(b"Something\nSomething\nSomething\n")
00857 
00858         for headers in {}, {"Content-Length": 30}:
00859             req = Request("http://example.com/", file_obj, headers)
00860             if not headers:
00861                 # Having an iterable body without a Content-Length should
00862                 # raise an exception
00863                 self.assertRaises(ValueError, h.do_request_, req)
00864             else:
00865                 newreq = h.do_request_(req)
00866                 self.assertEqual(int(newreq.get_header('Content-length')),30)
00867 
00868         file_obj.close()
00869 
00870         # array.array Iterable - Content Length is calculated
00871 
00872         iterable_array = array.array("I",[1,2,3,4])
00873 
00874         for headers in {}, {"Content-Length": 16}:
00875             req = Request("http://example.com/", iterable_array, headers)
00876             newreq = h.do_request_(req)
00877             self.assertEqual(int(newreq.get_header('Content-length')),16)

Here is the call graph for this function:

Definition at line 878 of file test_urllib2.py.

00878 
00879     def test_http_doubleslash(self):
00880         # Checks the presence of any unnecessary double slash in url does not
00881         # break anything. Previously, a double slash directly after the host
00882         # could could cause incorrect parsing.
00883         h = urllib.request.AbstractHTTPHandler()
00884         o = h.parent = MockOpener()
00885 
00886         data = b""
00887         ds_urls = [
00888             "http://example.com/foo/bar/baz.html",
00889             "http://example.com//foo/bar/baz.html",
00890             "http://example.com/foo//bar/baz.html",
00891             "http://example.com/foo/bar//baz.html"
00892             ]
00893 
00894         for ds_url in ds_urls:
00895             ds_req = Request(ds_url, data)
00896 
00897             # Check whether host is determined correctly if there is no proxy
00898             np_ds_req = h.do_request_(ds_req)
00899             self.assertEqual(np_ds_req.unredirected_hdrs["Host"],"example.com")
00900 
00901             # Check whether host is determined correctly if there is a proxy
00902             ds_req.set_proxy("someproxy:3128",None)
00903             p_ds_req = h.do_request_(ds_req)
00904             self.assertEqual(p_ds_req.unredirected_hdrs["Host"],"example.com")

Here is the call graph for this function:

Definition at line 1040 of file test_urllib2.py.

01040 
01041     def test_invalid_redirect(self):
01042         from_url = "http://example.com/a.html"
01043         valid_schemes = ['http','https','ftp']
01044         invalid_schemes = ['file','imap','ldap']
01045         schemeless_url = "example.com/b.html"
01046         h = urllib.request.HTTPRedirectHandler()
01047         o = h.parent = MockOpener()
01048         req = Request(from_url)
01049         req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
01050 
01051         for scheme in invalid_schemes:
01052             invalid_url = scheme + '://' + schemeless_url
01053             self.assertRaises(urllib.error.HTTPError, h.http_error_302,
01054                     req, MockFile(), 302, "Security Loophole",
01055                     MockHeaders({"location": invalid_url}))
01056 
01057         for scheme in valid_schemes:
01058             valid_url = scheme + '://' + schemeless_url
01059             h.http_error_302(req, MockFile(), 302, "That's fine",
01060                 MockHeaders({"location": valid_url}))
01061             self.assertEqual(o.req.get_full_url(), valid_url)

Here is the call graph for this function:

Definition at line 1169 of file test_urllib2.py.

01169 
01170     def test_osx_proxy_bypass(self):
01171         bypass = {
01172             'exclude_simple': False,
01173             'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.10',
01174                            '10.0/16']
01175         }
01176         # Check hosts that should trigger the proxy bypass
01177         for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.10.0.1',
01178                      '10.0.0.1'):
01179             self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
01180                             'expected bypass of %s to be True' % host)
01181         # Check hosts that should not trigger the proxy bypass
01182         for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1', 'test'):
01183             self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
01184                              'expected bypass of %s to be False' % host)
01185 
01186         # Check the exclude_simple flag
01187         bypass = {'exclude_simple': True, 'exceptions': []}
01188         self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass))

Here is the call graph for this function:

Definition at line 1086 of file test_urllib2.py.

01086 
01087     def test_proxy(self):
01088         o = OpenerDirector()
01089         ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
01090         o.add_handler(ph)
01091         meth_spec = [
01092             [("http_open", "return response")]
01093             ]
01094         handlers = add_ordered_mock_handlers(o, meth_spec)
01095 
01096         req = Request("http://acme.example.com/")
01097         self.assertEqual(req.get_host(), "acme.example.com")
01098         r = o.open(req)
01099         self.assertEqual(req.get_host(), "proxy.example.com:3128")
01100 
01101         self.assertEqual([(handlers[0], "http_open")],
01102                          [tup[0:2] for tup in o.calls])

Here is the call graph for this function:

Definition at line 1208 of file test_urllib2.py.

01208 
01209     def test_proxy_basic_auth(self):
01210         opener = OpenerDirector()
01211         ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
01212         opener.add_handler(ph)
01213         password_manager = MockPasswordManager()
01214         auth_handler = urllib.request.ProxyBasicAuthHandler(password_manager)
01215         realm = "ACME Networks"
01216         http_handler = MockHTTPHandler(
01217             407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
01218         opener.add_handler(auth_handler)
01219         opener.add_handler(http_handler)
01220         self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
01221                               realm, http_handler, password_manager,
01222                               "http://acme.example.com:3128/protected",
01223                               "proxy.example.com:3128",
01224                               )

Here is the call graph for this function:

Definition at line 1130 of file test_urllib2.py.

01130 
01131     def test_proxy_https(self):
01132         o = OpenerDirector()
01133         ph = urllib.request.ProxyHandler(dict(https="proxy.example.com:3128"))
01134         o.add_handler(ph)
01135         meth_spec = [
01136             [("https_open", "return response")]
01137         ]
01138         handlers = add_ordered_mock_handlers(o, meth_spec)
01139 
01140         req = Request("https://www.example.com/")
01141         self.assertEqual(req.get_host(), "www.example.com")
01142         r = o.open(req)
01143         self.assertEqual(req.get_host(), "proxy.example.com:3128")
01144         self.assertEqual([(handlers[0], "https_open")],
01145                          [tup[0:2] for tup in o.calls])

Here is the call graph for this function:

Definition at line 1146 of file test_urllib2.py.

01146 
01147     def test_proxy_https_proxy_authorization(self):
01148         o = OpenerDirector()
01149         ph = urllib.request.ProxyHandler(dict(https='proxy.example.com:3128'))
01150         o.add_handler(ph)
01151         https_handler = MockHTTPSHandler()
01152         o.add_handler(https_handler)
01153         req = Request("https://www.example.com/")
01154         req.add_header("Proxy-Authorization","FooBar")
01155         req.add_header("User-Agent","Grail")
01156         self.assertEqual(req.get_host(), "www.example.com")
01157         self.assertIsNone(req._tunnel_host)
01158         r = o.open(req)
01159         # Verify Proxy-Authorization gets tunneled to request.
01160         # httpsconn req_headers do not have the Proxy-Authorization header but
01161         # the req will have.
01162         self.assertNotIn(("Proxy-Authorization","FooBar"),
01163                          https_handler.httpconn.req_headers)
01164         self.assertIn(("User-Agent","Grail"),
01165                       https_handler.httpconn.req_headers)
01166         self.assertIsNotNone(req._tunnel_host)
01167         self.assertEqual(req.get_host(), "proxy.example.com:3128")
01168         self.assertEqual(req.get_header("Proxy-authorization"),"FooBar")

Here is the call graph for this function:

Definition at line 1103 of file test_urllib2.py.

01103 
01104     def test_proxy_no_proxy(self):
01105         os.environ['no_proxy'] = 'python.org'
01106         o = OpenerDirector()
01107         ph = urllib.request.ProxyHandler(dict(http="proxy.example.com"))
01108         o.add_handler(ph)
01109         req = Request("http://www.perl.org/")
01110         self.assertEqual(req.get_host(), "www.perl.org")
01111         r = o.open(req)
01112         self.assertEqual(req.get_host(), "proxy.example.com")
01113         req = Request("http://www.python.org")
01114         self.assertEqual(req.get_host(), "www.python.org")
01115         r = o.open(req)
01116         self.assertEqual(req.get_host(), "www.python.org")
01117         del os.environ['no_proxy']

Here is the call graph for this function:

Definition at line 1118 of file test_urllib2.py.

01118 
01119     def test_proxy_no_proxy_all(self):
01120         os.environ['no_proxy'] = '*'
01121         o = OpenerDirector()
01122         ph = urllib.request.ProxyHandler(dict(http="proxy.example.com"))
01123         o.add_handler(ph)
01124         req = Request("http://www.python.org")
01125         self.assertEqual(req.get_host(), "www.python.org")
01126         r = o.open(req)
01127         self.assertEqual(req.get_host(), "www.python.org")
01128         del os.environ['no_proxy']
01129 

Here is the call graph for this function:

Definition at line 967 of file test_urllib2.py.

00967 
00968     def test_redirect(self):
00969         from_url = "http://example.com/a.html"
00970         to_url = "http://example.com/b.html"
00971         h = urllib.request.HTTPRedirectHandler()
00972         o = h.parent = MockOpener()
00973 
00974         # ordinary redirect behaviour
00975         for code in 301, 302, 303, 307:
00976             for data in None, "blah\nblah\n":
00977                 method = getattr(h, "http_error_%s" % code)
00978                 req = Request(from_url, data)
00979                 req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
00980                 req.add_header("Nonsense", "viking=withhold")
00981                 if data is not None:
00982                     req.add_header("Content-Length", str(len(data)))
00983                 req.add_unredirected_header("Spam", "spam")
00984                 try:
00985                     method(req, MockFile(), code, "Blah",
00986                            MockHeaders({"location": to_url}))
00987                 except urllib.error.HTTPError:
00988                     # 307 in response to POST requires user OK
00989                     self.assertTrue(code == 307 and data is not None)
00990                 self.assertEqual(o.req.get_full_url(), to_url)
00991                 try:
00992                     self.assertEqual(o.req.get_method(), "GET")
00993                 except AttributeError:
00994                     self.assertFalse(o.req.has_data())
00995 
00996                 # now it's a GET, there should not be headers regarding content
00997                 # (possibly dragged from before being a POST)
00998                 headers = [x.lower() for x in o.req.headers]
00999                 self.assertNotIn("content-length", headers)
01000                 self.assertNotIn("content-type", headers)
01001 
01002                 self.assertEqual(o.req.headers["Nonsense"],
01003                                  "viking=withhold")
01004                 self.assertNotIn("Spam", o.req.headers)
01005                 self.assertNotIn("Spam", o.req.unredirected_hdrs)
01006 
01007         # loop detection
01008         req = Request(from_url)
01009         req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
01010         def redirect(h, req, url=to_url):
01011             h.http_error_302(req, MockFile(), 302, "Blah",
01012                              MockHeaders({"location": url}))
01013         # Note that the *original* request shares the same record of
01014         # redirections with the sub-requests caused by the redirections.
01015 
01016         # detect infinite loop redirect of a URL to itself
01017         req = Request(from_url, origin_req_host="example.com")
01018         count = 0
01019         req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
01020         try:
01021             while 1:
01022                 redirect(h, req, "http://example.com/")
01023                 count = count + 1
01024         except urllib.error.HTTPError:
01025             # don't stop until max_repeats, because cookies may introduce state
01026             self.assertEqual(count, urllib.request.HTTPRedirectHandler.max_repeats)
01027 
01028         # detect endless non-repeating chain of redirects
01029         req = Request(from_url, origin_req_host="example.com")
01030         count = 0
01031         req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
01032         try:
01033             while 1:
01034                 redirect(h, req, "http://example.com/%d" % count)
01035                 count = count + 1
01036         except urllib.error.HTTPError:
01037             self.assertEqual(count,
01038                              urllib.request.HTTPRedirectHandler.max_redirections)
01039 

Here is the call graph for this function:

Definition at line 1077 of file test_urllib2.py.

01077 
01078     def test_redirect_fragment(self):
01079         redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
01080         hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
01081         hdeh = urllib.request.HTTPDefaultErrorHandler()
01082         hrh = urllib.request.HTTPRedirectHandler()
01083         o = build_test_opener(hh, hdeh, hrh)
01084         fp = o.open('http://www.example.com')
01085         self.assertEqual(fp.geturl(), redirected_url.strip())

Here is the call graph for this function:


Member Data Documentation

Definition at line 621 of file test_urllib2.py.

Definition at line 633 of file test_urllib2.py.

Definition at line 623 of file test_urllib2.py.

Definition at line 634 of file test_urllib2.py.

Definition at line 631 of file test_urllib2.py.

Definition at line 632 of file test_urllib2.py.

Definition at line 1237 of file test_urllib2.py.


The documentation for this class was generated from the following file: