#NEWLINE# def test_ftp( self ) : #NEWLINE# #INDENT# class MockFTPWrapper ( ) : #NEWLINE# #INDENT# def __init__( self , data ) : #NEWLINE# #INDENT# self . data = data #NEWLINE# #UNINDENT# def retrfile( self , filename , filetype ) : #NEWLINE# #INDENT# ( self . filename , self . filetype ) = ( filename , filetype ) #NEWLINE# return ( StringIO . StringIO ( self . data ) , len ( self . data ) ) #NEWLINE# #UNINDENT# def close( self ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# class NullFTPHandler ( urllib2 . FTPHandler ) : #NEWLINE# #INDENT# def __init__( self , data ) : #NEWLINE# #INDENT# self . data = data #NEWLINE# #UNINDENT# def connect_ftp( self , user , passwd , host , port , dirs , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : #NEWLINE# #INDENT# ( self . user , self . passwd ) = ( user , passwd ) #NEWLINE# ( self . host , self . port ) = ( host , port ) #NEWLINE# self . dirs = dirs #NEWLINE# self . ftpwrapper = MockFTPWrapper ( self . data ) #NEWLINE# return self . ftpwrapper #NEWLINE# #UNINDENT# #UNINDENT# import ftplib #NEWLINE# data = 'rheum rhaponicum' #NEWLINE# h = NullFTPHandler ( data ) #NEWLINE# o = h . parent = MockOpener ( ) #NEWLINE# for ( url , host , port , user , passwd , type_ , dirs , filename , mimetype ) in [ ( 'ftp://localhost/foo/bar/baz.html' , 'localhost' , ftplib . FTP_PORT , '' , '' , 'I' , [ 'foo' , 'bar' ] , 'baz.html' , 'text/html' ) , ( 'ftp://parrot@localhost/foo/bar/baz.html' , 'localhost' , ftplib . FTP_PORT , 'parrot' , '' , 'I' , [ 'foo' , 'bar' ] , 'baz.html' , 'text/html' ) , ( 'ftp://%25parrot@localhost/foo/bar/baz.html' , 'localhost' , ftplib . FTP_PORT , '%parrot' , '' , 'I' , [ 'foo' , 'bar' ] , 'baz.html' , 'text/html' ) , ( 'ftp://%2542parrot@localhost/foo/bar/baz.html' , 'localhost' , ftplib . FTP_PORT , '%42parrot' , '' , 'I' , [ 'foo' , 'bar' ] , 'baz.html' , 'text/html' ) , ( 'ftp://localhost:80/foo/bar/' , 'localhost' , 80 , '' , '' , 'D' , [ 'foo' , 'bar' ] , '' , None ) , ( 'ftp://localhost/baz.gif;type=a' , 'localhost' , ftplib . FTP_PORT , '' , '' , 'A' , [ ] , 'baz.gif' , None ) ] : #NEWLINE# #INDENT# req = Request ( url ) #NEWLINE# req . timeout = None #NEWLINE# r = h . ftp_open ( req ) #NEWLINE# self . assertEqual ( h . user , user ) #NEWLINE# self . assertEqual ( h . passwd , passwd ) #NEWLINE# self . assertEqual ( h . host , socket . gethostbyname ( host ) ) #NEWLINE# self . assertEqual ( h . port , port ) #NEWLINE# self . assertEqual ( h . dirs , dirs ) #NEWLINE# self . assertEqual ( h . ftpwrapper . filename , filename ) #NEWLINE# self . assertEqual ( h . ftpwrapper . filetype , type_ ) #NEWLINE# headers = r . info ( ) #NEWLINE# self . assertEqual ( headers . get ( 'Content-type' ) , mimetype ) #NEWLINE# self . assertEqual ( int ( headers [ 'Content-length' ] ) , len ( data ) )