def test_basic_view(): app = Flak(__name__) class Index(views.View): methods = ['GET', 'POST'] def dispatch_request(self, cx): return cx.request.method app.add_url_rule('/', Index.as_view('index')) common_asserts(app)
def test_method_based_view(): app = Flak(__name__) class Index(views.MethodView): def get(self, cx): return 'GET' def post(self, cx): return 'POST' app.add_url_rule('/', Index.as_view('index')) common_asserts(app)
def test_jsonify_date_types(self): test_dates = (datetime.datetime(1973, 3, 11, 6, 30, 45), datetime.date(1975, 1, 5)) app = Flak(__name__) c = app.test_client() for i, d in enumerate(test_dates): url = "/datetest{0}".format(i) f = lambda cx, val=d: cx.jsonify(x=val) app.add_url_rule(url, f, str(i)) rv = c.get(url) assert rv.mimetype == "application/json" assert json.loads(rv.data)["x"] == http_date(d.timetuple())
def test_explicit_head(): app = Flak(__name__) class Index(views.MethodView): def get(self, cx): return 'GET' def head(self, cx): return Response('', headers={'X-Method': 'HEAD'}) app.add_url_rule('/', Index.as_view('index')) c = app.test_client() rv = c.get('/') assert rv.data == b'GET' rv = c.head('/') assert rv.data == b'' assert rv.headers['X-Method'] == 'HEAD'
def test_endpoint_override(): app = Flak(__name__) app.debug = True class Index(views.View): methods = ['GET', 'POST'] def dispatch_request(self, cx): return cx.request.method app.add_url_rule('/', Index.as_view('index')) with pytest.raises(AssertionError): app.add_url_rule('/', Index.as_view('index')) # But these tests should still pass. We just log a warning. common_asserts(app)
def test_implicit_head(): app = Flak(__name__) class Index(views.MethodView): def get(self, cx): headers={'X-Method': cx.request.method} return Response('Blub', headers=headers) app.add_url_rule('/', Index.as_view('index')) c = app.test_client() rv = c.get('/') assert rv.data == b'Blub' assert rv.headers['X-Method'] == 'GET' rv = c.head('/') assert rv.data == b'' assert rv.headers['X-Method'] == 'HEAD'
def test_view_inheritance(): app = Flak(__name__) class Index(views.MethodView): def get(self, cx): return 'GET' def post(self, cx): return 'POST' class BetterIndex(Index): def delete(self, cx): return 'DELETE' app.add_url_rule('/', BetterIndex.as_view('index')) c = app.test_client() meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow']) assert sorted(meths) == ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST']
def test_view_patching(): app = Flak(__name__) class Index(views.MethodView): def get(self, cx): 1 // 0 def post(self, cx): 1 // 0 class Other(Index): def get(self, cx): return 'GET' def post(self, cx): return 'POST' view = Index.as_view('index') view.view_class = Other app.add_url_rule('/', view) common_asserts(app)
def test_view_decorators(): app = Flak(__name__) def add_x_parachute(f): def new_function(cx, *args, **kwargs): resp = cx.make_response(f(cx, *args, **kwargs)) resp.headers['X-Parachute'] = 'awesome' return resp return new_function class Index(views.View): decorators = [add_x_parachute] def dispatch_request(self, cx): return 'Awesome' app.add_url_rule('/', Index.as_view('index')) c = app.test_client() rv = c.get('/') assert rv.headers['X-Parachute'] == 'awesome' assert rv.data == b'Awesome'
def test_url_with_method(self): from flak.views import MethodView app = Flak(__name__) class MyView(MethodView): def get(self, id=None): if id is None: return "List" return "Get %d" % id def post(self): return "Create" myview = MyView.as_view("myview") app.add_url_rule("/myview/", myview, methods=["GET"]) app.add_url_rule("/myview/<int:id>", myview, methods=["GET"]) app.add_url_rule("/myview/create", myview, methods=["POST"]) with app.test_context() as cx: assert cx.url_for("myview", _method="GET") == "/myview/" assert cx.url_for("myview", id=42, _method="GET") == "/myview/42" assert cx.url_for("myview", _method="POST") == "/myview/create"
def test_url_mapping(): app = Flak(__name__) random_uuid4 = "7eb41166-9ebf-4d26-b771-ea3f54f8b383" def index(cx): return cx.request.method def more(cx): return cx.request.method def options(cx): return random_uuid4 app.add_url_rule('/', index) app.add_url_rule('/more', more, methods=['GET', 'POST']) # Issue 1288: Test that automatic options are not added when non-uppercase 'options' in methods app.add_url_rule('/options', options, methods=['options']) c = app.test_client() assert c.get('/').data == b'GET' rv = c.post('/') assert rv.status_code == 405 assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS'] rv = c.head('/') assert rv.status_code == 200 assert not rv.data # head truncates assert c.post('/more').data == b'POST' assert c.get('/more').data == b'GET' rv = c.delete('/more') assert rv.status_code == 405 assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] rv = c.open('/options', method='OPTIONS') assert rv.status_code == 200 assert random_uuid4 in rv.data.decode("utf-8")