Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported klein to run on the latest werkzeug release. #21

Merged
merged 1 commit into from
Jul 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions klein/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@

from klein.interfaces import IKleinRequest

__all__ = ["KleinResource"]
__all__ = ["KleinResource", "ensure_utf8_bytes"]


def ensure_utf8_bytes(v):
"""
Coerces a value which is either a C{unicode} or C{str} to a C{str}.
If ``v`` is a C{unicode} object it is encoded as utf-8.
"""
if isinstance(v, unicode):
v = v.encode("utf-8")
return v


class KleinResource(Resource):
Expand Down Expand Up @@ -63,9 +73,9 @@ def render(self, request):
resp = he.get_response({})

for header, value in resp.headers:
request.setHeader(header, value)
request.setHeader(ensure_utf8_bytes(header), ensure_utf8_bytes(value))

return he.get_body({})
return ensure_utf8_bytes(he.get_body({}))

# Try pretty hard to fix up prepath and postpath.
segment_count = self._app.endpoints[endpoint].segment_count
Expand Down
7 changes: 6 additions & 1 deletion klein/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from twisted.trial import unittest

from klein import Klein
from klein.resource import KleinResource
from klein.resource import KleinResource, ensure_utf8_bytes
from klein.interfaces import IKleinRequest

from twisted.internet.defer import succeed, Deferred
Expand Down Expand Up @@ -653,3 +653,8 @@ def _cb(result):
d.addCallback(_cb)
return d


def test_ensure_utf8_bytes(self):
self.assertEqual(ensure_utf8_bytes(u"abc"), "abc")
self.assertEqual(ensure_utf8_bytes(u"\u2202"), "\xe2\x88\x82")
self.assertEqual(ensure_utf8_bytes("\xe2\x88\x82"), "\xe2\x88\x82")