Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #20169 -- Ensured that the WSGI request's path is corre…

…ctly based on the `SCRIPT_NAME` environment parameter or the `FORCE_SCRIPT_NAME` setting, regardless of whether or not those have a trailing slash. Thanks to bmispelon for the review.

Backport of 2f81a0c
  • Loading branch information...
commit a15a3e9148e9707f8ab3477be512546e7cf73607 1 parent 405d620
Julien Phalip authored March 31, 2013
2  django/core/handlers/wsgi.py
@@ -140,7 +140,7 @@ def __init__(self, environ):
140 140
             path_info = '/'
141 141
         self.environ = environ
142 142
         self.path_info = path_info
143  
-        self.path = '%s%s' % (script_name, path_info)
  143
+        self.path = '%s/%s' % (script_name.rstrip('/'), path_info.lstrip('/'))
144 144
         self.META = environ
145 145
         self.META['PATH_INFO'] = path_info
146 146
         self.META['SCRIPT_NAME'] = script_name
38  tests/regressiontests/requests/tests.py
@@ -50,6 +50,44 @@ def test_wsgirequest(self):
50 50
         self.assertEqual(request.META['REQUEST_METHOD'], 'bogus')
51 51
         self.assertEqual(request.META['SCRIPT_NAME'], '')
52 52
 
  53
+    def test_wsgirequest_with_script_name(self):
  54
+        """
  55
+        Ensure that the request's path is correctly assembled, regardless of
  56
+        whether or not the SCRIPT_NAME has a trailing slash.
  57
+        Refs #20169.
  58
+        """
  59
+        # With trailing slash
  60
+        request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
  61
+        self.assertEqual(request.path, '/PREFIX/somepath/')
  62
+        # Without trailing slash
  63
+        request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
  64
+        self.assertEqual(request.path, '/PREFIX/somepath/')
  65
+
  66
+    def test_wsgirequest_with_force_script_name(self):
  67
+        """
  68
+        Ensure that the FORCE_SCRIPT_NAME setting takes precedence over the
  69
+        request's SCRIPT_NAME environment parameter.
  70
+        Refs #20169.
  71
+        """
  72
+        with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX/'):
  73
+            request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
  74
+            self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
  75
+
  76
+    def test_wsgirequest_path_with_force_script_name_trailing_slash(self):
  77
+        """
  78
+        Ensure that the request's path is correctly assembled, regardless of
  79
+        whether or not the FORCE_SCRIPT_NAME setting has a trailing slash.
  80
+        Refs #20169.
  81
+        """
  82
+        # With trailing slash
  83
+        with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX/'):
  84
+            request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
  85
+            self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
  86
+        # Without trailing slash
  87
+        with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX'):
  88
+            request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
  89
+            self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
  90
+
53 91
     def test_wsgirequest_repr(self):
54 92
         request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
55 93
         request.GET = {'get-key': 'get-value'}

0 notes on commit a15a3e9

Please sign in to comment.
Something went wrong with that request. Please try again.