使用 requests-mock 捕获 URL 参数
Capture URL parameter with requests-mock
我正在使用 requests-mock to mock an external service with a dynamic response。
服务的 URL 类似于 http://test/containers/test/1234,其中 1234
是我要动态生成的对象 ID。
我尝试了正则表达式匹配器,但我似乎无法在动态响应回调中获取匹配对象。
有没有办法 "capture" URL 的最后一点?
传递给回调的第一个参数将是请求。它有一个 public 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
我正在使用 requests-mock to mock an external service with a dynamic response。
服务的 URL 类似于 http://test/containers/test/1234,其中 1234
是我要动态生成的对象 ID。
我尝试了正则表达式匹配器,但我似乎无法在动态响应回调中获取匹配对象。
有没有办法 "capture" URL 的最后一点?
传递给回调的第一个参数将是请求。它有一个 public 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