[英]Capture URL parameter with requests-mock
I'm using requests-mock to mock an external service with a dynamic response .我正在使用requests-mock来模拟具有动态响应的外部服务。
The service's URL is something like http://test/containers/test/1234 , where 1234
is the object id I want to dynamically generate.该服务的 URL 类似于http://test/containers/test/1234 ,其中1234
是我想要动态生成的对象 ID。
I've tried the regular expression matcher but I don't seem to be able to get the match object in the dynamic response callback.我已经尝试过正则表达式匹配器,但我似乎无法在动态响应回调中获取匹配对象。
Is there a way to "capture" that last bit of the URL?有没有办法“捕获” URL 的最后一点?
The first argument passed to your callback will be the request.传递给回调的第一个参数将是请求。 It has a public path
attribute that you can use:它有一个您可以使用的公共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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.