本文整理汇总了Python中flak.Flak类的典型用法代码示例。如果您正苦于以下问题:Python Flak类的具体用法?Python Flak怎么用?Python Flak使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Flak类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_preserve_remembers_exception
def test_preserve_remembers_exception():
app = Flak(__name__)
app.debug = True
errors = []
@app.route('/fail')
def fail_func(cx):
1 // 0
@app.route('/success')
def success_func(cx):
return 'Okay'
@app.teardown
def teardown_handler(cx, exc):
errors.append(exc)
c = app.test_client()
# After this failure we did not yet call the teardown handler
with pytest.raises(ZeroDivisionError):
c.get('/fail')
#assert errors == []
assert len(errors) == 1
# But this request triggers it, and it's an error
c.get('/success')
assert len(errors) == 2
# At this point another request does nothing.
c.get('/success')
assert len(errors) == 3
assert errors[1] is None
示例2: test_error_handler_no_match
def test_error_handler_no_match():
app = Flak(__name__)
class CustomException(Exception):
pass
@app.errorhandler(CustomException)
def custom_exception_handler(cx, e):
assert isinstance(e, CustomException)
return 'custom'
@app.errorhandler(500)
def handle_500(cx, e):
return type(e).__name__
@app.route('/custom')
def custom_test(cx):
raise CustomException()
@app.route('/keyerror')
def key_error(cx):
raise KeyError()
c = app.test_client()
assert c.get('/custom').data == b'custom'
assert c.get('/keyerror').data == b'KeyError'
示例3: test_url_processors
def test_url_processors():
app = Flak(__name__)
@app.url_defaults
def add_language_code(cx, endpoint, values):
if cx.lang_code is not None and \
app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
values.setdefault('lang_code', cx.lang_code)
@app.url_value_preprocessor
def pull_lang_code(cx, endpoint, values):
cx.lang_code = values.pop('lang_code', None)
@app.route('/<lang_code>/')
def index(cx):
return cx.url_for('about')
@app.route('/<lang_code>/about')
def about(cx):
return cx.url_for('something_else')
@app.route('/foo')
def something_else(cx):
return cx.url_for('about', lang_code='en')
c = app.test_client()
assert c.get('/de/').data == b'/de/about'
assert c.get('/de/about').data == b'/foo'
assert c.get('/foo').data == b'/en/about'
示例4: test_reuse_client
def test_reuse_client():
app = Flak(__name__)
c = app.test_client()
with c:
assert c.get('/').status_code == 404
with c:
assert c.get('/').status_code == 404
示例5: test_streaming_with_context_and_custom_close
def test_streaming_with_context_and_custom_close(self):
app = Flak(__name__)
called = []
class Wrapper(object):
def __init__(self, gen):
self._gen = gen
def __iter__(self):
return self
def close(self):
called.append(42)
def __next__(self):
return next(self._gen)
next = __next__
@app.route("/")
def index(cx):
def generate():
yield "Hello "
yield cx.request.args["name"]
yield "!"
gen = cx.close_with_generator(Wrapper(generate()))
return flak.Response(gen)
c = app.test_client()
rv = c.get("/?name=World")
assert rv.data == b"Hello World!"
assert called == [42]
示例6: test_request_preprocessing_early_return
def test_request_preprocessing_early_return():
app = Flak(__name__)
evts = []
@app.before_request
def before_request1(cx):
evts.append(1)
@app.before_request
def before_request2(cx):
evts.append(2)
return "hello"
@app.before_request
def before_request3(cx):
evts.append(3)
return "bye"
@app.route('/')
def index(cx):
evts.append('index')
return "damnit"
rv = app.test_client().get('/').data.strip()
assert rv == b'hello'
assert evts == [1, 2]
示例7: test_http_error_subclass_handling
def test_http_error_subclass_handling():
class ForbiddenSubclass(Forbidden):
pass
app = Flak(__name__)
@app.errorhandler(ForbiddenSubclass)
def handle_forbidden_subclass(cx, e):
assert isinstance(e, ForbiddenSubclass)
return 'banana'
@app.errorhandler(403)
def handle_forbidden_subclass(cx, e):
assert not isinstance(e, ForbiddenSubclass)
assert isinstance(e, Forbidden)
return 'apple'
@app.route('/1')
def index1(cx):
raise ForbiddenSubclass()
@app.route('/2')
def index2(cx):
flak.abort(403)
@app.route('/3')
def index3(cx):
raise Forbidden()
c = app.test_client()
assert c.get('/1').data == b'banana'
assert c.get('/2').data == b'apple'
assert c.get('/3').data == b'apple'
示例8: test_session_special_types
def test_session_special_types():
app = Flak(__name__)
app.secret_key = 'development-key'
now = datetime.utcnow().replace(microsecond=0)
the_uuid = uuid.uuid4()
@app.after_request
def modify_session(cx, response):
cx.session['u'] = the_uuid
cx.session['dt'] = now
cx.session['b'] = b'\xff'
cx.session['t'] = (1, 2, 3)
return response
@app.route('/')
def dump_session_contents(cx):
return pickle.dumps(dict(cx.session))
c = app.test_client()
c.get('/')
rv = pickle.loads(c.get('/').data)
assert rv['dt'] == now
assert rv['u'] == the_uuid
assert rv['b'] == b'\xff'
assert type(rv['b']) == bytes
assert rv['t'] == (1, 2, 3)
示例9: test_build_error_handler
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/'
示例10: test_json_customization
def test_json_customization(self):
class X(object):
def __init__(self, val):
self.val = val
class MyEncoder(flak.json.JSONEncoder):
def default(self, o):
if isinstance(o, X):
return "<%d>" % o.val
return flak.json.JSONEncoder.default(self, o)
class MyDecoder(flak.json.JSONDecoder):
def __init__(self, *args, **kwargs):
kwargs.setdefault("object_hook", self.object_hook)
flak.json.JSONDecoder.__init__(self, *args, **kwargs)
def object_hook(self, obj):
if len(obj) == 1 and "_foo" in obj:
return X(obj["_foo"])
return obj
app = Flak(__name__)
app.json_encoder = MyEncoder
app.json_decoder = MyDecoder
@app.route("/", methods=["POST"])
def index(cx):
return cx.dumps(cx.get_json()["x"])
c = app.test_client()
rv = c.post("/", data=json.dumps({"x": {"_foo": 42}}), content_type="application/json")
assert rv.data == b'"<42>"'
示例11: test_logger_cache
def test_logger_cache(self):
app = Flak(__name__)
logger1 = app.logger
assert app.logger is logger1
assert logger1.name == __name__
app.logger_name = __name__ + "/test_logger_cache"
assert app.logger is not logger1
示例12: test_processor_exceptions
def test_processor_exceptions(self):
app = Flak(__name__)
app.config["LOGGER_HANDLER_POLICY"] = "never"
@app.before_request
def before_request():
if trigger == "before":
1 // 0
@app.after_request
def after_request(response):
if trigger == "after":
1 // 0
return response
@app.route("/")
def index():
return "Foo"
@app.errorhandler(500)
def internal_server_error(cx, e):
return "Hello Server Error", 500
for trigger in "before", "after":
rv = app.test_client().get("/")
assert rv.status_code == 500
assert rv.data == b"Hello Server Error"
示例13: test_route_decorator_custom_endpoint
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'
示例14: test_session_expiration
def test_session_expiration():
permanent = True
app = Flak(__name__)
app.secret_key = 'testkey'
@app.route('/')
def index(cx):
cx.session['test'] = 42
cx.session.permanent = permanent
return ''
@app.route('/test')
def test(cx):
return text_type(cx.session.permanent)
client = app.test_client()
rv = client.get('/')
assert 'set-cookie' in rv.headers
match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie'])
expires = parse_date(match.group())
expected = datetime.utcnow() + app.permanent_session_lifetime
assert expires.year == expected.year
assert expires.month == expected.month
assert expires.day == expected.day
rv = client.get('/test')
assert rv.data == b'True'
permanent = False
rv = app.test_client().get('/')
assert 'set-cookie' in rv.headers
match = re.search(r'\bexpires=([^;]+)', rv.headers['set-cookie'])
assert match is None
示例15: test_test_client_context_binding
def test_test_client_context_binding():
app = Flak(__name__)
app.config['LOGGER_HANDLER_POLICY'] = 'never'
@app.route('/')
def index(cx):
cx.globals.value = 42
return 'Hello World!'
@app.route('/other')
def other(cx):
cx.globals.value = 23
1 // 0
with app.test_client() as c:
resp = c.get('/')
cx = c.captured_context
assert cx.globals.value == 42
assert resp.data == b'Hello World!'
assert resp.status_code == 200
resp = c.get('/other')
assert b'Internal Server Error' in resp.data
assert resp.status_code == 500
assert c.captured_context is not cx
cx = c.captured_context
assert cx.globals.value == 23