python - 如何将标题添加到烧瓶 test_request_context?
问题描述
我有一个测试夹具可以在我的烧瓶应用程序中创建一个测试客户端:
@pytest.fixture(scope='session')
def test_client():
""" Create the application and the test client.
"""
print('----------Setup test client----------')
app = create_app(config_class=config.TestConfig)
testing_client = app.test_client()
ctx = app.test_request_context()
ctx.push()
yield testing_client # this is where the testing happens
print('-------Teardown test client--------')
ctx.pop()
我想在此测试请求上下文中添加“User-Agent”的标头,因为在我的应用程序中,我检查了用户的浏览器。我正在通过我的应用程序中检索浏览器名称
user_agent = flask.request.user_agent.browser
如果我使用上面的测试客户端,此行将返回 None。虽然我可以在单个测试中成功设置单个请求中的“用户代理”标头,例如:
user_agent_str = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
response = test_client.get('/some_flask_route',
environ_base={'HTTP_USER_AGENT': user_agent_str},
follow_redirects=True)
执行此操作后:
flask.request.user_agent.browser
按预期返回“chrome”。
但是,这是非常多余的,因为它需要我将 environ_base 代码行插入到我的许多测试中的每一个中。
预期行为
我希望在测试夹具中创建测试请求上下文时可以设置标头,例如:
user_agent_str = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
@pytest.fixture(scope='session')
def test_client():
""" Create the application and the test client.
"""
print('----------Setup test client----------')
app = create_app(config_class=config.TestConfig)
testing_client = app.test_client()
ctx = app.test_request_context(environ_base={'HTTP_USER_AGENT': user_agent_str})
ctx.push()
yield testing_client # this is where the testing happens
print('-------Teardown test client--------')
ctx.pop()
这将为所有请求设置环境,从而无需在我的每个请求中设置 environ_base。
实际行为
虽然将 environ_base 添加到 test_request_context() 不会破坏夹具,但它不会设置“用户代理”标头和
flask.request.user_agent.browser
返回无。
环境
- Python版本:3.7.3
- 烧瓶版本:1.1.1
- Werkzeug 版本:0.15.4
我在这里尝试了问题中建议的解决方案:Setting (mocking) request headers for Flask app unit test
但它似乎并没有改变任何东西。我只是在没有部署的本地主机上运行烧瓶服务器,即基本上只是:
app = Flask(__name__)
app.run(host='127.0.0.1',port=5000,debug=True)
解决方案
测试客户端有一个environ_base
属性,用于设置在构建每个请求时开始使用的默认环境。
c = app.test_client()
c.environ_base["HTTP_USER_AGENT"] = "Firefox/71.0"
为了更好地控制传递给每个请求的内容,FlaskClient
可以将其子类化为 override open
:
class CustomClient(FlaskClient):
def open(self, *args, **kwargs):
headers = kwargs.setdefault("headers", {})
headers.setdefault("User-Agent", "Firefox/71.0")
return super().open(*args, **kwargs)
app.test_client_class = CustomClient
c = app.test_client()
assert c.get("/browser") == "firefox"
同样,Flask
可以子类化以覆盖test_request_context
:
class CustomFlask(Flask):
def test_request_context(self, *args, **kwargs):
headers = kwargs.setdefault("headers", {})
headers.setdefault("User-Agent", "Firefox/71.0")
return super().test_request_context(*args, **kwargs)
app = CustomFlask()
with app.test_request_client():
assert browser() == "firefox"
不要为所有测试全局推送测试请求上下文。应该为每个请求创建一个新的上下文,并在使用client
. 在会话夹具中推送一个不会产生您想要的效果。在大多数情况下,您应该使用test_client
over test_request_context
。
重新考虑你为什么首先依赖用户代理。将应用程序行为基于用户代理通常是糟糕的设计,因为浏览器会误报它们,并且功能会随着时间而改变。
推荐阅读
- python - 使用 Python 从字典 webapi 访问 JSON 字段
- python - 从文本文件中的行创建字符串列表的最pythonic方法?
- flutter - AlertDialog 函数在 Flutter 中的每一页上运行
- yii2 - Yii2 注销方法不允许 (#405)
- node.js - return res.status(401).json({ msg: "Auth Failed" });
- python - 在 Python Dataframe 中,如何根据每个唯一 ID 的日期匹配第一个事件并捕获该特定事件?
- python-3.x - 烧瓶错误:从应用程序导入视图 ImportError:无法导入名称“视图”
- python - 如何将第一列定义为 pd.read_csv 中的名称
- django - 赋值前引用的错误局部变量“实例”是什么意思
- ios - 如何修复滚动时从 UICollectionViewCell 中消失的图像