diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-04-11 18:35:40 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-04-11 18:35:40 +0000 |
| commit | 77a146c26fab9594b4a401fc44f1ee5b8373bbea (patch) | |
| tree | be1270ae2f1b291760070a799d215175fa1230e7 /repoze | |
| parent | 012f0e34d6e2f3238b0e5d16d045f292579d3822 (diff) | |
| download | pyramid-77a146c26fab9594b4a401fc44f1ee5b8373bbea.tar.gz pyramid-77a146c26fab9594b4a401fc44f1ee5b8373bbea.tar.bz2 pyramid-77a146c26fab9594b4a401fc44f1ee5b8373bbea.zip | |
- The default request charset encoding is now ``utf-8``. As a result,
the request machinery will attempt to decode values from the utf-8
encoding to Unicode automatically when they are obtained via
``request.params``, ``request.GET``, and ``request.POST``. The
previous behavior of BFG was to return a bytestring when a value was
accessed in this manner. This change will break form handling code
in apps that rely on values from those APIs being considered
bytestrings. If you are manually decoding values from form
submissions in your application, you'll either need to change the
code that does that to expect Unicode values from
``request.params``, ``request.GET`` and ``request.POST``, or you'll
need to explicitly reenable the previous behavior. To reenable the
previous behavior, add the following to your application's
``configure.zcml``::
<subscriber for="repoze.bfg.interfaces.INewRequest"
handler="repoze.bfg.request.make_request_ascii"/>
See also the documentation in the "Views" chapter of the BFG docs
entitled "Using Views to Handle Form Submissions (Unicode and
Character Set Issues)".
Diffstat (limited to 'repoze')
| -rw-r--r-- | repoze/bfg/request.py | 19 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_request.py | 49 |
2 files changed, 65 insertions, 3 deletions
diff --git a/repoze/bfg/request.py b/repoze/bfg/request.py index 8d938e974..de5711e0a 100644 --- a/repoze/bfg/request.py +++ b/repoze/bfg/request.py @@ -3,6 +3,17 @@ from webob import Request as WebobRequest import repoze.bfg.interfaces +def make_request_ascii(event): + """ An event handler that causes the request charset to be ASCII; + used as an INewRequest subscriber so code written before 0.7.0 can + continue to work without a change""" + request = event.request + request.charset = None + +class Request(WebobRequest): + implements(repoze.bfg.interfaces.IRequest) + charset = 'utf-8' + # We use 'precooked' Request subclasses that correspond to HTTP # request methods within ``router.py`` when constructing a request # object rather than using ``alsoProvides`` to attach the proper @@ -13,23 +24,25 @@ import repoze.bfg.interfaces # ``HTTP_METHOD_FACTORIES`` lookup dict should be imported directly by # user code. -class Request(WebobRequest): - implements(repoze.bfg.interfaces.IRequest) - class GETRequest(WebobRequest): implements(repoze.bfg.interfaces.IGETRequest) + charset = 'utf-8' class POSTRequest(WebobRequest): implements(repoze.bfg.interfaces.IPOSTRequest) + charset = 'utf-8' class PUTRequest(WebobRequest): implements(repoze.bfg.interfaces.IPUTRequest) + charset = 'utf-8' class DELETERequest(WebobRequest): implements(repoze.bfg.interfaces.IDELETERequest) + charset = 'utf-8' class HEADRequest(WebobRequest): implements(repoze.bfg.interfaces.IHEADRequest) + charset = 'utf-8' HTTP_METHOD_FACTORIES = { 'GET':GETRequest, diff --git a/repoze/bfg/tests/test_request.py b/repoze/bfg/tests/test_request.py new file mode 100644 index 000000000..f2124d4c9 --- /dev/null +++ b/repoze/bfg/tests/test_request.py @@ -0,0 +1,49 @@ +import unittest + +class TestMakeRequestASCII(unittest.TestCase): + def _callFUT(self, event): + from repoze.bfg.request import make_request_ascii + return make_request_ascii(event) + + def test_it(self): + request = DummyRequest() + event = DummyNewRequestEvent(request) + self._callFUT(event) + self.assertEqual(request.charset, None) + +class TestSubclassedRequest(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.request import Request + return Request + + def _makeOne(self, environ): + request = self._getTargetClass()(environ) + return request + + def test_params_decoded_from_utf_8_by_default(self): + environ = { + 'PATH_INFO':'/', + 'QUERY_STRING':'la=La%20Pe%C3%B1a' + } + request = self._makeOne(environ) + self.assertEqual(request.GET['la'], u'La Pe\xf1a') + + def test_params_bystring_when_charset_None(self): + environ = { + 'PATH_INFO':'/', + 'QUERY_STRING':'la=La%20Pe%C3%B1a' + } + request = self._makeOne(environ) + request.charset = None + self.assertEqual(request.GET['la'], 'La Pe\xc3\xb1a') + +class DummyRequest: + pass + +class DummyNewRequestEvent: + def __init__(self, request): + self.request = request + + + + |
