【问题标题】:How to call the sanic asyncio rest endpoint internally for testing purpose如何在内部调用 sanic asyncio rest 端点以进行测试
【发布时间】:2020-10-13 06:10:06
【问题描述】:

这里是一个简单的sanic例子

from sanic import Sanic
from sanic import response as res

app = Sanic(__name__)


@app.route("/")
async def test(req):
    return res.text("I\'m a teapot", status=418)


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000)

现在,这将打开我发送请求的端口 8000。取而代之的是,我可以在同一 python 代码中内部调用 test(req) 吗,例如:

from sanic import Sanic
from sanic import response as res

## generating some data
req = generate_data()

app = Sanic(__name__)


@app.route("/")
async def test(req):
    return res.text("I\'m a teapot", status=418)


if __name__ == '__main__':
    app.call(test(req))   ????????????????
    #app.run(host="0.0.0.0", port=8000)

对于测试,我可以执行 app.call(test(req)) 之类的操作,以便生成的相同“req”数据可以传递给 sanic api,而无需任何 HTTP 开销?

【问题讨论】:

    标签: python rest flask sanic


    【解决方案1】:

    答案is in the docs

    内置了两个测试客户端。同步版本支持应用程序并提交 http 请求。异步版本避免了利用 ASGI 到达内部并执行处理程序。听起来这就是你想要的。

    request, response = await app.asgi_client.put('/')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-11
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-10-02
      • 2015-02-22
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多