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:
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. Mocker( ) register_ uri(requests_ mock.ANY, re.compile( r"^https? ://testserver/ "), real_http=True)
mocker = requests_
mocker.
@mocker APITestCase) :
class MyAPITestCase(
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`:
``` requests_ mock.Mocker) :
class Mocker(
def copy(self):
kw= self._kw,
real_ http=self. _real_http
m = Mocker(
)
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.