(Flask) 伪造 request.environ['REMOTE_USER'] 进行测试
(Flask) Faking request.environ['REMOTE_USER'] for testing
我正在 IIS 上部署 Flask 应用程序并使用其 Windows 身份验证,如果身份验证成功,它将 request.environ['REMOTE_USER'] 设置为您的 windows 用户名。现在写测试用例的时候,怎么伪造request.environ['REMOTE_USER']?测试用例 运行 独立于 IIS 服务器。
我的尝试:
from flask import request
def test_insert_cash_flow_through_post(self):
"""Test that you can insert a cash flow through post."""
request.environ['REMOTE_USER'] = 'foo'
self.client.post("/index?account=main",
data=dict(settlement_date='01/01/2016',
transaction_type='Other',
certainty='Certain',
transaction_amount=1))
assert CashFlow.query.first().user == 'foo'
我视图中处理 'REMOTE_USER' 的部分类似于:
cf = CashFlow(...,
user=request.environ.get('REMOTE_USER'),
...)
db.session.add(cf)
你不必伪装它。你可以在你的测试中设置它。
from flask import request
def test_something():
request.environ['REMOTE_USER'] = 'some user'
do_something_with_remote_user()
del request.environ['REMOTE_USER']
如果您担心保留任何可能已经设置的值,您也可以轻松做到这一点。
def test_something():
original_remote_user = request.environ.get('REMOTE_USER')
do_something_with_remote_user()
request.environ['REMOTE_USER'] = original_remote_user
您也可以在更高的范围内处理这个问题,但是如果不知道您的测试是如何构建的,就很难告诉您如何去做。
从 Setting (mocking) request headers for Flask app unit test. There is an environ_base
parameter that you can pass request environment variables into. It is documented in werkzeug.test.EnvironBuilder 中找到了我自己的问题的答案。
def test_insert_cash_flow_through_post(self):
"""Test that you can insert a cash flow through post."""
assert not CashFlow.query.first()
self.client.post("/index?account=main",
environ_base={'REMOTE_USER': 'foo'},
data=dict(settlement_date='01/01/2016',
transaction_type='Other',
certainty='Certain',
transaction_amount=1))
assert CashFlow.query.first().user == 'foo'
我正在 IIS 上部署 Flask 应用程序并使用其 Windows 身份验证,如果身份验证成功,它将 request.environ['REMOTE_USER'] 设置为您的 windows 用户名。现在写测试用例的时候,怎么伪造request.environ['REMOTE_USER']?测试用例 运行 独立于 IIS 服务器。
我的尝试:
from flask import request
def test_insert_cash_flow_through_post(self):
"""Test that you can insert a cash flow through post."""
request.environ['REMOTE_USER'] = 'foo'
self.client.post("/index?account=main",
data=dict(settlement_date='01/01/2016',
transaction_type='Other',
certainty='Certain',
transaction_amount=1))
assert CashFlow.query.first().user == 'foo'
我视图中处理 'REMOTE_USER' 的部分类似于:
cf = CashFlow(...,
user=request.environ.get('REMOTE_USER'),
...)
db.session.add(cf)
你不必伪装它。你可以在你的测试中设置它。
from flask import request
def test_something():
request.environ['REMOTE_USER'] = 'some user'
do_something_with_remote_user()
del request.environ['REMOTE_USER']
如果您担心保留任何可能已经设置的值,您也可以轻松做到这一点。
def test_something():
original_remote_user = request.environ.get('REMOTE_USER')
do_something_with_remote_user()
request.environ['REMOTE_USER'] = original_remote_user
您也可以在更高的范围内处理这个问题,但是如果不知道您的测试是如何构建的,就很难告诉您如何去做。
从 Setting (mocking) request headers for Flask app unit test. There is an environ_base
parameter that you can pass request environment variables into. It is documented in werkzeug.test.EnvironBuilder 中找到了我自己的问题的答案。
def test_insert_cash_flow_through_post(self):
"""Test that you can insert a cash flow through post."""
assert not CashFlow.query.first()
self.client.post("/index?account=main",
environ_base={'REMOTE_USER': 'foo'},
data=dict(settlement_date='01/01/2016',
transaction_type='Other',
certainty='Certain',
transaction_amount=1))
assert CashFlow.query.first().user == 'foo'