【发布时间】:2021-11-27 01:20:00
【问题描述】:
例如,假设您正在使用 fastapi.testclient.TestClient 来执行 GET。在定义该 GET 方法的 API 代码中,如果您获得 request.client.host,您将获得字符串“testclient”。
测试,使用pytest:
def test_success(self):
client = TestClient(app)
client.get('/my_ip')
现在,假设您的 API 代码是这样的:
@router.get('/my_ip')
def my_ip(request: Request):
return request.client.host
endpoit /my_ip 假设返回客户端 IP,但是在运行 pytest 时,它将返回“testclient”字符串。有没有办法将 TestClient 上的客户端 IP(主机)更改为“testclient”以外的其他内容?
【问题讨论】: