<s> """<STR_LIT>""" <EOL> import flask <EOL> import unittest <EOL> try : <EOL> from greenlet import greenlet <EOL> except ImportError : <EOL> greenlet = None <EOL> from flask . testsuite import FlaskTestCase <EOL> class RequestContextTestCase ( FlaskTestCase ) : <EOL> def test_teardown_on_pop ( self ) : <EOL> buffer = [ ] <EOL> app = flask . Flask ( __name__ ) <EOL> @ app . teardown_request <EOL> def end_of_request ( exception ) : <EOL> buffer . append ( exception ) <EOL> ctx = app . test_request_context ( ) <EOL> ctx . push ( ) <EOL> self . assert_equal ( buffer , [ ] ) <EOL> ctx . pop ( ) <EOL> self . assert_equal ( buffer , [ None ] ) <EOL> def test_proper_test_request_context ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> app . config . update ( <EOL> SERVER_NAME = '<STR_LIT>' <EOL> ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return None <EOL> @ app . route ( '<STR_LIT:/>' , subdomain = '<STR_LIT:foo>' ) <EOL> def sub ( ) : <EOL> return None <EOL> with app . test_request_context ( '<STR_LIT:/>' ) : <EOL> self . assert_equal ( flask . url_for ( '<STR_LIT:index>' , _external = True ) , '<STR_LIT>' ) <EOL> with app . test_request_context ( '<STR_LIT:/>' ) : <EOL> self . assert_equal ( flask . url_for ( '<STR_LIT>' , _external = True ) , '<STR_LIT>' ) <EOL> try : <EOL> with app . test_request_context ( '<STR_LIT:/>' , environ_overrides = { '<STR_LIT>' : '<STR_LIT:localhost>' } ) : <EOL> pass <EOL> except Exception as e : <EOL> self . assert_true ( isinstance ( e , ValueError ) ) <EOL> self . assert_equal ( str ( e ) , "<STR_LIT>" + <EOL> "<STR_LIT>" + "<STR_LIT>" ) <EOL> try : <EOL> app . config . update ( SERVER_NAME = '<STR_LIT:localhost>' ) <EOL> with app . test_request_context ( '<STR_LIT:/>' , environ_overrides = { '<STR_LIT>' : '<STR_LIT:localhost>' } ) : <EOL> pass <EOL> except ValueError as e : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % e <EOL> ) <EOL> try : <EOL> app . config . update ( SERVER_NAME = '<STR_LIT>' ) <EOL> with app . test_request_context ( '<STR_LIT:/>' , environ_overrides = { '<STR_LIT>' : '<STR_LIT>' } ) : <EOL> pass <EOL> except ValueError as e : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % e <EOL> ) <EOL> def test_context_binding ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return '<STR_LIT>' % flask . request . args [ '<STR_LIT:name>' ] <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def meh ( ) : <EOL> return flask . request . url <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assert_equal ( index ( ) , '<STR_LIT>' ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assert_equal ( meh ( ) , '<STR_LIT>' ) <EOL> self . assert_true ( flask . _request_ctx_stack . top is None ) <EOL> def test_context_test ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> self . assert_false ( flask . request ) <EOL> self . assert_false ( flask . has_request_context ( ) ) <EOL> ctx = app . test_request_context ( ) <EOL> ctx . push ( ) <EOL> try : <EOL> self . assert_true ( flask . request ) <EOL> self . assert_true ( flask . has_request_context ( ) ) <EOL> finally : <EOL> ctx . pop ( ) <EOL> def test_manual_context_binding ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return '<STR_LIT>' % flask . request . args [ '<STR_LIT:name>' ] <EOL> ctx = app . test_request_context ( '<STR_LIT>' ) <EOL> ctx . push ( ) <EOL> self . assert_equal ( index ( ) , '<STR_LIT>' ) <EOL> ctx . pop ( ) <EOL> try : <EOL> index ( ) <EOL> except RuntimeError : <EOL> pass <EOL> else : <EOL> self . assert_true ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> def test_greenlet_context_copying ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> greenlets = [ ] <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> reqctx = flask . _request_ctx_stack . top . copy ( ) <EOL> def g ( ) : <EOL> self . assert_false ( flask . request ) <EOL> self . assert_false ( flask . current_app ) <EOL> with reqctx : <EOL> self . assert_true ( flask . request ) <EOL> self . assert_equal ( flask . current_app , app ) <EOL> self . assert_equal ( flask . request . path , '<STR_LIT:/>' ) <EOL> self . assert_equal ( flask . request . args [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> self . assert_false ( flask . request ) <EOL> return <NUM_LIT> <EOL> greenlets . append ( greenlet ( g ) ) <EOL> return '<STR_LIT>' <EOL> rv = app . test_client ( ) . get ( '<STR_LIT>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> result = greenlets [ <NUM_LIT:0> ] . run ( ) <EOL> self . assert_equal ( result , <NUM_LIT> ) <EOL> def test_greenlet_context_copying_api ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> greenlets = [ ] <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> reqctx = flask . _request_ctx_stack . top . copy ( ) <EOL> @ flask . copy_current_request_context <EOL> def g ( ) : <EOL> self . assert_true ( flask . request ) <EOL> self . assert_equal ( flask . current_app , app ) <EOL> self . assert_equal ( flask . request . path , '<STR_LIT:/>' ) <EOL> self . assert_equal ( flask . request . args [ '<STR_LIT:foo>' ] , '<STR_LIT:bar>' ) <EOL> return <NUM_LIT> <EOL> greenlets . append ( greenlet ( g ) ) <EOL> return '<STR_LIT>' <EOL> rv = app . test_client ( ) . get ( '<STR_LIT>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> result = greenlets [ <NUM_LIT:0> ] . run ( ) <EOL> self . assert_equal ( result , <NUM_LIT> ) <EOL> if greenlet is None : <EOL> test_greenlet_context_copying = None <EOL> test_greenlet_context_copying_api = None <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( RequestContextTestCase ) ) <EOL> return suite </s>
