<s> """<STR_LIT>""" <EOL> import os , stat <EOL> from twisted . python . usage import UsageError <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . internet . interfaces import IReactorUNIX <EOL> from twisted . internet import reactor <EOL> from twisted . python . threadpool import ThreadPool <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . application import strports <EOL> from twisted . web . server import Site <EOL> from twisted . web . static import Data , File <EOL> from twisted . web . distrib import ResourcePublisher , UserDirectory <EOL> from twisted . web . wsgi import WSGIResource <EOL> from twisted . web . tap import Options , makePersonalServerFactory , makeService <EOL> from twisted . web . twcgi import CGIScript , PHP3Script , PHPScript <EOL> from twisted . web . script import PythonScript <EOL> from twisted . spread . pb import PBServerFactory <EOL> application = object ( ) <EOL> class ServiceTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _pathOption ( self ) : <EOL> """<STR_LIT>""" <EOL> path = FilePath ( self . mktemp ( ) ) <EOL> path . makedirs ( ) <EOL> options = Options ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , path . path ] ) <EOL> root = options [ '<STR_LIT:root>' ] <EOL> return path , root <EOL> def test_path ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> self . assertIsInstance ( root , File ) <EOL> self . assertEqual ( root . path , path . path ) <EOL> def test_cgiProcessor ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> path . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . assertIsInstance ( root . getChild ( "<STR_LIT>" , None ) , CGIScript ) <EOL> def test_php3Processor ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> path . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . assertIsInstance ( root . getChild ( "<STR_LIT>" , None ) , PHP3Script ) <EOL> def test_phpProcessor ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> path . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . assertIsInstance ( root . getChild ( "<STR_LIT>" , None ) , PHPScript ) <EOL> def test_epyProcessor ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> path . child ( "<STR_LIT>" ) . setContent ( "<STR_LIT>" ) <EOL> self . assertIsInstance ( root . getChild ( "<STR_LIT>" , None ) , PythonScript ) <EOL> def test_rpyProcessor ( self ) : <EOL> """<STR_LIT>""" <EOL> path , root = self . _pathOption ( ) <EOL> path . child ( "<STR_LIT>" ) . setContent ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> child = root . getChild ( "<STR_LIT>" , None ) <EOL> self . assertIsInstance ( child , Data ) <EOL> self . assertEqual ( child . data , '<STR_LIT:content>' ) <EOL> self . assertEqual ( child . type , '<STR_LIT>' ) <EOL> def test_makePersonalServerFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> site = Site ( Data ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> serverFactory = makePersonalServerFactory ( site ) <EOL> self . assertIsInstance ( serverFactory , PBServerFactory ) <EOL> self . assertIsInstance ( serverFactory . root , ResourcePublisher ) <EOL> self . assertIdentical ( serverFactory . root . site , site ) <EOL> def test_personalServer ( self ) : <EOL> """<STR_LIT>""" <EOL> port = self . mktemp ( ) <EOL> options = Options ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , '<STR_LIT>' + port , '<STR_LIT>' ] ) <EOL> service = makeService ( options ) <EOL> service . startService ( ) <EOL> self . addCleanup ( service . stopService ) <EOL> self . assertTrue ( os . path . exists ( port ) ) <EOL> self . assertTrue ( stat . S_ISSOCK ( os . stat ( port ) . st_mode ) ) <EOL> if not IReactorUNIX . providedBy ( reactor ) : <EOL> test_personalServer . skip = ( <EOL> "<STR_LIT>" ) <EOL> def test_defaultPersonalPath ( self ) : <EOL> """<STR_LIT>""" <EOL> options = Options ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' ] ) <EOL> path = os . path . expanduser ( <EOL> os . path . join ( '<STR_LIT>' , UserDirectory . userSocketName ) ) <EOL> self . assertEqual ( <EOL> strports . parse ( options [ '<STR_LIT:port>' ] , None ) [ : <NUM_LIT:2> ] , <EOL> ( '<STR_LIT>' , ( path , None ) ) ) <EOL> if not IReactorUNIX . providedBy ( reactor ) : <EOL> test_defaultPersonalPath . skip = ( <EOL> "<STR_LIT>" ) <EOL> def test_defaultPort ( self ) : <EOL> """<STR_LIT>""" <EOL> options = Options ( ) <EOL> options . parseOptions ( [ ] ) <EOL> self . assertEqual ( <EOL> strports . parse ( options [ '<STR_LIT:port>' ] , None ) [ : <NUM_LIT:2> ] , <EOL> ( '<STR_LIT>' , ( <NUM_LIT> , None ) ) ) <EOL> def test_wsgi ( self ) : <EOL> """<STR_LIT>""" <EOL> options = Options ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , __name__ + '<STR_LIT>' ] ) <EOL> root = options [ '<STR_LIT:root>' ] <EOL> self . assertTrue ( root , WSGIResource ) <EOL> self . assertIdentical ( root . _reactor , reactor ) <EOL> self . assertTrue ( isinstance ( root . _threadpool , ThreadPool ) ) <EOL> self . assertIdentical ( root . _application , application ) <EOL> self . assertFalse ( root . _threadpool . started ) <EOL> reactor . fireSystemEvent ( '<STR_LIT>' ) <EOL> self . assertTrue ( root . _threadpool . started ) <EOL> self . assertFalse ( root . _threadpool . joined ) <EOL> reactor . fireSystemEvent ( '<STR_LIT>' ) <EOL> self . assertTrue ( root . _threadpool . joined ) <EOL> def test_invalidApplication ( self ) : <EOL> """<STR_LIT>""" <EOL> options = Options ( ) <EOL> for name in [ __name__ + '<STR_LIT>' , '<STR_LIT>' ] : <EOL> exc = self . assertRaises ( <EOL> UsageError , options . parseOptions , [ '<STR_LIT>' , name ] ) <EOL> self . assertEqual ( str ( exc ) , "<STR_LIT>" % ( name , ) ) </s>
