当前位置: 首页>>代码示例>>Python>>正文


Python Flak.add_url_rule方法代码示例

本文整理汇总了Python中flak.Flak.add_url_rule方法的典型用法代码示例。如果您正苦于以下问题:Python Flak.add_url_rule方法的具体用法?Python Flak.add_url_rule怎么用?Python Flak.add_url_rule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在flak.Flak的用法示例。


在下文中一共展示了Flak.add_url_rule方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_basic_view

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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)
开发者ID:avdd,项目名称:flak,代码行数:12,代码来源:test_views.py

示例2: test_method_based_view

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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)
开发者ID:avdd,项目名称:flak,代码行数:13,代码来源:test_views.py

示例3: test_jsonify_date_types

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
    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())
开发者ID:avdd,项目名称:flak,代码行数:15,代码来源:test_helpers.py

示例4: test_explicit_head

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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'
开发者ID:avdd,项目名称:flak,代码行数:18,代码来源:test_views.py

示例5: test_endpoint_override

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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)
开发者ID:avdd,项目名称:flak,代码行数:18,代码来源:test_views.py

示例6: test_implicit_head

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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'
开发者ID:avdd,项目名称:flak,代码行数:18,代码来源:test_views.py

示例7: test_view_inheritance

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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']
开发者ID:avdd,项目名称:flak,代码行数:20,代码来源:test_views.py

示例8: test_view_patching

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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)
开发者ID:avdd,项目名称:flak,代码行数:21,代码来源:test_views.py

示例9: test_view_decorators

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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'
开发者ID:avdd,项目名称:flak,代码行数:22,代码来源:test_views.py

示例10: test_url_with_method

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
    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"
开发者ID:avdd,项目名称:flak,代码行数:25,代码来源:test_helpers.py

示例11: test_url_mapping

# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import add_url_rule [as 别名]
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")
开发者ID:avdd,项目名称:flak,代码行数:39,代码来源:test_basic.py


注:本文中的flak.Flak.add_url_rule方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。