<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import unittest <EOL> import wsgiref . headers <EOL> from google . appengine . tools . devappserver2 import request_rewriter <EOL> class WSGITestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def _normalize_headers ( headers ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( headers , dict ) or <EOL> isinstance ( headers , wsgiref . headers . Headers ) ) : <EOL> headers = headers . items ( ) <EOL> return [ ( name . lower ( ) , value ) for name , value in headers ] <EOL> def assertHeadersEqual ( self , expected , actual , msg = None ) : <EOL> """<STR_LIT>""" <EOL> expected = self . _normalize_headers ( expected ) <EOL> actual = self . _normalize_headers ( actual ) <EOL> for name , value in actual : <EOL> self . assertIsInstance ( name , str , '<STR_LIT>' % name ) <EOL> self . assertIsInstance ( name , str , '<STR_LIT>' % value ) <EOL> self . assertItemsEqual ( expected , actual , msg ) <EOL> def assertResponse ( self , <EOL> expected_status , <EOL> expected_headers , <EOL> expected_content , <EOL> fn , <EOL> * args , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> write_buffer = cStringIO . StringIO ( ) <EOL> def start_response ( status , headers , exc_info = None ) : <EOL> self . assertEqual ( expected_status , status ) <EOL> self . assertHeadersEqual ( expected_headers , headers ) <EOL> self . assertEqual ( None , exc_info ) <EOL> return write_buffer . write <EOL> args += ( start_response , ) <EOL> response = fn ( * args , ** kwargs ) <EOL> self . assertNotEqual ( None , response , '<STR_LIT>' % fn ) <EOL> response = '<STR_LIT>' . join ( response ) <EOL> self . assertIsInstance ( <EOL> response , str , '<STR_LIT>' % response [ : <NUM_LIT:10> ] ) <EOL> self . assertMultiLineEqual ( expected_content , <EOL> write_buffer . getvalue ( ) + response ) <EOL> class RewriterTestCase ( WSGITestCase ) : <EOL> """<STR_LIT>""" <EOL> rewriter_middleware = staticmethod ( <EOL> request_rewriter . frontend_rewriter_middleware ) <EOL> def assert_rewritten_response ( self , expected_status , expected_headers , <EOL> expected_body , application , environ = None ) : <EOL> """<STR_LIT>""" <EOL> if environ is None : <EOL> environ = { } <EOL> wrapped_application = self . rewriter_middleware ( application ) <EOL> self . assertResponse ( expected_status , expected_headers , expected_body , <EOL> wrapped_application , environ ) <EOL> def constant_app ( status , headers , body ) : <EOL> """<STR_LIT>""" <EOL> def application ( unused_environ , start_response ) : <EOL> start_response ( status , headers ) <EOL> return [ body ] <EOL> return application </s>
