本文整理汇总了Python中flak.Flak.test_context方法的典型用法代码示例。如果您正苦于以下问题:Python Flak.test_context方法的具体用法?Python Flak.test_context怎么用?Python Flak.test_context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flak.Flak
的用法示例。
在下文中一共展示了Flak.test_context方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_build_error_handler
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_build_error_handler():
app = Flak(__name__)
# Test base case, a URL which results in a BuildError.
with app.test_context() as cx:
pytest.raises(BuildError, cx.url_for, 'spam')
# Verify the error is re-raised if not the current exception.
try:
with app.test_context() as cx:
cx.url_for('spam')
except BuildError as err:
error = err
try:
raise RuntimeError('Test case where BuildError is not current.')
except RuntimeError:
pytest.raises(BuildError, app.handle_url_build_error,
None, error, 'spam', {})
# Test a custom handler.
def handler(cx, error, endpoint, values):
# Just a test.
return '/test_handler/'
app.url_build_error_handlers.append(handler)
with app.test_context() as cx:
assert cx.url_for('spam') == '/test_handler/'
示例2: test_route_decorator_custom_endpoint
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_route_decorator_custom_endpoint():
app = Flak(__name__)
app.debug = True
@app.route('/foo/')
def foo(cx):
return cx.request.endpoint
@app.route('/bar/', endpoint='bar')
def for_bar(cx):
return cx.request.endpoint
@app.route('/bar/123', endpoint='123')
def for_bar_foo(cx):
return cx.request.endpoint
with app.test_context() as cx:
assert cx.url_for('foo') == '/foo/'
assert cx.url_for('bar') == '/bar/'
assert cx.url_for('123') == '/bar/123'
c = app.test_client()
assert c.get('/foo/').data == b'foo'
assert c.get('/bar/').data == b'bar'
assert c.get('/bar/123').data == b'123'
示例3: test_build_error_handler_reraise
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_build_error_handler_reraise():
app = Flak(__name__)
# Test a custom handler which reraises the BuildError
def handler_raises_build_error(cx, error, endpoint, values):
raise error
app.url_build_error_handlers.append(handler_raises_build_error)
with app.test_context() as cx:
pytest.raises(BuildError, cx.url_for, 'not.existing')
示例4: test_url_for_with_scheme
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_url_for_with_scheme(self):
app = Flak(__name__)
@app.route("/")
def index():
return "42"
with app.test_context() as cx:
assert cx.url_for("index", _external=True, _scheme="https") == "https://localhost/"
示例5: test_url_for_with_scheme_not_external
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_url_for_with_scheme_not_external(self):
app = Flak(__name__)
@app.route("/")
def index():
return "42"
with app.test_context() as cx:
pytest.raises(ValueError, cx.url_for, "index", _scheme="https")
示例6: test_jsonify_no_prettyprint
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_jsonify_no_prettyprint():
app = Flak(__name__)
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
with app.test_context() as cx:
json = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'
obj = {"msg": {"submsg": "W00t"},
"msg2": "foobar"}
rv = cx.make_response(cx.jsonify(obj), 200)
assert rv.data == json
示例7: test_url_for_with_anchor
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_url_for_with_anchor(self):
app = Flak(__name__)
@app.route("/")
def index():
return "42"
with app.test_context() as cx:
assert cx.url_for("index", _anchor="x y") == "/#x%20y"
示例8: test_url_generation
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_url_generation():
app = Flak(__name__)
@app.route('/hello/<name>', methods=['POST'])
def hello(cx):
pass
with app.test_context() as cx:
assert cx.url_for('hello', name='test x') == '/hello/test%20x'
assert cx.url_for('hello', name='test x', _external=True) == \
'http://localhost/hello/test%20x'
示例9: test_jsonify_prettyprint
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_jsonify_prettyprint():
app = Flak(__name__)
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True})
with app.test_context() as cx:
compressed = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
expect =\
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'
rv = cx.make_response(cx.jsonify(compressed), 200)
assert rv.data == expect
示例10: test_environ_defaults
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_environ_defaults():
app = Flak(__name__)
@app.route('/')
def index(cx):
return cx.request.url
cx = app.test_context()
assert cx.request.url == 'http://localhost/'
with app.test_client() as c:
rv = c.get('/')
assert rv.data == b'http://localhost/'
示例11: test_environ_defaults_from_config
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_environ_defaults_from_config():
app = Flak(__name__)
app.config['SERVER_NAME'] = 'example.com:1234'
app.config['APPLICATION_ROOT'] = '/foo'
@app.route('/')
def index(cx):
return cx.request.url
cx = app.test_context()
assert cx.request.url == 'http://example.com:1234/foo/'
with app.test_client() as c:
rv = c.get('/')
assert rv.data == b'http://example.com:1234/foo/'
示例12: test_nosubdomain
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_nosubdomain():
app = Flak(__name__)
app.config['SERVER_NAME'] = 'example.com'
@app.route('/<company_id>')
def view(cx, company_id):
return company_id
with app.test_context() as cx:
url = cx.url_for('view', company_id='xxx')
with app.test_client() as c:
response = c.get(url)
assert 200 == response.status_code
assert b'xxx' == response.data
示例13: test_make_response
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_make_response():
app = Flak(__name__)
with app.test_context() as cx:
rv = cx.make_response()
assert rv.status_code == 200
assert rv.data == b''
assert rv.mimetype == 'text/plain'
rv = cx.make_response('Awesome')
assert rv.status_code == 200
assert rv.data == b'Awesome'
assert rv.mimetype == 'text/plain'
rv = cx.make_response('W00t', 404)
assert rv.status_code == 404
assert rv.data == b'W00t'
assert rv.mimetype == 'text/plain'
示例14: test_make_response_with_response_instance
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [as 别名]
def test_make_response_with_response_instance():
app = Flak(__name__)
with app.test_context() as cx:
rv = cx.make_response(cx.jsonify({'msg': 'W00t'}), 400)
assert rv.status_code == 400
assert rv.data == b'{\n "msg": "W00t"\n}\n'
assert rv.mimetype == 'application/json'
rv = cx.make_response(flak.Response(''), 400)
assert rv.status_code == 400
assert rv.data == b''
assert rv.mimetype == 'text/plain'
rsp = flak.Response('', headers={'Content-Type': 'text/html'})
rv = cx.make_response(rsp, 400, [('X-Foo', 'bar')])
assert rv.status_code == 400
assert rv.headers['Content-Type'] == 'text/html'
assert rv.headers['X-Foo'] == 'bar'
示例15: test_url_with_method
# 需要导入模块: from flak import Flak [as 别名]
# 或者: from flak.Flak import test_context [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"