Django RequestFactory() secure not working

111 views
Skip to first unread message

jvc26

unread,
May 13, 2014, 8:12:46 AM5/13/14
to django...@googlegroups.com
Could anyone explain what is going wrong here:

factory = RequestFactory()
factory.post('/', secure=True).is_secure()

Surely that should be True?

J

Kelvin Wong

unread,
May 17, 2014, 5:20:11 AM5/17/14
to django...@googlegroups.com
If you print out your code you will see that you are adding a key-value to the WSGI environ:

# print factory.post('/', secure=True)

<WSGIRequest
path:/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{},
META:{
 ...
 'PATH_INFO': u'/',
 ...
 'REQUEST_METHOD': 'POST',
 ...
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'secure': True,
 ...
 'wsgi.url_scheme': 'http',
 ...
>

This is because factory.post has the following parameters:

post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)

Your secure=True gets pushed into extra and it appears in the environ as expected.

The solution for modeling TLS requests is to set the 'wsgi.url_scheme' to 'https' using an unpacked dictionary.

# obj = factory.post('/', **{'wsgi.url_scheme': 'https'})
# print obj
# print obj.is_secure()

<WSGIRequest
path:/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{},
META:{
 ...
 'PATH_INFO': u'/',
 ...
 'REQUEST_METHOD': 'POST',
 ...
 'wsgi.url_scheme': u'https',
 ...
>
True

---

See line 109


K

James Clemence

unread,
May 17, 2014, 11:43:10 AM5/17/14
to django...@googlegroups.com

Great thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/9zVxCtO7gJQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a392facb-7846-496f-aa7c-38f4ab7c50b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages