2016-11-04 21:14:33 |
brosner |
description |
I am trying to use django-rest-framework's RequestsClient with requests_mock. This has taken me down quite the rat hole. After reading a ton of the requests_mock code, I've concluded that the best way for me to do this is to essentially exclude the requests being made to http://testserver.
To do this, I create my own mocker instance before decorating my classes:
```
mocker = requests_mocker.Mocker()
mocker.register_uri(requests_mock.ANY, re.compile(r"^https?://testserver/"), real_http=True)
@mocker
class MyAPITestCase(APITestCase):
client_class = RequestsClient
...
```
However, this does not work. After digging around, I discovered this is due to Mocker.decorate_class making a copy of itself, but failing to include the adaptor on the copy.
A quick fix for my code is to override `copy`:
```
class Mocker(requests_mock.Mocker):
def copy(self):
m = Mocker(
kw=self._kw,
real_http=self._real_http
)
m._adapter = self._adapter
return m
```
Everything works as expected. I'd love to submit a patch, but I don't have a ton of time to work through all the contribution guidelines for the OpenStack project. |
I am trying to use django-rest-framework's RequestsClient with requests_mock. This has taken me down quite the rat hole. After reading a ton of the requests_mock code, I've concluded that the best way for me to do this is to essentially exclude the requests being made to http://testserver.
To do this, I create my own mocker instance before decorating my classes:
mocker = requests_mocker.Mocker()
mocker.register_uri(requests_mock.ANY, re.compile(r"^https?://testserver/"), real_http=True)
@mocker
class MyAPITestCase(APITestCase):
client_class = RequestsClient
...
However, this does not work. After digging around, I discovered this is due to Mocker.decorate_class making a copy of itself, but failing to include the adaptor on the copy.
A quick fix for my code is to override `copy`:
class Mocker(requests_mock.Mocker):
def copy(self):
m = Mocker(
kw=self._kw,
real_http=self._real_http
)
m._adapter = self._adapter
return m
Everything works as expected. I'd love to submit a patch, but I don't have a ton of time to work through all the contribution guidelines for the OpenStack project. |
|