summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2013-11-07 01:54:50 -0600
committerMichael Merickel <michael@merickel.org>2013-11-07 01:54:50 -0600
commitfc30e8e2bac083907f0fcda6fddfd30d0d48751f (patch)
tree83a52d9f9085b206701ed5f14ee9786c0f28a890
parente967a9da69291d603a395550b0f30868a25c4ea5 (diff)
downloadpyramid-fc30e8e2bac083907f0fcda6fddfd30d0d48751f.tar.gz
pyramid-fc30e8e2bac083907f0fcda6fddfd30d0d48751f.tar.bz2
pyramid-fc30e8e2bac083907f0fcda6fddfd30d0d48751f.zip
fix mishandled anchors that need to be quoted
-rw-r--r--pyramid/config/views.py7
-rw-r--r--pyramid/tests/test_config/test_views.py11
-rw-r--r--pyramid/tests/test_url.py33
-rw-r--r--pyramid/url.py4
4 files changed, 26 insertions, 29 deletions
diff --git a/pyramid/config/views.py b/pyramid/config/views.py
index 3b768b0e9..190eefc98 100644
--- a/pyramid/config/views.py
+++ b/pyramid/config/views.py
@@ -46,7 +46,10 @@ from pyramid.compat import (
is_nonstr_iter
)
-from pyramid.encode import urlencode
+from pyramid.encode import (
+ quote_plus,
+ urlencode,
+)
from pyramid.exceptions import (
ConfigurationError,
@@ -1915,7 +1918,7 @@ class StaticURLInfo(object):
result += '?' + urlencode(query, doseq=True)
if '_anchor' in kw:
anchor = kw.pop('_anchor')
- anchor = native_(anchor, 'utf-8')
+ anchor = quote_plus(anchor)
result += '#' + anchor
return result
diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py
index 051961d25..a7e32f0c6 100644
--- a/pyramid/tests/test_config/test_views.py
+++ b/pyramid/tests/test_config/test_views.py
@@ -3820,6 +3820,17 @@ class TestStaticURLInfo(unittest.TestCase):
result = inst.generate('package:path/abc def', request, a=1)
self.assertEqual(result, 'http://example.com/abc%20def')
+ def test_generate_url_with_custom_anchor(self):
+ inst = self._makeOne()
+ registrations = [('http://example.com/', 'package:path/', None)]
+ inst._get_registrations = lambda *x: registrations
+ request = self._makeRequest()
+ uc = text_(b'La Pe\xc3\xb1a', 'utf-8')
+ result = inst.generate('package:path/abc def', request, a=1,
+ _anchor=uc)
+ self.assertEqual(result,
+ 'http://example.com/abc%20def#La+Pe%C3%B1a')
+
def test_add_already_exists(self):
inst = self._makeOne()
config = self._makeConfig(
diff --git a/pyramid/tests/test_url.py b/pyramid/tests/test_url.py
index 9cee7f61c..80dbd7001 100644
--- a/pyramid/tests/test_url.py
+++ b/pyramid/tests/test_url.py
@@ -157,15 +157,10 @@ class TestURLMethodsMixin(unittest.TestCase):
request = self._makeOne()
self._registerResourceURL(request.registry)
context = DummyContext()
- uc = text_(b'La Pe\xc3\xb1a', 'utf-8')
+ uc = text_(b'La Pe\xc3\xb1a', 'utf-8')
result = request.resource_url(context, anchor=uc)
- self.assertEqual(
- result,
- native_(
- text_(b'http://example.com:5432/context/#La Pe\xc3\xb1a',
- 'utf-8'),
- 'utf-8')
- )
+ self.assertEqual(result,
+ 'http://example.com:5432/context/#La+Pe%C3%B1a')
def test_resource_url_anchor_is_not_urlencoded(self):
request = self._makeOne()
@@ -173,7 +168,7 @@ class TestURLMethodsMixin(unittest.TestCase):
context = DummyContext()
result = request.resource_url(context, anchor=' /#')
self.assertEqual(result,
- 'http://example.com:5432/context/# /#')
+ 'http://example.com:5432/context/#+%2F%23')
def test_resource_url_no_IResourceURL_registered(self):
# falls back to ResourceURL
@@ -456,14 +451,8 @@ class TestURLMethodsMixin(unittest.TestCase):
request.registry.registerUtility(mapper, IRoutesMapper)
result = request.route_url('flub', _anchor=b"La Pe\xc3\xb1a")
- self.assertEqual(
- result,
- native_(
- text_(
- b'http://example.com:5432/1/2/3#La Pe\xc3\xb1a',
- 'utf-8'),
- 'utf-8')
- )
+ self.assertEqual(result,
+ 'http://example.com:5432/1/2/3#La+Pe%C3%B1a')
def test_route_url_with_anchor_unicode(self):
from pyramid.interfaces import IRoutesMapper
@@ -473,14 +462,8 @@ class TestURLMethodsMixin(unittest.TestCase):
anchor = text_(b'La Pe\xc3\xb1a', 'utf-8')
result = request.route_url('flub', _anchor=anchor)
- self.assertEqual(
- result,
- native_(
- text_(
- b'http://example.com:5432/1/2/3#La Pe\xc3\xb1a',
- 'utf-8'),
- 'utf-8')
- )
+ self.assertEqual(result,
+ 'http://example.com:5432/1/2/3#La+Pe%C3%B1a')
def test_route_url_with_query(self):
from pyramid.interfaces import IRoutesMapper
diff --git a/pyramid/url.py b/pyramid/url.py
index 4803283c3..eb9b364d9 100644
--- a/pyramid/url.py
+++ b/pyramid/url.py
@@ -233,7 +233,7 @@ class URLMethodsMixin(object):
if '_anchor' in kw:
anchor = kw.pop('_anchor')
- anchor = native_(anchor, 'utf-8')
+ anchor = quote_plus(anchor)
anchor = '#' + anchor
if '_app_url' in kw:
@@ -594,7 +594,7 @@ class URLMethodsMixin(object):
if 'anchor' in kw:
anchor = kw['anchor']
if isinstance(anchor, text_type):
- anchor = native_(anchor, 'utf-8')
+ anchor = quote_plus(anchor)
anchor = '#' + anchor
if elements: