Browse files

- Backport fix from master: ``request.static_url`` now generates URL-…

…quoted

  URLs when fed a ``path`` argument which contains characters that are
  unsuitable for URLs.  See #349 for
  more information.

- Backport from master: fix ``request.json_body`` to deal with alternate
  request charsets.

Ref: issue #349.
  • Loading branch information...
1 parent 7fe36a3 commit 28f21cd9979f30f510bd6c727fc9f7c6c90dee6d @mcdonc mcdonc committed Nov 18, 2011
Showing with 59 additions and 8 deletions.
  1. +13 −0 CHANGES.txt
  2. +2 −1 pyramid/config/views.py
  3. +1 −1 pyramid/request.py
  4. +21 −0 pyramid/tests/test_config/test_views.py
  5. +4 −4 pyramid/tests/test_request.py
  6. +1 −1 pyramid/traversal.py
  7. +17 −1 tox.ini
View
13 CHANGES.txt
@@ -18,6 +18,19 @@ Bug Fixes
and can still be used when a path is encoded. See
https://github.com/Pylons/pyramid/issues/349 for more information.
+- Backport fix from master: ``request.static_url`` now generates URL-quoted
+ URLs when fed a ``path`` argument which contains characters that are
+ unsuitable for URLs. See https://github.com/Pylons/pyramid/issues/349 for
+ more information.
+
+- Backport from master: fix ``request.json_body`` to deal with alternate
+ request charsets.
+
+Testing
+-------
+
+- Make tox use WebOb 1.1 for Python 2.5-based systems (WebOb 1.2 is 2.6+).
+
1.2.1 (2011-09-28)
==================
View
3 pyramid/config/views.py
@@ -1,6 +1,7 @@
import inspect
from urlparse import urljoin
from urlparse import urlparse
+import urllib
from zope.interface import Interface
from zope.interface import classProvides
@@ -1421,7 +1422,7 @@ def generate(self, path, request, **kw):
registry = get_current_registry()
for (url, spec, route_name) in self._get_registrations(registry):
if path.startswith(spec):
- subpath = path[len(spec):]
+ subpath = urllib.quote(path[len(spec):])
if url is None:
kw['subpath'] = subpath
return request.route_url(route_name, **kw)
View
2 pyramid/request.py
@@ -365,7 +365,7 @@ def is_response(self, ob):
@property
def json_body(self):
- return json.loads(self.body, encoding=self.charset)
+ return json.loads(self.body.decode(self.charset))
def route_request_iface(name, bases=()):
# zope.interface treats the __name__ as the __doc__ and changes __name__
View
21 pyramid/tests/test_config/test_views.py
@@ -3346,6 +3346,27 @@ def route_url(n, **kw):
result = inst.generate('package:path/abc', request, a=1)
self.assertEqual(result, 'url')
+ def test_generate_url_quoted_local(self):
+ inst = self._makeOne()
+ registrations = [(None, 'package:path/', '__viewname/')]
+ inst._get_registrations = lambda *x: registrations
+ def route_url(n, **kw):
+ self.assertEqual(n, '__viewname/')
+ self.assertEqual(kw, {'subpath':'abc%20def', 'a':1})
+ return 'url'
+ request = self._makeRequest()
+ request.route_url = route_url
+ result = inst.generate('package:path/abc def', request, a=1)
+ self.assertEqual(result, 'url')
+
+ def test_generate_url_quoted_remote(self):
+ inst = self._makeOne()
+ registrations = [('http://example.com/', 'package:path/', None)]
+ inst._get_registrations = lambda *x: registrations
+ request = self._makeRequest()
+ result = inst.generate('package:path/abc def', request, a=1)
+ self.assertEqual(result, 'http://example.com/abc%20def')
+
def test_add_already_exists(self):
inst = self._makeOne()
config = self._makeConfig(
View
8 pyramid/tests/test_request.py
@@ -246,11 +246,11 @@ def test_json_body_valid_json(self):
def test_json_body_alternate_charset(self):
from pyramid.compat import json
request = self._makeOne({'REQUEST_METHOD':'POST'})
- request.charset = 'latin-1'
- la = unicode('La Pe\xc3\xb1a', 'utf-8')
- body = json.dumps({'a':la}, encoding='latin-1')
+ inp ='/\xe6\xb5\x81\xe8\xa1\x8c\xe8\xb6\x8b\xe5\x8a\xbf'.decode('utf-8')
+ body = json.dumps({'a':inp}).decode('utf-8').encode('utf-16')
request.body = body
- self.assertEqual(request.json_body, {'a':la})
+ request.content_type = 'application/json; charset=utf-16'
+ self.assertEqual(request.json_body, {'a':inp})
def test_json_body_GET_request(self):
request = self._makeOne({'REQUEST_METHOD':'GET'})
View
2 pyramid/traversal.py
@@ -489,7 +489,7 @@ def traversal_path_info(path):
"""
try:
path = path.decode('utf-8')
- except UnicodeDecodeError as e:
+ except UnicodeDecodeError, e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
path = path.strip('/')
clean = []
View
18 tox.ini
@@ -11,9 +11,25 @@ deps =
repoze.sphinx.autointerface
virtualenv
+[testenv:py25]
+commands =
+ python setup.py test -q
+deps =
+ https://github.com/Pylons/webob/zipball/1.1-branch
+ Sphinx
+ WebTest
+ repoze.sphinx.autointerface
+ virtualenv
+
[testenv:jython]
commands =
- jython setup.py test -q
+ jython setup.py test -q
+deps =
+ https://github.com/Pylons/webob/zipball/1.1-branch
+ Sphinx
+ WebTest
+ repoze.sphinx.autointerface
+ virtualenv
[testenv:cover]
basepython =

0 comments on commit 28f21cd

Please sign in to comment.