我正在使用requests-mock通过dynamic response模拟外部服务。
服务的URL类似于http://test/containers/test/1234,其中1234
是我想要动态生成的对象id。
我尝试过正则表达式匹配器,但似乎无法在动态响应回调中获取match对象。
有没有办法“捕获”URL的最后一部分?
发布于 2019-03-01 04:14:48
传递给回调的第一个参数是请求。它有一个可以使用的公共path
属性:
>>> def callback(request, context):
... print("request path: ", request.path)
...
>>> with requests_mock.Mocker() as m:
... m.get("http://test/containers/test/1234", text=callback)
... requests.get("http://test/containers/test/1234")
...
request path: /containers/test/1234
https://stackoverflow.com/questions/54933440
复制相似问题