1

I'm using requests-mock to mock an external service with a dynamic response.

The service's URL is something like http://test/containers/test/1234, where 1234 is the object id I want to dynamically generate.

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?

4

1 回答 1

1

传递给回调的第一个参数将是请求。它有一个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
于 2019-02-28T20:14:48.053 回答