大佬教程收集整理的这篇文章主要介绍了为什么我的 Flask 应用返回状态代码为 308 的响应正在测试中?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在对我的 Flask 应用进行单元测试。 被测代码如下:
@app.route("/my_endpoint/",methods=["GET"])
def say_Hello():
"""
Greets the user.
"""
name = request.args.get("name")
return f"Hello {name}"
测试如下:
class TestFlaskApp:
def test_R_643_11845@y_endpoint(self):
"""
Tests that my endpoint returns the result as plain text.
:return:
"""
clIEnt = app.test_clIEnt()
response = clIEnt.get("/my_endpoint?name=Peter")
assert response.status_code == status.http_200_OK
assert response.data.decode() == "Hello Peter"
错误是:
预期:200 实际:308
因此,我得到的是“永久重定向”(308)而不是“OK”(200)
如果 @app.route
以斜线结尾,您还必须在测试中使用斜线:
而不是
response = client.get("/my_endpoint?name=Peter")
使用
response = client.get("/my_endpoint/?name=Peter")
在您的单元测试中。
这是有道理的,但我花了很长时间才弄明白。
,Flask
使用 werkzeug.routIng.Rule
,默认情况下启用 Strict_slashes
,访问没有尾部斜杠的分支 URL 将重定向到附加斜杠的 URL。这会导致响应 308(永久重定向)。
因此,您的请求路径必须与路由路径完全匹配。
如果你想同时支持两条路线:
/my_endpoint
/my_endpoint/
只需使用 Strict_slashes=false
设置 app.route,如下所示:
@app.route('/my_endpoint',methods = ['POST'],Strict_slashes=falsE)
def view_func():
pass
以上是大佬教程为你收集整理的为什么我的 Flask 应用返回状态代码为 308 的响应正在测试中?全部内容,希望文章能够帮你解决为什么我的 Flask 应用返回状态代码为 308 的响应正在测试中?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。