Flask 重定向 Returns 200 而不是 302
Flask Redirect Returns 200 Instead of 302
我正在为 Flask 应用程序编写单元测试,我不确定为什么响应返回状态代码 200 而不是 302。我正在测试的视图正在使用重定向并且我的 post 有 follow_redirects=False
设置.
下面是测试和查看代码:
#test_views.py
def test_can_post_new_engagement(self):
response = self.client.post(
url_for('main.new_engagement'),
data={
'title': 'Sample Title',
'client': 1},
follow_redirects=False)
self.assertEqual(response.status_code, 302)
#views.py
@main.route('/engagement/new', methods=['GET', 'POST'])
def new_engagement():
form = EngagementForm()
form.client.choices = [(o.id, o.official_name) for o in
Organization.query.order_by('official_name')]
form.client.choices.insert(0, (0, '--- Select Organization ---'))
if form.validate_on_submit():
eng = Engagement(title=form.title.data,
client_id=form.client.data)
db.session.add(eng)
return redirect(url_for('main.new_pentest'))
return render_template('engagement_form.html', form=form)
错误:
FAIL: test_can_post_new_engagement (test_views.NewEngagementView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ramces/dev/global_engagement_manager/tests/unit/test_views.py
", line 36, in test_can_post_new_engagement
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
flask 重定向函数 执行 return 代码 302
的响应
from flask import redirect
print(redirect("some_url").status_code) # 302
您的测试可能失败,因为您的表单验证失败。
你能确定你确实进入了if分支吗?
一个问题可能是您的 client
SelectField
(假设您使用的是 wtforms
)使用 int
作为键。确保在字段的构造函数中提供 coerce=int
。
我用的是flask版本0.10.1
注意:这应该是一条评论,但 SO 告诉我在评论之前我需要 50 个代表。但是发布答案当然没问题。
分辨率
首先,正如 Wombatz 所建议的,我没有意识到我的表格没有通过 form.validate_on_submit()
。
经过一些调试后,我意识到我的测试配置中缺少 CSRF 令牌,因此添加 WTF_CSRF_ENABLED = False
解决了这个问题。
我正在为 Flask 应用程序编写单元测试,我不确定为什么响应返回状态代码 200 而不是 302。我正在测试的视图正在使用重定向并且我的 post 有 follow_redirects=False
设置.
下面是测试和查看代码:
#test_views.py
def test_can_post_new_engagement(self):
response = self.client.post(
url_for('main.new_engagement'),
data={
'title': 'Sample Title',
'client': 1},
follow_redirects=False)
self.assertEqual(response.status_code, 302)
#views.py
@main.route('/engagement/new', methods=['GET', 'POST'])
def new_engagement():
form = EngagementForm()
form.client.choices = [(o.id, o.official_name) for o in
Organization.query.order_by('official_name')]
form.client.choices.insert(0, (0, '--- Select Organization ---'))
if form.validate_on_submit():
eng = Engagement(title=form.title.data,
client_id=form.client.data)
db.session.add(eng)
return redirect(url_for('main.new_pentest'))
return render_template('engagement_form.html', form=form)
错误:
FAIL: test_can_post_new_engagement (test_views.NewEngagementView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ramces/dev/global_engagement_manager/tests/unit/test_views.py
", line 36, in test_can_post_new_engagement
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
flask 重定向函数 执行 return 代码 302
from flask import redirect
print(redirect("some_url").status_code) # 302
您的测试可能失败,因为您的表单验证失败。 你能确定你确实进入了if分支吗?
一个问题可能是您的 client
SelectField
(假设您使用的是 wtforms
)使用 int
作为键。确保在字段的构造函数中提供 coerce=int
。
我用的是flask版本0.10.1
注意:这应该是一条评论,但 SO 告诉我在评论之前我需要 50 个代表。但是发布答案当然没问题。
分辨率
首先,正如 Wombatz 所建议的,我没有意识到我的表格没有通过 form.validate_on_submit()
。
经过一些调试后,我意识到我的测试配置中缺少 CSRF 令牌,因此添加 WTF_CSRF_ENABLED = False
解决了这个问题。