<s> """<STR_LIT>""" <EOL> import flask <EOL> import flask . views <EOL> import unittest <EOL> from flask . testsuite import FlaskTestCase <EOL> from werkzeug . http import parse_set_header <EOL> class ViewTestCase ( FlaskTestCase ) : <EOL> def common_test ( self , app ) : <EOL> c = app . test_client ( ) <EOL> self . assert_equal ( c . get ( '<STR_LIT:/>' ) . data , b'<STR_LIT:GET>' ) <EOL> self . assert_equal ( c . post ( '<STR_LIT:/>' ) . data , b'<STR_LIT:POST>' ) <EOL> self . assert_equal ( c . put ( '<STR_LIT:/>' ) . status_code , <NUM_LIT> ) <EOL> meths = parse_set_header ( c . open ( '<STR_LIT:/>' , method = '<STR_LIT>' ) . headers [ '<STR_LIT>' ] ) <EOL> self . assert_equal ( sorted ( meths ) , [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:POST>' ] ) <EOL> def test_basic_view ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . View ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self ) : <EOL> return flask . request . method <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> self . common_test ( app ) <EOL> def test_method_based_view ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . MethodView ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT:GET>' <EOL> def post ( self ) : <EOL> return '<STR_LIT:POST>' <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> self . common_test ( app ) <EOL> def test_view_patching ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . MethodView ) : <EOL> def get ( self ) : <EOL> <NUM_LIT:1> // <NUM_LIT:0> <EOL> def post ( self ) : <EOL> <NUM_LIT:1> // <NUM_LIT:0> <EOL> class Other ( Index ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT:GET>' <EOL> def post ( self ) : <EOL> return '<STR_LIT:POST>' <EOL> view = Index . as_view ( '<STR_LIT:index>' ) <EOL> view . view_class = Other <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = view ) <EOL> self . common_test ( app ) <EOL> def test_view_inheritance ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . MethodView ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT:GET>' <EOL> def post ( self ) : <EOL> return '<STR_LIT:POST>' <EOL> class BetterIndex ( Index ) : <EOL> def delete ( self ) : <EOL> return '<STR_LIT>' <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = BetterIndex . as_view ( '<STR_LIT:index>' ) ) <EOL> c = app . test_client ( ) <EOL> meths = parse_set_header ( c . open ( '<STR_LIT:/>' , method = '<STR_LIT>' ) . headers [ '<STR_LIT>' ] ) <EOL> self . assert_equal ( sorted ( meths ) , [ '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:POST>' ] ) <EOL> def test_view_decorators ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> def add_x_parachute ( f ) : <EOL> def new_function ( * args , ** kwargs ) : <EOL> resp = flask . make_response ( f ( * args , ** kwargs ) ) <EOL> resp . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return resp <EOL> return new_function <EOL> class Index ( flask . views . View ) : <EOL> decorators = [ add_x_parachute ] <EOL> def dispatch_request ( self ) : <EOL> return '<STR_LIT>' <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> c = app . test_client ( ) <EOL> rv = c . get ( '<STR_LIT:/>' ) <EOL> self . assert_equal ( rv . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> def test_implicit_head ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . MethodView ) : <EOL> def get ( self ) : <EOL> return flask . Response ( '<STR_LIT>' , headers = { <EOL> '<STR_LIT>' : flask . request . method <EOL> } ) <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> c = app . test_client ( ) <EOL> rv = c . get ( '<STR_LIT:/>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> self . assert_equal ( rv . headers [ '<STR_LIT>' ] , '<STR_LIT:GET>' ) <EOL> rv = c . head ( '<STR_LIT:/>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> self . assert_equal ( rv . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_explicit_head ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> class Index ( flask . views . MethodView ) : <EOL> def get ( self ) : <EOL> return '<STR_LIT:GET>' <EOL> def head ( self ) : <EOL> return flask . Response ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> c = app . test_client ( ) <EOL> rv = c . get ( '<STR_LIT:/>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT:GET>' ) <EOL> rv = c . head ( '<STR_LIT:/>' ) <EOL> self . assert_equal ( rv . data , b'<STR_LIT>' ) <EOL> self . assert_equal ( rv . headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_endpoint_override ( self ) : <EOL> app = flask . Flask ( __name__ ) <EOL> app . debug = True <EOL> class Index ( flask . views . View ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self ) : <EOL> return flask . request . method <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> with self . assert_raises ( AssertionError ) : <EOL> app . add_url_rule ( '<STR_LIT:/>' , view_func = Index . as_view ( '<STR_LIT:index>' ) ) <EOL> self . common_test ( app ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( ViewTestCase ) ) <EOL> return suite </s>
