Hello,
I recently started using SECURE_SSL_REDIRECT = True. This however broke most tests that use SimpleTestCase.client.get("/some/relative/url/"), because the secure parameter of get() is by default False:
SimpleTestCase.client.get("/some/relative/url/", secure=False)
I fixed this problem by using a custom test client that makes HTTPS requests by default:
from django.test import Client, TestCase
class SecureTestClient(Client):
"""
A test client that uses HTTPS requests by default (secure=True).
This is required when SECURE_SSL_REDIRECT=True is used.
"""
def get(self, path, data=None, secure=True, **extra):
return super().get(path, data=data, secure=secure, **extra)
# Similar for post(), put(), ...
...
class MyTestCase(TestCase):
client_class = SecureTestClient
...
This helps with most tests, but tests like this still don’t work:
class AdminTest(TestCase):
def test_admin_login(self):
response = self.client.get("/admin/")
self.assertRedirects(response, "/admin/login/?next=/admin/")
This is because assertRedirects() loads the final page by default [docs]:
assertRedirects(response, …, fetch_redirect_response=True)
However, this fails: assertRedirects() makes an attempt [source] to use the same scheme for loading the final page as was used in request.url – which however is not "https" if request.url is a relative URL like "/some/relative/url/".
As a result, we get:
AssertionError: 301 != 200 : Couldn't retrieve redirection page '/admin/login/': response code was 301 (expected 200)
The problem is that, apart from using fetch_redirect_response=False, I can neither see a workaround for this nor a proper fix. ![]()
Any ideas?