Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #20169 -- Ensured that the WSGI request's path is correctly bas…

…ed 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.
  • Loading branch information...
commit 2f81a0ca6543f4f7b59bf6cd9aeb8ae87f1e968e 1 parent 8c41bd9
@jphalip jphalip authored
Showing with 39 additions and 1 deletion.
  1. +1 −1  django/core/handlers/wsgi.py
  2. +38 −0 tests/requests/tests.py
View
2  django/core/handlers/wsgi.py
@@ -137,7 +137,7 @@ def __init__(self, environ):
path_info = '/'
self.environ = environ
self.path_info = path_info
- self.path = '%s%s' % (script_name, path_info)
+ self.path = '%s/%s' % (script_name.rstrip('/'), path_info.lstrip('/'))
self.META = environ
self.META['PATH_INFO'] = path_info
self.META['SCRIPT_NAME'] = script_name
View
38 tests/requests/tests.py
@@ -50,6 +50,44 @@ def test_wsgirequest(self):
self.assertEqual(request.META['REQUEST_METHOD'], 'bogus')
self.assertEqual(request.META['SCRIPT_NAME'], '')
+ def test_wsgirequest_with_script_name(self):
+ """
+ Ensure that the request's path is correctly assembled, regardless of
+ whether or not the SCRIPT_NAME has a trailing slash.
+ Refs #20169.
+ """
+ # With trailing slash
+ request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
+ self.assertEqual(request.path, '/PREFIX/somepath/')
+ # Without trailing slash
+ request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
+ self.assertEqual(request.path, '/PREFIX/somepath/')
+
+ def test_wsgirequest_with_force_script_name(self):
+ """
+ Ensure that the FORCE_SCRIPT_NAME setting takes precedence over the
+ request's SCRIPT_NAME environment parameter.
+ Refs #20169.
+ """
+ with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX/'):
+ request = WSGIRequest({'PATH_INFO': '/somepath/', 'SCRIPT_NAME': '/PREFIX/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
+ self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
+
+ def test_wsgirequest_path_with_force_script_name_trailing_slash(self):
+ """
+ Ensure that the request's path is correctly assembled, regardless of
+ whether or not the FORCE_SCRIPT_NAME setting has a trailing slash.
+ Refs #20169.
+ """
+ # With trailing slash
+ with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX/'):
+ request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
+ self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
+ # Without trailing slash
+ with override_settings(FORCE_SCRIPT_NAME='/FORCED_PREFIX'):
+ request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
+ self.assertEqual(request.path, '/FORCED_PREFIX/somepath/')
+
def test_wsgirequest_repr(self):
request = WSGIRequest({'PATH_INFO': '/somepath/', 'REQUEST_METHOD': 'get', 'wsgi.input': BytesIO(b'')})
request.GET = {'get-key': 'get-value'}
Please sign in to comment.
Something went wrong with that request. Please try again.