python - 如何在 python 中模拟烧瓶请求来测试我的程序?
问题描述
我在 python 中编写了一个函数,它将接收一个 flask.request。我想知道如何通过向它发送虚假请求来测试此功能。有没有办法在本地做到这一点?
我的功能:
def main(request: flask.Request):
if request.method == 'GET':
try:
request_json = request.get_json()
except:
return '', 400
else:
stuff = do_stuff(request_json)
return stuff, 200
解决方案
我用它在我的本地测试你的问题
import flask
app = flask.Flask(__name__)
@app.route("/<request>")
def main(request: flask.Request):
if flask.request.method == 'GET':
try:
request_json = request.get_json()
except:
return '', 400
else:
stuff = do_stuff(request_json)
return stuff, 200
if __name__ == "__main__":
app.run(debug=True)
然后卷曲
curl -i http://localhost:5000/testing
并会给出类似的输出
HTTP/1.0 400 BAD REQUEST
Content-Type: text/html; charset=utf-8
Content-Length: 0
Server: Werkzeug/2.0.1 Python/3.9.6
Date: Tue, 26 Oct 2021 16:57:19 GMT
这是预期的输出吗?
推荐阅读
- python - 用于 python 上随机图像的 Karhunen Loeve 变换(KLT)
- visual-studio-2019 - Visual Studio 2019 在空闲时在后台做什么?
- function - Powershell - 如何构建一个函数来用其他字符替换某些特殊字符
- java - 将值传递给减速器不工作的清理功能
- java - 如何将参数值传递到 JDBC 存储库 @Query
- java - 无法执行发送或更新操作,因为请求中传递的更改键与项目的当前更改键不匹配
- sql - Oracle SQL*Plus 获取每个 DISTINCT Branch_Id 值的最高 Cost_Maint 值
- server - 在大型大学安装 Moodle 的确切硬件要求是什么?
- mplab - ADC 不工作 PIC32MX795F512L MPLAB X IDE v5.20
- java - 对 StartUpWMClassName 的工具包反射访问警告