<s> import unittest <EOL> from test import support <EOL> import os <EOL> import io <EOL> import socket <EOL> import array <EOL> import sys <EOL> import urllib . request <EOL> from urllib . request import Request , OpenerDirector , _proxy_bypass_macosx_sysconf <EOL> import urllib . error <EOL> class TrivialTests ( unittest . TestCase ) : <EOL> def test___all__ ( self ) : <EOL> for module in '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' : <EOL> context = { } <EOL> exec ( '<STR_LIT>' % module , context ) <EOL> del context [ '<STR_LIT>' ] <EOL> if module == '<STR_LIT>' and os . name == '<STR_LIT>' : <EOL> u , p = context . pop ( '<STR_LIT>' ) , context . pop ( '<STR_LIT>' ) <EOL> self . assertEqual ( u . __module__ , '<STR_LIT>' ) <EOL> self . assertEqual ( p . __module__ , '<STR_LIT>' ) <EOL> for k , v in context . items ( ) : <EOL> self . assertEqual ( v . __module__ , '<STR_LIT>' % module , <EOL> "<STR_LIT>" % <EOL> ( k , module , v . __module__ ) ) <EOL> def test_trivial ( self ) : <EOL> self . assertRaises ( ValueError , urllib . request . urlopen , '<STR_LIT>' ) <EOL> fname = os . path . abspath ( urllib . request . __file__ ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> if os . name == '<STR_LIT>' : <EOL> file_url = "<STR_LIT>" % fname <EOL> else : <EOL> file_url = "<STR_LIT>" % fname <EOL> f = urllib . request . urlopen ( file_url ) <EOL> f . read ( ) <EOL> f . close ( ) <EOL> def test_parse_http_list ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( '<STR_LIT>' , <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:g>' , '<STR_LIT:h>' ] ) , <EOL> ( '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ] <EOL> for string , list in tests : <EOL> self . assertEqual ( urllib . request . parse_http_list ( string ) , list ) <EOL> def test_URLError_reasonstr ( self ) : <EOL> err = urllib . error . URLError ( '<STR_LIT>' ) <EOL> self . assertIn ( err . reason , str ( err ) ) <EOL> class RequestHdrsTests ( unittest . TestCase ) : <EOL> def test_request_headers_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" <EOL> self . assertEqual ( Request ( url , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } <EOL> ) . headers [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( Request ( url , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } <EOL> ) . headers [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def test_request_headers_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" <EOL> req = Request ( url , headers = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertTrue ( req . has_header ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( req . header_items ( ) , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( sorted ( req . header_items ( ) ) , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertFalse ( req . has_header ( "<STR_LIT>" ) ) <EOL> self . assertIsNone ( req . get_header ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( req . get_header ( "<STR_LIT>" , "<STR_LIT:default>" ) , "<STR_LIT:default>" ) <EOL> def test_password_manager ( self ) : <EOL> mgr = urllib . request . HTTPPasswordMgr ( ) <EOL> add = mgr . add_password <EOL> find_user_pass = mgr . find_user_password <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" ) <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:c>" , "<STR_LIT>" , "<STR_LIT:foo>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:c>" , "<STR_LIT>" , "<STR_LIT:bar>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:b>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:b>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:a>" , "<STR_LIT>" , "<STR_LIT:1>" , "<STR_LIT:a>" ) <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:3>" , "<STR_LIT:c>" ) <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:4>" , "<STR_LIT:d>" ) <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:5>" , "<STR_LIT:e>" ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:password>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:password>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:password>' ) ) <EOL> self . assertEqual ( <EOL> find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:password>' ) ) <EOL> self . assertEqual ( <EOL> find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:password>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:c>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:c>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:b>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:a>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:1>' , '<STR_LIT:a>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:a>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:3>' , '<STR_LIT:c>' ) ) <EOL> self . assertEqual ( <EOL> find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:3>' , '<STR_LIT:c>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:4>' , '<STR_LIT:d>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT:5>' , '<STR_LIT:e>' ) ) <EOL> def test_password_manager_default_port ( self ) : <EOL> """<STR_LIT>""" <EOL> mgr = urllib . request . HTTPPasswordMgr ( ) <EOL> add = mgr . add_password <EOL> find_user_pass = mgr . find_user_password <EOL> add ( "<STR_LIT:f>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> add ( "<STR_LIT:g>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:k>" ) <EOL> add ( "<STR_LIT:h>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:l>" ) <EOL> add ( "<STR_LIT:i>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:m>" ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:f>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:g>" , "<STR_LIT>" ) , ( '<STR_LIT>' , '<STR_LIT:k>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:g>" , "<STR_LIT>" ) , ( '<STR_LIT>' , '<STR_LIT:k>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:g>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:k>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:h>" , "<STR_LIT>" ) , ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:h>" , "<STR_LIT>" ) , ( '<STR_LIT>' , '<STR_LIT:l>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:h>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:l>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:i>" , "<STR_LIT>" ) , ( '<STR_LIT>' , '<STR_LIT:m>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:i>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:i>" , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' ) ) <EOL> self . assertEqual ( find_user_pass ( "<STR_LIT:i>" , "<STR_LIT>" ) , <EOL> ( None , None ) ) <EOL> class MockOpener : <EOL> addheaders = [ ] <EOL> def open ( self , req , data = None , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . req , self . data , self . timeout = req , data , timeout <EOL> def error ( self , proto , * args ) : <EOL> self . proto , self . args = proto , args <EOL> class MockFile : <EOL> def read ( self , count = None ) : pass <EOL> def readline ( self , count = None ) : pass <EOL> def close ( self ) : pass <EOL> class MockHeaders ( dict ) : <EOL> def getheaders ( self , name ) : <EOL> return list ( self . values ( ) ) <EOL> class MockResponse ( io . StringIO ) : <EOL> def __init__ ( self , code , msg , headers , data , url = None ) : <EOL> io . StringIO . __init__ ( self , data ) <EOL> self . code , self . msg , self . headers , self . url = code , msg , headers , url <EOL> def info ( self ) : <EOL> return self . headers <EOL> def geturl ( self ) : <EOL> return self . url <EOL> class MockCookieJar : <EOL> def add_cookie_header ( self , request ) : <EOL> self . ach_req = request <EOL> def extract_cookies ( self , response , request ) : <EOL> self . ec_req , self . ec_r = request , response <EOL> class FakeMethod : <EOL> def __init__ ( self , meth_name , action , handle ) : <EOL> self . meth_name = meth_name <EOL> self . handle = handle <EOL> self . action = action <EOL> def __call__ ( self , * args ) : <EOL> return self . handle ( self . meth_name , self . action , * args ) <EOL> class MockHTTPResponse ( io . IOBase ) : <EOL> def __init__ ( self , fp , msg , status , reason ) : <EOL> self . fp = fp <EOL> self . msg = msg <EOL> self . status = status <EOL> self . reason = reason <EOL> self . code = <NUM_LIT:200> <EOL> def read ( self ) : <EOL> return '<STR_LIT>' <EOL> def info ( self ) : <EOL> return { } <EOL> def geturl ( self ) : <EOL> return self . url <EOL> class MockHTTPClass : <EOL> def __init__ ( self ) : <EOL> self . level = <NUM_LIT:0> <EOL> self . req_headers = [ ] <EOL> self . data = None <EOL> self . raise_on_endheaders = False <EOL> self . _tunnel_headers = { } <EOL> def __call__ ( self , host , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . host = host <EOL> self . timeout = timeout <EOL> return self <EOL> def set_debuglevel ( self , level ) : <EOL> self . level = level <EOL> def set_tunnel ( self , host , port = None , headers = None ) : <EOL> self . _tunnel_host = host <EOL> self . _tunnel_port = port <EOL> if headers : <EOL> self . _tunnel_headers = headers <EOL> else : <EOL> self . _tunnel_headers . clear ( ) <EOL> def request ( self , method , url , body = None , headers = None ) : <EOL> self . method = method <EOL> self . selector = url <EOL> if headers is not None : <EOL> self . req_headers += headers . items ( ) <EOL> self . req_headers . sort ( ) <EOL> if body : <EOL> self . data = body <EOL> if self . raise_on_endheaders : <EOL> import socket <EOL> raise socket . error ( ) <EOL> def getresponse ( self ) : <EOL> return MockHTTPResponse ( MockFile ( ) , { } , <NUM_LIT:200> , "<STR_LIT:OK>" ) <EOL> def close ( self ) : <EOL> pass <EOL> class MockHandler : <EOL> handler_order = <NUM_LIT> <EOL> def __init__ ( self , methods ) : <EOL> self . _define_methods ( methods ) <EOL> def _define_methods ( self , methods ) : <EOL> for spec in methods : <EOL> if len ( spec ) == <NUM_LIT:2> : name , action = spec <EOL> else : name , action = spec , None <EOL> meth = FakeMethod ( name , action , self . handle ) <EOL> setattr ( self . __class__ , name , meth ) <EOL> def handle ( self , fn_name , action , * args , ** kwds ) : <EOL> self . parent . calls . append ( ( self , fn_name , args , kwds ) ) <EOL> if action is None : <EOL> return None <EOL> elif action == "<STR_LIT>" : <EOL> return self <EOL> elif action == "<STR_LIT>" : <EOL> res = MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , { } , "<STR_LIT>" ) <EOL> return res <EOL> elif action == "<STR_LIT>" : <EOL> return Request ( "<STR_LIT>" ) <EOL> elif action . startswith ( "<STR_LIT:error>" ) : <EOL> code = action [ action . rfind ( "<STR_LIT:U+0020>" ) + <NUM_LIT:1> : ] <EOL> try : <EOL> code = int ( code ) <EOL> except ValueError : <EOL> pass <EOL> res = MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , { } , "<STR_LIT>" ) <EOL> return self . parent . error ( "<STR_LIT:http>" , args [ <NUM_LIT:0> ] , res , code , "<STR_LIT>" , { } ) <EOL> elif action == "<STR_LIT>" : <EOL> raise urllib . error . URLError ( "<STR_LIT>" ) <EOL> assert False <EOL> def close ( self ) : pass <EOL> def add_parent ( self , parent ) : <EOL> self . parent = parent <EOL> self . parent . calls = [ ] <EOL> def __lt__ ( self , other ) : <EOL> if not hasattr ( other , "<STR_LIT>" ) : <EOL> return True <EOL> return self . handler_order < other . handler_order <EOL> def add_ordered_mock_handlers ( opener , meth_spec ) : <EOL> """<STR_LIT>""" <EOL> handlers = [ ] <EOL> count = <NUM_LIT:0> <EOL> for meths in meth_spec : <EOL> class MockHandlerSubclass ( MockHandler ) : pass <EOL> h = MockHandlerSubclass ( meths ) <EOL> h . handler_order += count <EOL> h . add_parent ( opener ) <EOL> count = count + <NUM_LIT:1> <EOL> handlers . append ( h ) <EOL> opener . add_handler ( h ) <EOL> return handlers <EOL> def build_test_opener ( * handler_instances ) : <EOL> opener = OpenerDirector ( ) <EOL> for h in handler_instances : <EOL> opener . add_handler ( h ) <EOL> return opener <EOL> class MockHTTPHandler ( urllib . request . BaseHandler ) : <EOL> def __init__ ( self , code , headers ) : <EOL> self . code = code <EOL> self . headers = headers <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> self . _count = <NUM_LIT:0> <EOL> self . requests = [ ] <EOL> def http_open ( self , req ) : <EOL> import email , http . client , copy <EOL> self . requests . append ( copy . deepcopy ( req ) ) <EOL> if self . _count == <NUM_LIT:0> : <EOL> self . _count = self . _count + <NUM_LIT:1> <EOL> name = http . client . responses [ self . code ] <EOL> msg = email . message_from_string ( self . headers ) <EOL> return self . parent . error ( <EOL> "<STR_LIT:http>" , req , MockFile ( ) , self . code , name , msg ) <EOL> else : <EOL> self . req = req <EOL> msg = email . message_from_string ( "<STR_LIT>" ) <EOL> return MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , msg , "<STR_LIT>" , req . get_full_url ( ) ) <EOL> class MockHTTPSHandler ( urllib . request . AbstractHTTPHandler ) : <EOL> def __init__ ( self ) : <EOL> urllib . request . AbstractHTTPHandler . __init__ ( self ) <EOL> self . httpconn = MockHTTPClass ( ) <EOL> def https_open ( self , req ) : <EOL> return self . do_open ( self . httpconn , req ) <EOL> class MockPasswordManager : <EOL> def add_password ( self , realm , uri , user , password ) : <EOL> self . realm = realm <EOL> self . url = uri <EOL> self . user = user <EOL> self . password = password <EOL> def find_user_password ( self , realm , authuri ) : <EOL> self . target_realm = realm <EOL> self . target_url = authuri <EOL> return self . user , self . password <EOL> class OpenerDirectorTests ( unittest . TestCase ) : <EOL> def test_add_non_handler ( self ) : <EOL> class NonHandler ( object ) : <EOL> pass <EOL> self . assertRaises ( TypeError , <EOL> OpenerDirector ( ) . add_handler , NonHandler ( ) ) <EOL> def test_badly_named_methods ( self ) : <EOL> from urllib . error import URLError <EOL> o = OpenerDirector ( ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> ] <EOL> add_ordered_mock_handlers ( o , meth_spec ) <EOL> o . add_handler ( urllib . request . UnknownHandler ( ) ) <EOL> for scheme in "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" : <EOL> self . assertRaises ( URLError , o . open , scheme + "<STR_LIT>" ) <EOL> def test_handled ( self ) : <EOL> o = OpenerDirector ( ) <EOL> meth_spec = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> r = o . open ( req ) <EOL> self . assertEqual ( r , handlers [ <NUM_LIT:2> ] ) <EOL> calls = [ ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" ) , ( handlers [ <NUM_LIT:2> ] , "<STR_LIT>" ) ] <EOL> for expected , got in zip ( calls , o . calls ) : <EOL> handler , name , args , kwds = got <EOL> self . assertEqual ( ( handler , name ) , expected ) <EOL> self . assertEqual ( args , ( req , ) ) <EOL> def test_handler_order ( self ) : <EOL> o = OpenerDirector ( ) <EOL> handlers = [ ] <EOL> for meths , handler_order in [ <EOL> ( [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <NUM_LIT> ) , <EOL> ( [ "<STR_LIT>" ] , <NUM_LIT:0> ) , <EOL> ] : <EOL> class MockHandlerSubclass ( MockHandler ) : pass <EOL> h = MockHandlerSubclass ( meths ) <EOL> h . handler_order = handler_order <EOL> handlers . append ( h ) <EOL> o . add_handler ( h ) <EOL> o . open ( "<STR_LIT>" ) <EOL> self . assertEqual ( o . calls [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , handlers [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( o . calls [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , handlers [ <NUM_LIT:0> ] ) <EOL> def test_raise ( self ) : <EOL> o = OpenerDirector ( ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertRaises ( urllib . error . URLError , o . open , req ) <EOL> self . assertEqual ( o . calls , [ ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" , ( req , ) , { } ) ] ) <EOL> def test_http_error ( self ) : <EOL> o = OpenerDirector ( ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" , <EOL> "<STR_LIT>" ] , <EOL> [ ( "<STR_LIT>" ) ] , <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> class Unknown : <EOL> def __eq__ ( self , other ) : return True <EOL> req = Request ( "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> assert len ( o . calls ) == <NUM_LIT:2> <EOL> calls = [ ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" , ( req , ) ) , <EOL> ( handlers [ <NUM_LIT:2> ] , "<STR_LIT>" , <EOL> ( req , Unknown ( ) , <NUM_LIT> , "<STR_LIT>" , { } ) ) ] <EOL> for expected , got in zip ( calls , o . calls ) : <EOL> handler , method_name , args = expected <EOL> self . assertEqual ( ( handler , method_name ) , got [ : <NUM_LIT:2> ] ) <EOL> self . assertEqual ( args , got [ <NUM_LIT:2> ] ) <EOL> def test_processors ( self ) : <EOL> o = OpenerDirector ( ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> calls = [ <EOL> ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" ) , ( handlers [ <NUM_LIT:1> ] , "<STR_LIT>" ) , <EOL> ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" ) , ( handlers [ <NUM_LIT:1> ] , "<STR_LIT>" ) ] <EOL> for i , ( handler , name , args , kwds ) in enumerate ( o . calls ) : <EOL> if i < <NUM_LIT:2> : <EOL> self . assertEqual ( ( handler , name ) , calls [ i ] ) <EOL> self . assertEqual ( len ( args ) , <NUM_LIT:1> ) <EOL> self . assertIsInstance ( args [ <NUM_LIT:0> ] , Request ) <EOL> else : <EOL> self . assertEqual ( ( handler , name ) , calls [ i ] ) <EOL> self . assertEqual ( len ( args ) , <NUM_LIT:2> ) <EOL> self . assertIsInstance ( args [ <NUM_LIT:0> ] , Request ) <EOL> self . assertTrue ( args [ <NUM_LIT:1> ] is None or <EOL> isinstance ( args [ <NUM_LIT:1> ] , MockResponse ) ) <EOL> def test_method_deprecations ( self ) : <EOL> req = Request ( "<STR_LIT>" ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . add_data ( "<STR_LIT:data>" ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . get_data ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . has_data ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . get_host ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . get_selector ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . is_unverifiable ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . get_origin_req_host ( ) <EOL> with self . assertWarns ( DeprecationWarning ) : <EOL> req . get_type ( ) <EOL> def sanepathname2url ( path ) : <EOL> try : <EOL> path . encode ( "<STR_LIT:utf-8>" ) <EOL> except UnicodeEncodeError : <EOL> raise unittest . SkipTest ( "<STR_LIT>" ) <EOL> urlpath = urllib . request . pathname2url ( path ) <EOL> if os . name == "<STR_LIT>" and urlpath . startswith ( "<STR_LIT>" ) : <EOL> urlpath = urlpath [ <NUM_LIT:2> : ] <EOL> return urlpath <EOL> class HandlerTests ( unittest . TestCase ) : <EOL> def test_ftp ( self ) : <EOL> class MockFTPWrapper : <EOL> def __init__ ( self , data ) : self . data = data <EOL> def retrfile ( self , filename , filetype ) : <EOL> self . filename , self . filetype = filename , filetype <EOL> return io . StringIO ( self . data ) , len ( self . data ) <EOL> def close ( self ) : pass <EOL> class NullFTPHandler ( urllib . request . FTPHandler ) : <EOL> def __init__ ( self , data ) : self . data = data <EOL> def connect_ftp ( self , user , passwd , host , port , dirs , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . user , self . passwd = user , passwd <EOL> self . host , self . port = host , port <EOL> self . dirs = dirs <EOL> self . ftpwrapper = MockFTPWrapper ( self . data ) <EOL> return self . ftpwrapper <EOL> import ftplib <EOL> data = "<STR_LIT>" <EOL> h = NullFTPHandler ( data ) <EOL> h . parent = MockOpener ( ) <EOL> for url , host , port , user , passwd , type_ , dirs , filename , mimetype in [ <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , ftplib . FTP_PORT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:I>" , <EOL> [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , ftplib . FTP_PORT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:I>" , <EOL> [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , ftplib . FTP_PORT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:I>" , <EOL> [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , ftplib . FTP_PORT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:I>" , <EOL> [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , <NUM_LIT> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:D>" , <EOL> [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] , "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" , ftplib . FTP_PORT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:A>" , <EOL> [ ] , "<STR_LIT>" , None ) , <EOL> ] : <EOL> req = Request ( url ) <EOL> req . timeout = None <EOL> r = h . ftp_open ( req ) <EOL> self . assertEqual ( h . user , user ) <EOL> self . assertEqual ( h . passwd , passwd ) <EOL> self . assertEqual ( h . host , socket . gethostbyname ( host ) ) <EOL> self . assertEqual ( h . port , port ) <EOL> self . assertEqual ( h . dirs , dirs ) <EOL> self . assertEqual ( h . ftpwrapper . filename , filename ) <EOL> self . assertEqual ( h . ftpwrapper . filetype , type_ ) <EOL> headers = r . info ( ) <EOL> self . assertEqual ( headers . get ( "<STR_LIT>" ) , mimetype ) <EOL> self . assertEqual ( int ( headers [ "<STR_LIT>" ] ) , len ( data ) ) <EOL> def test_file ( self ) : <EOL> import email . utils , socket <EOL> h = urllib . request . FileHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> TESTFN = support . TESTFN <EOL> urlpath = sanepathname2url ( os . path . abspath ( TESTFN ) ) <EOL> towrite = b"<STR_LIT>" <EOL> urls = [ <EOL> "<STR_LIT>" % urlpath , <EOL> "<STR_LIT>" % urlpath , <EOL> "<STR_LIT>" % ( socket . gethostbyname ( '<STR_LIT:localhost>' ) , urlpath ) , <EOL> ] <EOL> try : <EOL> localaddr = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> except socket . gaierror : <EOL> localaddr = '<STR_LIT>' <EOL> if localaddr : <EOL> urls . append ( "<STR_LIT>" % ( localaddr , urlpath ) ) <EOL> for url in urls : <EOL> f = open ( TESTFN , "<STR_LIT:wb>" ) <EOL> try : <EOL> try : <EOL> f . write ( towrite ) <EOL> finally : <EOL> f . close ( ) <EOL> r = h . file_open ( Request ( url ) ) <EOL> try : <EOL> data = r . read ( ) <EOL> headers = r . info ( ) <EOL> respurl = r . geturl ( ) <EOL> finally : <EOL> r . close ( ) <EOL> stats = os . stat ( TESTFN ) <EOL> modified = email . utils . formatdate ( stats . st_mtime , usegmt = True ) <EOL> finally : <EOL> os . remove ( TESTFN ) <EOL> self . assertEqual ( data , towrite ) <EOL> self . assertEqual ( headers [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( headers [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( headers [ "<STR_LIT>" ] , modified ) <EOL> self . assertEqual ( respurl , url ) <EOL> for url in [ <EOL> "<STR_LIT>" % urlpath , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( socket . gethostbyname ( '<STR_LIT:localhost>' ) , <EOL> os . getcwd ( ) , TESTFN ) , <EOL> "<STR_LIT>" % <EOL> ( os . getcwd ( ) , TESTFN ) , <EOL> ] : <EOL> try : <EOL> f = open ( TESTFN , "<STR_LIT:wb>" ) <EOL> try : <EOL> f . write ( towrite ) <EOL> finally : <EOL> f . close ( ) <EOL> self . assertRaises ( urllib . error . URLError , <EOL> h . file_open , Request ( url ) ) <EOL> finally : <EOL> os . remove ( TESTFN ) <EOL> h = urllib . request . FileHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> for url , ftp in [ <EOL> ( "<STR_LIT>" , False ) , <EOL> ( "<STR_LIT>" , False ) , <EOL> ( "<STR_LIT>" , False ) , <EOL> ( "<STR_LIT>" , False ) , <EOL> ( "<STR_LIT>" , False ) , <EOL> ] : <EOL> req = Request ( url ) <EOL> try : <EOL> h . file_open ( req ) <EOL> except ( urllib . error . URLError , OSError ) : <EOL> self . assertFalse ( ftp ) <EOL> else : <EOL> self . assertIs ( o . req , req ) <EOL> self . assertEqual ( req . type , "<STR_LIT>" ) <EOL> self . assertEqual ( req . type == "<STR_LIT>" , ftp ) <EOL> def test_http ( self ) : <EOL> h = urllib . request . AbstractHTTPHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> url = "<STR_LIT>" <EOL> for method , data in [ ( "<STR_LIT:GET>" , None ) , ( "<STR_LIT:POST>" , b"<STR_LIT>" ) ] : <EOL> req = Request ( url , data , { "<STR_LIT>" : "<STR_LIT:bar>" } ) <EOL> req . timeout = None <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> http = MockHTTPClass ( ) <EOL> r = h . do_open ( http , req ) <EOL> r . read ; r . readline <EOL> r . info ; r . geturl <EOL> r . code , r . msg == <NUM_LIT:200> , "<STR_LIT:OK>" <EOL> hdrs = r . info ( ) <EOL> hdrs . get ; hdrs . __contains__ <EOL> self . assertEqual ( r . geturl ( ) , url ) <EOL> self . assertEqual ( http . host , "<STR_LIT>" ) <EOL> self . assertEqual ( http . level , <NUM_LIT:0> ) <EOL> self . assertEqual ( http . method , method ) <EOL> self . assertEqual ( http . selector , "<STR_LIT:/>" ) <EOL> self . assertEqual ( http . req_headers , <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:bar>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> self . assertEqual ( http . data , data ) <EOL> http . raise_on_endheaders = True <EOL> self . assertRaises ( urllib . error . URLError , h . do_open , http , req ) <EOL> req = Request ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , h . do_request_ , req ) <EOL> o . addheaders = [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> for data in b"<STR_LIT>" , None : <EOL> req = Request ( "<STR_LIT>" , data ) <EOL> r = MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , { } , "<STR_LIT>" ) <EOL> newreq = h . do_request_ ( req ) <EOL> if data is None : <EOL> self . assertNotIn ( "<STR_LIT>" , req . unredirected_hdrs ) <EOL> self . assertNotIn ( "<STR_LIT>" , req . unredirected_hdrs ) <EOL> else : <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT:0>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT:foo>" ) <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT:bar>" ) <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT:foo>" ) <EOL> newreq = h . do_request_ ( req ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT:bar>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> self . assertEqual ( req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT:foo>" ) <EOL> def iterable_body ( ) : <EOL> yield b"<STR_LIT>" <EOL> yield b"<STR_LIT>" <EOL> yield b"<STR_LIT>" <EOL> for headers in { } , { "<STR_LIT>" : <NUM_LIT:11> } : <EOL> req = Request ( "<STR_LIT>" , iterable_body ( ) , headers ) <EOL> if not headers : <EOL> self . assertRaises ( ValueError , h . do_request_ , req ) <EOL> else : <EOL> newreq = h . do_request_ ( req ) <EOL> file_obj = io . BytesIO ( ) <EOL> file_obj . write ( b"<STR_LIT>" ) <EOL> for headers in { } , { "<STR_LIT>" : <NUM_LIT:30> } : <EOL> req = Request ( "<STR_LIT>" , file_obj , headers ) <EOL> if not headers : <EOL> self . assertRaises ( ValueError , h . do_request_ , req ) <EOL> else : <EOL> newreq = h . do_request_ ( req ) <EOL> self . assertEqual ( int ( newreq . get_header ( '<STR_LIT>' ) ) , <NUM_LIT:30> ) <EOL> file_obj . close ( ) <EOL> iterable_array = array . array ( "<STR_LIT:I>" , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> for headers in { } , { "<STR_LIT>" : <NUM_LIT:16> } : <EOL> req = Request ( "<STR_LIT>" , iterable_array , headers ) <EOL> newreq = h . do_request_ ( req ) <EOL> self . assertEqual ( int ( newreq . get_header ( '<STR_LIT>' ) ) , <NUM_LIT:16> ) <EOL> def test_http_doubleslash ( self ) : <EOL> h = urllib . request . AbstractHTTPHandler ( ) <EOL> h . parent = MockOpener ( ) <EOL> data = b"<STR_LIT>" <EOL> ds_urls = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> for ds_url in ds_urls : <EOL> ds_req = Request ( ds_url , data ) <EOL> np_ds_req = h . do_request_ ( ds_req ) <EOL> self . assertEqual ( np_ds_req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> ds_req . set_proxy ( "<STR_LIT>" , None ) <EOL> p_ds_req = h . do_request_ ( ds_req ) <EOL> self . assertEqual ( p_ds_req . unredirected_hdrs [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def test_fixpath_in_weirdurls ( self ) : <EOL> h = urllib . request . AbstractHTTPHandler ( ) <EOL> h . parent = MockOpener ( ) <EOL> weird_url = '<STR_LIT>' <EOL> req = Request ( weird_url ) <EOL> newreq = h . do_request_ ( req ) <EOL> self . assertEqual ( newreq . host , '<STR_LIT>' ) <EOL> self . assertEqual ( newreq . selector , '<STR_LIT>' ) <EOL> url_without_path = '<STR_LIT>' <EOL> req = Request ( url_without_path ) <EOL> newreq = h . do_request_ ( req ) <EOL> self . assertEqual ( newreq . host , '<STR_LIT>' ) <EOL> self . assertEqual ( newreq . selector , '<STR_LIT>' ) <EOL> def test_errors ( self ) : <EOL> h = urllib . request . HTTPErrorProcessor ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> url = "<STR_LIT>" <EOL> req = Request ( url ) <EOL> r = MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , { } , "<STR_LIT>" , url ) <EOL> newr = h . http_response ( req , r ) <EOL> self . assertIs ( r , newr ) <EOL> self . assertFalse ( hasattr ( o , "<STR_LIT>" ) ) <EOL> r = MockResponse ( <NUM_LIT> , "<STR_LIT>" , { } , "<STR_LIT>" , url ) <EOL> newr = h . http_response ( req , r ) <EOL> self . assertIs ( r , newr ) <EOL> self . assertFalse ( hasattr ( o , "<STR_LIT>" ) ) <EOL> r = MockResponse ( <NUM_LIT> , "<STR_LIT>" , { } , "<STR_LIT>" , url ) <EOL> newr = h . http_response ( req , r ) <EOL> self . assertIs ( r , newr ) <EOL> self . assertFalse ( hasattr ( o , "<STR_LIT>" ) ) <EOL> r = MockResponse ( <NUM_LIT> , "<STR_LIT>" , { } , "<STR_LIT>" , url ) <EOL> self . assertIsNone ( h . http_response ( req , r ) ) <EOL> self . assertEqual ( o . proto , "<STR_LIT:http>" ) <EOL> self . assertEqual ( o . args , ( req , r , <NUM_LIT> , "<STR_LIT>" , { } ) ) <EOL> def test_cookies ( self ) : <EOL> cj = MockCookieJar ( ) <EOL> h = urllib . request . HTTPCookieProcessor ( cj ) <EOL> h . parent = MockOpener ( ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> r = MockResponse ( <NUM_LIT:200> , "<STR_LIT:OK>" , { } , "<STR_LIT>" ) <EOL> newreq = h . http_request ( req ) <EOL> self . assertIs ( cj . ach_req , req ) <EOL> self . assertIs ( cj . ach_req , newreq ) <EOL> self . assertEqual ( req . origin_req_host , "<STR_LIT>" ) <EOL> self . assertFalse ( req . unverifiable ) <EOL> newr = h . http_response ( req , r ) <EOL> self . assertIs ( cj . ec_req , req ) <EOL> self . assertIs ( cj . ec_r , r ) <EOL> self . assertIs ( r , newr ) <EOL> def test_redirect ( self ) : <EOL> from_url = "<STR_LIT>" <EOL> to_url = "<STR_LIT>" <EOL> h = urllib . request . HTTPRedirectHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> for code in <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> : <EOL> for data in None , "<STR_LIT>" : <EOL> method = getattr ( h , "<STR_LIT>" % code ) <EOL> req = Request ( from_url , data ) <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if data is not None : <EOL> req . add_header ( "<STR_LIT>" , str ( len ( data ) ) ) <EOL> req . add_unredirected_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> method ( req , MockFile ( ) , code , "<STR_LIT>" , <EOL> MockHeaders ( { "<STR_LIT:location>" : to_url } ) ) <EOL> except urllib . error . HTTPError : <EOL> self . assertTrue ( code == <NUM_LIT> and data is not None ) <EOL> self . assertEqual ( o . req . get_full_url ( ) , to_url ) <EOL> try : <EOL> self . assertEqual ( o . req . get_method ( ) , "<STR_LIT:GET>" ) <EOL> except AttributeError : <EOL> self . assertFalse ( o . req . data ) <EOL> headers = [ x . lower ( ) for x in o . req . headers ] <EOL> self . assertNotIn ( "<STR_LIT>" , headers ) <EOL> self . assertNotIn ( "<STR_LIT>" , headers ) <EOL> self . assertEqual ( o . req . headers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> self . assertNotIn ( "<STR_LIT>" , o . req . headers ) <EOL> self . assertNotIn ( "<STR_LIT>" , o . req . unredirected_hdrs ) <EOL> req = Request ( from_url ) <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> def redirect ( h , req , url = to_url ) : <EOL> h . http_error_302 ( req , MockFile ( ) , <NUM_LIT> , "<STR_LIT>" , <EOL> MockHeaders ( { "<STR_LIT:location>" : url } ) ) <EOL> req = Request ( from_url , origin_req_host = "<STR_LIT>" ) <EOL> count = <NUM_LIT:0> <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> redirect ( h , req , "<STR_LIT>" ) <EOL> count = count + <NUM_LIT:1> <EOL> except urllib . error . HTTPError : <EOL> self . assertEqual ( count , urllib . request . HTTPRedirectHandler . max_repeats ) <EOL> req = Request ( from_url , origin_req_host = "<STR_LIT>" ) <EOL> count = <NUM_LIT:0> <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> redirect ( h , req , "<STR_LIT>" % count ) <EOL> count = count + <NUM_LIT:1> <EOL> except urllib . error . HTTPError : <EOL> self . assertEqual ( count , <EOL> urllib . request . HTTPRedirectHandler . max_redirections ) <EOL> def test_invalid_redirect ( self ) : <EOL> from_url = "<STR_LIT>" <EOL> valid_schemes = [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> invalid_schemes = [ '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> schemeless_url = "<STR_LIT>" <EOL> h = urllib . request . HTTPRedirectHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> req = Request ( from_url ) <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> for scheme in invalid_schemes : <EOL> invalid_url = scheme + '<STR_LIT>' + schemeless_url <EOL> self . assertRaises ( urllib . error . HTTPError , h . http_error_302 , <EOL> req , MockFile ( ) , <NUM_LIT> , "<STR_LIT>" , <EOL> MockHeaders ( { "<STR_LIT:location>" : invalid_url } ) ) <EOL> for scheme in valid_schemes : <EOL> valid_url = scheme + '<STR_LIT>' + schemeless_url <EOL> h . http_error_302 ( req , MockFile ( ) , <NUM_LIT> , "<STR_LIT>" , <EOL> MockHeaders ( { "<STR_LIT:location>" : valid_url } ) ) <EOL> self . assertEqual ( o . req . get_full_url ( ) , valid_url ) <EOL> def test_relative_redirect ( self ) : <EOL> from_url = "<STR_LIT>" <EOL> relative_url = "<STR_LIT>" <EOL> h = urllib . request . HTTPRedirectHandler ( ) <EOL> o = h . parent = MockOpener ( ) <EOL> req = Request ( from_url ) <EOL> req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT <EOL> valid_url = urllib . parse . urljoin ( from_url , relative_url ) <EOL> h . http_error_302 ( req , MockFile ( ) , <NUM_LIT> , "<STR_LIT>" , <EOL> MockHeaders ( { "<STR_LIT:location>" : valid_url } ) ) <EOL> self . assertEqual ( o . req . get_full_url ( ) , valid_url ) <EOL> def test_cookie_redirect ( self ) : <EOL> from http . cookiejar import CookieJar <EOL> from test . test_http_cookiejar import interact_netscape <EOL> cj = CookieJar ( ) <EOL> interact_netscape ( cj , "<STR_LIT>" , "<STR_LIT>" ) <EOL> hh = MockHTTPHandler ( <NUM_LIT> , "<STR_LIT>" ) <EOL> hdeh = urllib . request . HTTPDefaultErrorHandler ( ) <EOL> hrh = urllib . request . HTTPRedirectHandler ( ) <EOL> cp = urllib . request . HTTPCookieProcessor ( cj ) <EOL> o = build_test_opener ( hh , hdeh , hrh , cp ) <EOL> o . open ( "<STR_LIT>" ) <EOL> self . assertFalse ( hh . req . has_header ( "<STR_LIT>" ) ) <EOL> def test_redirect_fragment ( self ) : <EOL> redirected_url = '<STR_LIT>' <EOL> hh = MockHTTPHandler ( <NUM_LIT> , '<STR_LIT>' + redirected_url ) <EOL> hdeh = urllib . request . HTTPDefaultErrorHandler ( ) <EOL> hrh = urllib . request . HTTPRedirectHandler ( ) <EOL> o = build_test_opener ( hh , hdeh , hrh ) <EOL> fp = o . open ( '<STR_LIT>' ) <EOL> self . assertEqual ( fp . geturl ( ) , redirected_url . strip ( ) ) <EOL> def test_proxy ( self ) : <EOL> o = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( http = "<STR_LIT>" ) ) <EOL> o . add_handler ( ph ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> self . assertEqual ( [ ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" ) ] , <EOL> [ tup [ <NUM_LIT:0> : <NUM_LIT:2> ] for tup in o . calls ] ) <EOL> def test_proxy_no_proxy ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> o = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( http = "<STR_LIT>" ) ) <EOL> o . add_handler ( ph ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> del os . environ [ '<STR_LIT>' ] <EOL> def test_proxy_no_proxy_all ( self ) : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:*>' <EOL> o = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( http = "<STR_LIT>" ) ) <EOL> o . add_handler ( ph ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> del os . environ [ '<STR_LIT>' ] <EOL> def test_proxy_https ( self ) : <EOL> o = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( https = "<STR_LIT>" ) ) <EOL> o . add_handler ( ph ) <EOL> meth_spec = [ <EOL> [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> ] <EOL> handlers = add_ordered_mock_handlers ( o , meth_spec ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> o . open ( req ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> self . assertEqual ( [ ( handlers [ <NUM_LIT:0> ] , "<STR_LIT>" ) ] , <EOL> [ tup [ <NUM_LIT:0> : <NUM_LIT:2> ] for tup in o . calls ] ) <EOL> def test_proxy_https_proxy_authorization ( self ) : <EOL> o = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( https = '<STR_LIT>' ) ) <EOL> o . add_handler ( ph ) <EOL> https_handler = MockHTTPSHandler ( ) <EOL> o . add_handler ( https_handler ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> self . assertIsNone ( req . _tunnel_host ) <EOL> o . open ( req ) <EOL> self . assertNotIn ( ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> https_handler . httpconn . req_headers ) <EOL> self . assertIn ( ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> https_handler . httpconn . req_headers ) <EOL> self . assertIsNotNone ( req . _tunnel_host ) <EOL> self . assertEqual ( req . host , "<STR_LIT>" ) <EOL> self . assertEqual ( req . get_header ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> @ unittest . skipUnless ( sys . platform == '<STR_LIT>' , "<STR_LIT>" ) <EOL> def test_osx_proxy_bypass ( self ) : <EOL> bypass = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:127.0.0.1>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> } <EOL> for host in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:127.0.0.1>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> self . assertTrue ( _proxy_bypass_macosx_sysconf ( host , bypass ) , <EOL> '<STR_LIT>' % host ) <EOL> for host in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ) : <EOL> self . assertFalse ( _proxy_bypass_macosx_sysconf ( host , bypass ) , <EOL> '<STR_LIT>' % host ) <EOL> bypass = { '<STR_LIT>' : True , '<STR_LIT>' : [ ] } <EOL> self . assertTrue ( _proxy_bypass_macosx_sysconf ( '<STR_LIT:test>' , bypass ) ) <EOL> def test_basic_auth ( self , quote_char = '<STR_LIT:">' ) : <EOL> opener = OpenerDirector ( ) <EOL> password_manager = MockPasswordManager ( ) <EOL> auth_handler = urllib . request . HTTPBasicAuthHandler ( password_manager ) <EOL> realm = "<STR_LIT>" <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' % <EOL> ( quote_char , realm , quote_char ) ) <EOL> opener . add_handler ( auth_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> self . _test_basic_auth ( opener , auth_handler , "<STR_LIT>" , <EOL> realm , http_handler , password_manager , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_basic_auth_with_single_quoted_realm ( self ) : <EOL> self . test_basic_auth ( quote_char = "<STR_LIT:'>" ) <EOL> def test_basic_auth_with_unquoted_realm ( self ) : <EOL> opener = OpenerDirector ( ) <EOL> password_manager = MockPasswordManager ( ) <EOL> auth_handler = urllib . request . HTTPBasicAuthHandler ( password_manager ) <EOL> realm = "<STR_LIT>" <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' % realm ) <EOL> opener . add_handler ( auth_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> with self . assertWarns ( UserWarning ) : <EOL> self . _test_basic_auth ( opener , auth_handler , "<STR_LIT>" , <EOL> realm , http_handler , password_manager , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_proxy_basic_auth ( self ) : <EOL> opener = OpenerDirector ( ) <EOL> ph = urllib . request . ProxyHandler ( dict ( http = "<STR_LIT>" ) ) <EOL> opener . add_handler ( ph ) <EOL> password_manager = MockPasswordManager ( ) <EOL> auth_handler = urllib . request . ProxyBasicAuthHandler ( password_manager ) <EOL> realm = "<STR_LIT>" <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' % realm ) <EOL> opener . add_handler ( auth_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> self . _test_basic_auth ( opener , auth_handler , "<STR_LIT>" , <EOL> realm , http_handler , password_manager , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_basic_and_digest_auth_handlers ( self ) : <EOL> class RecordingOpenerDirector ( OpenerDirector ) : <EOL> def __init__ ( self ) : <EOL> OpenerDirector . __init__ ( self ) <EOL> self . recorded = [ ] <EOL> def record ( self , info ) : <EOL> self . recorded . append ( info ) <EOL> class TestDigestAuthHandler ( urllib . request . HTTPDigestAuthHandler ) : <EOL> def http_error_401 ( self , * args , ** kwds ) : <EOL> self . parent . record ( "<STR_LIT>" ) <EOL> urllib . request . HTTPDigestAuthHandler . http_error_401 ( self , <EOL> * args , ** kwds ) <EOL> class TestBasicAuthHandler ( urllib . request . HTTPBasicAuthHandler ) : <EOL> def http_error_401 ( self , * args , ** kwds ) : <EOL> self . parent . record ( "<STR_LIT>" ) <EOL> urllib . request . HTTPBasicAuthHandler . http_error_401 ( self , <EOL> * args , ** kwds ) <EOL> opener = RecordingOpenerDirector ( ) <EOL> password_manager = MockPasswordManager ( ) <EOL> digest_handler = TestDigestAuthHandler ( password_manager ) <EOL> basic_handler = TestBasicAuthHandler ( password_manager ) <EOL> realm = "<STR_LIT>" <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' % realm ) <EOL> opener . add_handler ( basic_handler ) <EOL> opener . add_handler ( digest_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> self . _test_basic_auth ( opener , basic_handler , "<STR_LIT>" , <EOL> realm , http_handler , password_manager , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> self . assertEqual ( opener . recorded , [ "<STR_LIT>" , "<STR_LIT>" ] * <NUM_LIT:2> ) <EOL> def test_unsupported_auth_digest_handler ( self ) : <EOL> opener = OpenerDirector ( ) <EOL> digest_auth_handler = urllib . request . HTTPDigestAuthHandler ( None ) <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> opener . add_handler ( digest_auth_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> self . assertRaises ( ValueError , opener . open , "<STR_LIT>" ) <EOL> def test_unsupported_auth_basic_handler ( self ) : <EOL> opener = OpenerDirector ( ) <EOL> basic_auth_handler = urllib . request . HTTPBasicAuthHandler ( None ) <EOL> http_handler = MockHTTPHandler ( <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> opener . add_handler ( basic_auth_handler ) <EOL> opener . add_handler ( http_handler ) <EOL> self . assertRaises ( ValueError , opener . open , "<STR_LIT>" ) <EOL> def _test_basic_auth ( self , opener , auth_handler , auth_header , <EOL> realm , http_handler , password_manager , <EOL> request_url , protected_url ) : <EOL> import base64 <EOL> user , password = "<STR_LIT>" , "<STR_LIT>" <EOL> auth_handler . add_password ( realm , request_url , user , password ) <EOL> self . assertEqual ( realm , password_manager . realm ) <EOL> self . assertEqual ( request_url , password_manager . url ) <EOL> self . assertEqual ( user , password_manager . user ) <EOL> self . assertEqual ( password , password_manager . password ) <EOL> opener . open ( request_url ) <EOL> self . assertEqual ( password_manager . target_realm , realm ) <EOL> self . assertEqual ( password_manager . target_url , protected_url ) <EOL> self . assertEqual ( len ( http_handler . requests ) , <NUM_LIT:2> ) <EOL> self . assertFalse ( http_handler . requests [ <NUM_LIT:0> ] . has_header ( auth_header ) ) <EOL> userpass = bytes ( '<STR_LIT>' % ( user , password ) , "<STR_LIT:ascii>" ) <EOL> auth_hdr_value = ( '<STR_LIT>' + <EOL> base64 . encodebytes ( userpass ) . strip ( ) . decode ( ) ) <EOL> self . assertEqual ( http_handler . requests [ <NUM_LIT:1> ] . get_header ( auth_header ) , <EOL> auth_hdr_value ) <EOL> self . assertEqual ( http_handler . requests [ <NUM_LIT:1> ] . unredirected_hdrs [ auth_header ] , <EOL> auth_hdr_value ) <EOL> password_manager . user = password_manager . password = None <EOL> http_handler . reset ( ) <EOL> opener . open ( request_url ) <EOL> self . assertEqual ( len ( http_handler . requests ) , <NUM_LIT:1> ) <EOL> self . assertFalse ( http_handler . requests [ <NUM_LIT:0> ] . has_header ( auth_header ) ) <EOL> class MiscTests ( unittest . TestCase ) : <EOL> def opener_has_handler ( self , opener , handler_class ) : <EOL> self . assertTrue ( any ( h . __class__ == handler_class <EOL> for h in opener . handlers ) ) <EOL> def test_build_opener ( self ) : <EOL> class MyHTTPHandler ( urllib . request . HTTPHandler ) : pass <EOL> class FooHandler ( urllib . request . BaseHandler ) : <EOL> def foo_open ( self ) : pass <EOL> class BarHandler ( urllib . request . BaseHandler ) : <EOL> def bar_open ( self ) : pass <EOL> build_opener = urllib . request . build_opener <EOL> o = build_opener ( FooHandler , BarHandler ) <EOL> self . opener_has_handler ( o , FooHandler ) <EOL> self . opener_has_handler ( o , BarHandler ) <EOL> o = build_opener ( FooHandler , BarHandler ( ) ) <EOL> self . opener_has_handler ( o , FooHandler ) <EOL> self . opener_has_handler ( o , BarHandler ) <EOL> o = build_opener ( MyHTTPHandler ) <EOL> self . opener_has_handler ( o , MyHTTPHandler ) <EOL> o = build_opener ( ) <EOL> self . opener_has_handler ( o , urllib . request . HTTPHandler ) <EOL> o = build_opener ( urllib . request . HTTPHandler ) <EOL> self . opener_has_handler ( o , urllib . request . HTTPHandler ) <EOL> o = build_opener ( urllib . request . HTTPHandler ( ) ) <EOL> self . opener_has_handler ( o , urllib . request . HTTPHandler ) <EOL> class MyOtherHTTPHandler ( urllib . request . HTTPHandler ) : pass <EOL> o = build_opener ( MyHTTPHandler , MyOtherHTTPHandler ) <EOL> self . opener_has_handler ( o , MyHTTPHandler ) <EOL> self . opener_has_handler ( o , MyOtherHTTPHandler ) <EOL> def test_HTTPError_interface ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> url = code = fp = None <EOL> hdrs = '<STR_LIT>' <EOL> err = urllib . error . HTTPError ( url , code , msg , hdrs , fp ) <EOL> self . assertTrue ( hasattr ( err , '<STR_LIT>' ) ) <EOL> self . assertEqual ( err . reason , '<STR_LIT>' ) <EOL> self . assertTrue ( hasattr ( err , '<STR_LIT>' ) ) <EOL> self . assertEqual ( err . hdrs , '<STR_LIT>' ) <EOL> expected_errmsg = '<STR_LIT>' % ( err . code , err . msg ) <EOL> self . assertEqual ( str ( err ) , expected_errmsg ) <EOL> class RequestTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . get = Request ( "<STR_LIT>" ) <EOL> self . post = Request ( "<STR_LIT>" , <EOL> "<STR_LIT:data>" , <EOL> headers = { "<STR_LIT>" : "<STR_LIT:test>" } ) <EOL> def test_method ( self ) : <EOL> self . assertEqual ( "<STR_LIT:POST>" , self . post . get_method ( ) ) <EOL> self . assertEqual ( "<STR_LIT:GET>" , self . get . get_method ( ) ) <EOL> def test_data ( self ) : <EOL> self . assertFalse ( self . get . data ) <EOL> self . assertEqual ( "<STR_LIT:GET>" , self . get . get_method ( ) ) <EOL> self . get . data = "<STR_LIT>" <EOL> self . assertTrue ( self . get . data ) <EOL> self . assertEqual ( "<STR_LIT:POST>" , self . get . get_method ( ) ) <EOL> def test_get_full_url ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> self . get . get_full_url ( ) ) <EOL> def test_selector ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , self . get . selector ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT:/>" , req . selector ) <EOL> def test_get_type ( self ) : <EOL> self . assertEqual ( "<STR_LIT:http>" , self . get . type ) <EOL> def test_get_host ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , self . get . host ) <EOL> def test_get_host_unquote ( self ) : <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , req . host ) <EOL> def test_proxy ( self ) : <EOL> self . assertFalse ( self . get . has_proxy ( ) ) <EOL> self . get . set_proxy ( "<STR_LIT>" , "<STR_LIT:http>" ) <EOL> self . assertTrue ( self . get . has_proxy ( ) ) <EOL> self . assertEqual ( "<STR_LIT>" , self . get . origin_req_host ) <EOL> self . assertEqual ( "<STR_LIT>" , self . get . host ) <EOL> def test_wrapped_url ( self ) : <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , req . host ) <EOL> def test_url_fragment ( self ) : <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , req . selector ) <EOL> req = Request ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT:/>" , req . selector ) <EOL> url = '<STR_LIT>' <EOL> req = Request ( url ) <EOL> self . assertEqual ( req . get_full_url ( ) , url ) <EOL> def test_HTTPError_interface_call ( self ) : <EOL> """<STR_LIT>""" <EOL> err = urllib . request . HTTPError ( msg = "<STR_LIT>" , url = None , <EOL> code = None , hdrs = '<STR_LIT>' , fp = None ) <EOL> self . assertTrue ( hasattr ( err , '<STR_LIT>' ) ) <EOL> assert hasattr ( err , '<STR_LIT>' ) <EOL> assert hasattr ( err , '<STR_LIT:info>' ) <EOL> assert callable ( err . info ) <EOL> try : <EOL> err . info ( ) <EOL> except AttributeError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . assertEqual ( err . info ( ) , "<STR_LIT>" ) <EOL> def test_main ( verbose = None ) : <EOL> from test import test_urllib2 <EOL> support . run_doctest ( test_urllib2 , verbose ) <EOL> support . run_doctest ( urllib . request , verbose ) <EOL> tests = ( TrivialTests , <EOL> OpenerDirectorTests , <EOL> HandlerTests , <EOL> MiscTests , <EOL> RequestTests , <EOL> RequestHdrsTests ) <EOL> support . run_unittest ( * tests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( verbose = True ) </s>
