<s> from cherrypy . _cpcompat import ntob <EOL> from cherrypy . test import helper <EOL> class WSGIGraftTests ( helper . CPWebCase ) : <EOL> def setup_server ( ) : <EOL> import os <EOL> curdir = os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) <EOL> import cherrypy <EOL> def test_app ( environ , start_response ) : <EOL> status = '<STR_LIT>' <EOL> response_headers = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> start_response ( status , response_headers ) <EOL> output = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> keys = list ( environ . keys ( ) ) <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> output . append ( '<STR_LIT>' % ( k , environ [ k ] ) ) <EOL> return [ ntob ( x , '<STR_LIT:utf-8>' ) for x in output ] <EOL> def test_empty_string_app ( environ , start_response ) : <EOL> status = '<STR_LIT>' <EOL> response_headers = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> start_response ( status , response_headers ) <EOL> return [ ntob ( '<STR_LIT>' ) , ntob ( '<STR_LIT>' ) , ntob ( '<STR_LIT:U+0020>' ) , ntob ( '<STR_LIT>' ) , ntob ( '<STR_LIT>' ) ] <EOL> class WSGIResponse ( object ) : <EOL> def __init__ ( self , appresults ) : <EOL> self . appresults = appresults <EOL> self . iter = iter ( appresults ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . iter . next ( ) <EOL> def __next__ ( self ) : <EOL> return next ( self . iter ) <EOL> def close ( self ) : <EOL> if hasattr ( self . appresults , "<STR_LIT>" ) : <EOL> self . appresults . close ( ) <EOL> class ReversingMiddleware ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> results = app ( environ , start_response ) <EOL> class Reverser ( WSGIResponse ) : <EOL> def next ( this ) : <EOL> line = list ( this . iter . next ( ) ) <EOL> line . reverse ( ) <EOL> return "<STR_LIT>" . join ( line ) <EOL> def __next__ ( this ) : <EOL> line = list ( next ( this . iter ) ) <EOL> line . reverse ( ) <EOL> return bytes ( line ) <EOL> return Reverser ( results ) <EOL> class Root : <EOL> def index ( self ) : <EOL> return ntob ( "<STR_LIT>" ) <EOL> index . exposed = True <EOL> cherrypy . tree . mount ( Root ( ) ) <EOL> cherrypy . tree . graft ( test_app , '<STR_LIT>' ) <EOL> cherrypy . tree . graft ( test_empty_string_app , '<STR_LIT>' ) <EOL> app = cherrypy . Application ( Root ( ) , script_name = None ) <EOL> cherrypy . tree . graft ( ReversingMiddleware ( app ) , '<STR_LIT>' ) <EOL> setup_server = staticmethod ( setup_server ) <EOL> wsgi_output = '''<STR_LIT>''' <EOL> def test_01_standard_app ( self ) : <EOL> self . getPage ( "<STR_LIT:/>" ) <EOL> self . assertBody ( "<STR_LIT>" ) <EOL> def test_04_pure_wsgi ( self ) : <EOL> import cherrypy <EOL> if not cherrypy . server . using_wsgi : <EOL> return self . skip ( "<STR_LIT>" ) <EOL> self . getPage ( "<STR_LIT>" ) <EOL> self . assertHeader ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> self . assertInBody ( self . wsgi_output ) <EOL> def test_05_wrapped_cp_app ( self ) : <EOL> import cherrypy <EOL> if not cherrypy . server . using_wsgi : <EOL> return self . skip ( "<STR_LIT>" ) <EOL> self . getPage ( "<STR_LIT>" ) <EOL> body = list ( "<STR_LIT>" ) <EOL> body . reverse ( ) <EOL> body = "<STR_LIT>" . join ( body ) <EOL> self . assertInBody ( body ) <EOL> def test_06_empty_string_app ( self ) : <EOL> import cherrypy <EOL> if not cherrypy . server . using_wsgi : <EOL> return self . skip ( "<STR_LIT>" ) <EOL> self . getPage ( "<STR_LIT>" ) <EOL> self . assertHeader ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> self . assertInBody ( '<STR_LIT>' ) </s>
