Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django SECURE_SSL_REDIRECT and 301 HTTP responses

In environments different from local, I set DJ_SSL_REDIRECT = True for my Django project.

But now, all my unit tests related to REST API endpoints failed when they are run in another environment (for example, in Travis CI).

What is happening is that all HTTP responses are 301 (I'm expecting 2XX or 4XX in my tests) because of the DJ_SSL_REDIRECT setting, as explained in doc :

If you set the SECURE_SSL_REDIRECT setting to True, SecurityMiddleware will permanently (HTTP 301) redirect all HTTP connections to HTTPS.

How can I deal with this in a simple way, keeping my unit tests relevant? Thanks.

Note : I'm using Django Rest Framework 3.5

like image 273
David D. Avatar asked Nov 07 '16 18:11

David D.


1 Answers

I've had a similar problem and solved it using the "secure"-attribute for the testing client post ang get functions, like

    response = self.client.post('/accounts/signup/', self.getvalues(), follow=True, secure=True)

Hope this helps!

Mikko

like image 112
Mikko P Avatar answered Oct 24 '22 09:10

Mikko P