<s> from twisted . internet . defer import succeed <EOL> from twisted . web import server <EOL> from twisted . web . test . test_web import DummyRequest <EOL> class SmartDummyRequest ( DummyRequest ) : <EOL> def __init__ ( self , method , url , args = None ) : <EOL> DummyRequest . __init__ ( self , url . split ( '<STR_LIT:/>' ) ) <EOL> self . method = method <EOL> args = args or { } <EOL> for k , v in args . items ( ) : <EOL> self . addArg ( k , v ) <EOL> def value ( self ) : <EOL> return "<STR_LIT>" . join ( self . written ) <EOL> class DummySite ( server . Site ) : <EOL> def get ( self , url , args = None ) : <EOL> return self . _request ( "<STR_LIT:GET>" , url , args ) <EOL> def post ( self , url , args = None ) : <EOL> return self . _request ( "<STR_LIT:POST>" , url , args ) <EOL> def _request ( self , method , url , args ) : <EOL> request = SmartDummyRequest ( method , url , args ) <EOL> resource = self . getResourceFor ( request ) <EOL> result = resource . render ( request ) <EOL> return self . _resolveResult ( request , result ) <EOL> def _resolveResult ( self , request , result ) : <EOL> if isinstance ( result , str ) : <EOL> request . write ( result ) <EOL> request . finish ( ) <EOL> return succeed ( request ) <EOL> elif result is server . NOT_DONE_YET : <EOL> if request . finished : <EOL> return succeed ( request ) <EOL> else : <EOL> return request . notifyFinish ( ) . addCallback ( lambda _ : request ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( result , ) ) </s>
