I am working on a project that already has hundreds of unit tests, many of which use either the built in django Client, or the django rest framework APIClient for making requests and testing responses.
Having recently implemented the necessaries to make SSL work locally, and setting the SECURE_SSL_REDIRECT
to True (trying to make our dockerised dev
and test
environments as close to production
as possible), I have come a cropper to find that so many unit tests fail, due to the (API)Clients requesting, by default, always using http, not https.
Many (most) requests look like this:
response = self.client.get(some_url)
I am aware that I could use:
response = self.client.get(some_url, secure=True)
But this does mean changing a lot of unit tests. The same is true for using follow=True
, but had the added disadvantage that this could produce some other undesired behaviour.
I cannot see a way of setting the use of secure requests as a default behaviour in the Django Client. I could make my own SecureClient (and SecureAPIClient), but I would then have to make sure that I make a new base TestCase (possibly multiple) to inherit from, and change this everywhere for all the tests - still a lot of work.
It is possible of course to monkey patch the Client, but I am reluctant to to do this as, again, it could have undesired effects that are hard to debug later.
TLDR; Is there a simple (ideally supported) way, to make all unit test requests via the django test's Client, to use SSL by default?