#NEWLINE# def test_file( self ) : #NEWLINE# #INDENT# import rfc822 , socket #NEWLINE# h = urllib2 . FileHandler ( ) #NEWLINE# o = h . parent = MockOpener ( ) #NEWLINE# TESTFN = test_support . TESTFN #NEWLINE# urlpath = sanepathname2url ( os . path . abspath ( TESTFN ) ) #NEWLINE# towrite = 'hello, world\n' #NEWLINE# urls = [ ( 'file://localhost%s' % urlpath ) , ( 'file://%s' % urlpath ) , ( 'file://%s%s' % ( socket . gethostbyname ( 'localhost' ) , urlpath ) ) ] #NEWLINE# try : #NEWLINE# #INDENT# localaddr = socket . gethostbyname ( socket . gethostname ( ) ) #NEWLINE# #UNINDENT# except socket . gaierror : #NEWLINE# #INDENT# localaddr = '' #NEWLINE# #UNINDENT# if localaddr : #NEWLINE# #INDENT# urls . append ( ( 'file://%s%s' % ( localaddr , urlpath ) ) ) #NEWLINE# #UNINDENT# for url in urls : #NEWLINE# #INDENT# f = open ( TESTFN , 'wb' ) #NEWLINE# try : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# f . write ( towrite ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# f . close ( ) #NEWLINE# #UNINDENT# r = h . file_open ( Request ( url ) ) #NEWLINE# try : #NEWLINE# #INDENT# data = r . read ( ) #NEWLINE# headers = r . info ( ) #NEWLINE# respurl = r . geturl ( ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# r . close ( ) #NEWLINE# #UNINDENT# stats = os . stat ( TESTFN ) #NEWLINE# modified = rfc822 . formatdate ( stats . st_mtime ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# os . remove ( TESTFN ) #NEWLINE# #UNINDENT# self . assertEqual ( data , towrite ) #NEWLINE# self . assertEqual ( headers [ 'Content-type' ] , 'text/plain' ) #NEWLINE# self . assertEqual ( headers [ 'Content-length' ] , '13' ) #NEWLINE# self . assertEqual ( headers [ 'Last-modified' ] , modified ) #NEWLINE# self . assertEqual ( respurl , url ) #NEWLINE# #UNINDENT# for url in [ ( 'file://localhost:80%s' % urlpath ) , 'file:///file_does_not_exist.txt' , ( 'file://%s:80%s/%s' % ( socket . gethostbyname ( 'localhost' ) , os . getcwd ( ) , TESTFN ) ) , ( 'file://somerandomhost.ontheinternet.com%s/%s' % ( os . getcwd ( ) , TESTFN ) ) ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# f = open ( TESTFN , 'wb' ) #NEWLINE# try : #NEWLINE# #INDENT# f . write ( towrite ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# f . close ( ) #NEWLINE# #UNINDENT# self . assertRaises ( urllib2 . URLError , h . file_open , Request ( url ) ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# os . remove ( TESTFN ) #NEWLINE# #UNINDENT# #UNINDENT# h = urllib2 . FileHandler ( ) #NEWLINE# o = h . parent = MockOpener ( ) #NEWLINE# for ( url , ftp ) in [ ( 'file://ftp.example.com//foo.txt' , True ) , ( 'file://ftp.example.com///foo.txt' , False ) , ( 'file://ftp.example.com/foo.txt' , False ) , ( 'file://somehost//foo/something.txt' , True ) , ( 'file://localhost//foo/something.txt' , False ) ] : #NEWLINE# #INDENT# req = Request ( url ) #NEWLINE# try : #NEWLINE# #INDENT# h . file_open ( req ) #NEWLINE# #UNINDENT# except ( urllib2 . URLError , OSError ) : #NEWLINE# #INDENT# self . assertTrue ( ( not ftp ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . assertTrue ( ( o . req is req ) ) #NEWLINE# self . assertEqual ( req . type , 'ftp' ) #NEWLINE# #UNINDENT# self . assertEqual ( ( req . type == 'ftp' ) , ftp )