Ticket #18314: TICKET-18314.diff

File TICKET-18314.diff, 5.5 KB (added by Michael Angeletti, 12 years ago)

Patch to fix bug, updated to remove basestring.format() usage

  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index bf848de..79e5043 100644
    a b class HttpRequest(object):  
    205205
    206206    def build_absolute_uri(self, location=None):
    207207        """
    208         Builds an absolute URI from the location and the variables available in
    209         this request. If no location is specified, the absolute URI is built on
    210         ``request.get_full_path()``.
     208        Builds an absolute URI, using location and/or variables available in
     209        the request, and returns it. If ``location`` is provided and is
     210        absolute, it is simply converted to an RFC 3987 compliant URI and
     211        returned. If ``location`` is provided but is relative or is
     212        scheme-relative (i.e., //example.com/), it is urljoined to a base URL
     213        constructed from the request variables.
    211214        """
    212         if not location:
    213             location = self.get_full_path()
    214         if not absolute_http_url_re.match(location):
    215             current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
    216                                          self.get_host(), self.path)
    217             location = urljoin(current_uri, location)
     215        if location is None:
     216            # Build the URI using ``self.get_full_path()``
     217            location = '%(scheme)s//%(host)s%(path)s' % {
     218                'scheme': 'https:' if self.is_secure() else 'http:',
     219                'host': self.get_host(),
     220                'path': self.get_full_path()
     221            }
     222        elif not absolute_http_url_re.match(location):
     223            # The location was explicitly provided, but isn't absolute
     224            base_url = '%(scheme)s//%(host)s%(path)s' % {
     225                'scheme': 'https:' if self.is_secure() else 'http:',
     226                'host': self.get_host(),
     227                'path': self.path
     228            }
     229            # Join the constructed URL with the provided location, which will
     230            # allow the provided ``location`` to apply query strings to the
     231            # base path as well as override the host, if it begins with //
     232            location = urljoin(base_url, location)
     233
    218234        return iri_to_uri(location)
    219235
    220236    def _is_secure(self):
    def str_to_unicode(s, encoding):  
    750766        return unicode(s, encoding, 'replace')
    751767    else:
    752768        return s
    753 
  • new file tests/regressiontests/http/__init__.py

    diff --git a/tests/regressiontests/http/__init__.py b/tests/regressiontests/http/__init__.py
    new file mode 100644
    index 0000000..8b13789
    - +  
     1
  • new file tests/regressiontests/http/models.py

    diff --git a/tests/regressiontests/http/models.py b/tests/regressiontests/http/models.py
    new file mode 100644
    index 0000000..8b13789
    - +  
     1
  • new file tests/regressiontests/http/tests.py

    diff --git a/tests/regressiontests/http/tests.py b/tests/regressiontests/http/tests.py
    new file mode 100644
    index 0000000..2c62458
    - +  
     1from django.utils import unittest
     2from django.test.client import RequestFactory
     3
     4
     5class HttpRequestTestCase(unittest.TestCase):
     6    """
     7    Regression tests for ticket # 18314.
     8    """
     9    def setUp(self):
     10        """
     11        Attaches a request factory.
     12        """
     13        self.factory = RequestFactory()
     14
     15    def test_build_absolute_uri_no_location(self):
     16        """
     17        Ensures that ``request.build_absolute_uri()`` returns the proper value
     18        when the ``location`` argument is not provided, and ``request.path``
     19        begins with //.
     20        """
     21        # //// is needed to create a request with a path beginning with //
     22        request = self.factory.get('////django-ate-my-baby')
     23        self.assertEqual(
     24            request.build_absolute_uri(),
     25            'http://testserver//django-ate-my-baby'
     26        )
     27
     28    def test_build_absolute_uri_absolute_location(self):
     29        """
     30        Ensures that ``request.build_absolute_uri()`` returns the proper value
     31        when an absolute URL ``location`` argument is provided, and
     32        ``request.path`` begins with //.
     33        """
     34        # //// is needed to create a request with a path beginning with //
     35        request = self.factory.get('////django-ate-my-baby')
     36        self.assertEqual(
     37            request.build_absolute_uri(location='http://example.com/?foo=bar'),
     38            'http://example.com/?foo=bar'
     39        )
     40
     41    def test_build_absolute_uri_schema_relateive_location(self):
     42        """
     43        Ensures that ``request.build_absolute_uri()`` returns the proper value
     44        when a schema-relative URL ``location`` argument is provided, and
     45        ``request.path`` begins with //.
     46        """
     47        # //// is needed to create a request with a path beginning with //
     48        request = self.factory.get('////django-ate-my-baby')
     49        self.assertEqual(
     50            request.build_absolute_uri(location='//example.com/?foo=bar'),
     51            'http://example.com/?foo=bar'
     52        )
     53
     54    def test_build_absolute_uri_relative_location(self):
     55        """
     56        Ensures that ``request.build_absolute_uri()`` returns the proper value
     57        when a relative URL ``location`` argument is provided, and
     58        ``request.path`` begins with //.
     59        """
     60        # //// is needed to create a request with a path beginning with //
     61        request = self.factory.get('////django-ate-my-baby')
     62        self.assertEqual(
     63            request.build_absolute_uri(location='/foo/bar/'),
     64            'http://testserver/foo/bar/'
     65        )
Back to Top