Commits

Ian Lewis committed ea7639d

Fixed Django 1.5 problem in the context_processors as well.

  • Participants
  • Parent commits e8cbac3
  • Tags v1.0.2

Comments (0)

Files changed (4)

beproud/django/ssl/context_processors.py

 #:coding=utf-8:
 
-from django.http import get_host
 from django.conf import settings as django_settings
 from beproud.django.ssl.conf import settings
 
 def conf(request):
     return {
         'USE_SSL': settings.USE_SSL,
-        'HTTP_HOST': getattr(django_settings, 'HTTP_HOST', get_host(request)),
-        'SSL_HOST': getattr(django_settings, 'SSL_HOST', get_host(request)),
+        'HTTP_HOST': getattr(django_settings, 'HTTP_HOST', request.get_host()),
+        'SSL_HOST': getattr(django_settings, 'SSL_HOST', request.get_host()),
     }

beproud/django/ssl/tests/__init__.py

 from wsgi_tests import *
 from proxy_tests import *
+from context_processors_tests import *

beproud/django/ssl/tests/base.py

 
 from urlparse import urlsplit, urlunsplit
 
+import django
 from django.http import QueryDict
 from django.conf import settings as django_settings
 
 
 AVAILABLE_SETTINGS = ['SSL_REQUEST_HEADER',]
 
+def request_factory(method, path, data={}, **extra):
+    if django.VERSION >= (1,3):
+        from django.test.client import RequestFactory
+
+        return getattr(RequestFactory(), method)(
+            path=path,
+            data=data,
+            **extra
+        )
+    else:
+        import urlparse
+        import urllib
+        from django.http import SimpleCookie
+        from django.utils.http import urlencode
+        from django.core.handlers.wsgi import WSGIRequest
+        from django.test.client import FakePayload
+        try:
+            from cStringIO import StringIO
+        except ImportError:
+            from StringIO import StringIO
+
+        # This is a minimal valid WSGI environ dictionary, plus:
+        # - HTTP_COOKIE: for cookie support,
+        # - REMOTE_ADDR: often useful, see #8551.
+        # See http://www.python.org/dev/peps/pep-3333/#environ-variables
+        environ = {
+            'HTTP_COOKIE':       SimpleCookie().output(header='', sep='; '),
+            'PATH_INFO':         '/',
+            'REMOTE_ADDR':       '127.0.0.1',
+            'REQUEST_METHOD':    'GET',
+            'SCRIPT_NAME':       '',
+            'SERVER_NAME':       'testserver',
+            'SERVER_PORT':       '80',
+            'SERVER_PROTOCOL':   'HTTP/1.1',
+            'wsgi.version':      (1,0),
+            'wsgi.url_scheme':   'http',
+            'wsgi.input':        FakePayload(''),
+            'wsgi.errors':       StringIO(),
+            'wsgi.multiprocess': True,
+            'wsgi.multithread':  False,
+            'wsgi.run_once':     False,
+        }
+
+        parsed = urlparse.urlparse(path)
+        # If there are parameters, add them
+        if parsed[3]:
+            path = urllib.unquote(parsed[2] + ";" + parsed[3])
+        else:
+            path = urllib.unquote(parsed[2])
+
+        r = {
+            'CONTENT_TYPE':    'text/html; charset=utf-8',
+            'PATH_INFO':       path,
+            'QUERY_STRING':    urlencode(data, doseq=True) or parsed[4],
+            'REQUEST_METHOD':  method.upper(),
+        }
+        environ.update(r)
+        environ.update(extra)
+
+        return WSGIRequest(environ)
+
 class BaseTestCase(object):
     SSL_REQUEST_HEADER = None
     MIDDLEWARE_CLASSES = (

beproud/django/ssl/tests/context_processors_tests.py

+#:coding=utf-8:
+
+from django.test import TestCase as DjangoTestCase
+
+from beproud.django.ssl.context_processors import conf
+
+from beproud.django.ssl.tests.base import request_factory
+
+class ContextProcessorTestCase(DjangoTestCase):
+    """
+    Tests bpssl's context processors.
+    """
+    def test_conf(self):
+        request = request_factory('get', '/')
+        context = conf(request)
+        self.assertEquals(context, {
+            'USE_SSL': True,
+            'HTTP_HOST': 'testserver',
+            'SSL_HOST': 'testserver',
+        })