#NEWLINE# def test_redirect( self ) : #NEWLINE# #INDENT# from_url = 'http://example.com/a.html' #NEWLINE# to_url = 'http://example.com/b.html' #NEWLINE# h = urllib2 . HTTPRedirectHandler ( ) #NEWLINE# o = h . parent = MockOpener ( ) #NEWLINE# for code in ( 301 , 302 , 303 , 307 ) : #NEWLINE# #INDENT# for data in ( None , 'blah\nblah\n' ) : #NEWLINE# #INDENT# method = getattr ( h , ( 'http_error_%s' % code ) ) #NEWLINE# req = Request ( from_url , data ) #NEWLINE# req . add_header ( 'Nonsense' , 'viking=withhold' ) #NEWLINE# req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT #NEWLINE# if ( data is not None ) : #NEWLINE# #INDENT# req . add_header ( 'Content-Length' , str ( len ( data ) ) ) #NEWLINE# #UNINDENT# req . add_unredirected_header ( 'Spam' , 'spam' ) #NEWLINE# try : #NEWLINE# #INDENT# method ( req , MockFile ( ) , code , 'Blah' , MockHeaders ( { #NEWLINE# #INDENT# 'location' : to_url , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# except urllib2 . HTTPError : #NEWLINE# #INDENT# self . assertEqual ( code , 307 ) #NEWLINE# self . assertIsNotNone ( data ) #NEWLINE# #UNINDENT# self . assertEqual ( o . req . get_full_url ( ) , to_url ) #NEWLINE# try : #NEWLINE# #INDENT# self . assertEqual ( o . req . get_method ( ) , 'GET' ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# self . assertTrue ( ( not o . req . has_data ( ) ) ) #NEWLINE# #UNINDENT# headers = [ x . lower ( ) for x in o . req . headers ] #NEWLINE# self . assertNotIn ( 'content-length' , headers ) #NEWLINE# self . assertNotIn ( 'content-type' , headers ) #NEWLINE# self . assertEqual ( o . req . headers [ 'Nonsense' ] , 'viking=withhold' ) #NEWLINE# self . assertNotIn ( 'Spam' , o . req . headers ) #NEWLINE# self . assertNotIn ( 'Spam' , o . req . unredirected_hdrs ) #NEWLINE# #UNINDENT# #UNINDENT# req = Request ( from_url ) #NEWLINE# req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT #NEWLINE# def redirect( h , req , url = to_url ) : #NEWLINE# #INDENT# h . http_error_302 ( req , MockFile ( ) , 302 , 'Blah' , MockHeaders ( { #NEWLINE# #INDENT# 'location' : url , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# req = Request ( from_url , origin_req_host = 'example.com' ) #NEWLINE# count = 0 #NEWLINE# req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT #NEWLINE# try : #NEWLINE# #INDENT# while 1 : #NEWLINE# #INDENT# redirect ( data , req , 'http://example.com/' ) #NEWLINE# count = ( count + 1 ) #NEWLINE# #UNINDENT# #UNINDENT# except urllib2 . HTTPError : #NEWLINE# #INDENT# self . assertEqual ( count , urllib2 . HTTPRedirectHandler . max_repeats ) #NEWLINE# #UNINDENT# req = Request ( from_url , origin_req_host = 'example.com' ) #NEWLINE# count = 0 #NEWLINE# req . timeout = socket . _GLOBAL_DEFAULT_TIMEOUT #NEWLINE# try : #NEWLINE# #INDENT# while 1 : #NEWLINE# #INDENT# redirect ( h , req , ( 'http://example.com/%d' % count ) ) #NEWLINE# count = ( count + 1 ) #NEWLINE# #UNINDENT# #UNINDENT# except urllib2 . HTTPError : #NEWLINE# #INDENT# self . assertEqual ( count , urllib2 . HTTPRedirectHandler . max_redirections )