Skip to content

Commit

Permalink
Fix encoding when body is unicode; #254
Browse files Browse the repository at this point in the history
If a Response body is a unicode we want to encode it using self.charset
when sending to the wire. For static resources the body should be a byte
string so we shouldn't need to encode it at all.
  • Loading branch information
chadwhitacre committed May 1, 2014
1 parent 4accdf2 commit 9c170f0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aspen/http/response.py
Expand Up @@ -113,7 +113,7 @@ def __call__(self, environ, start_response):
body = self.body
if isinstance(body, basestring):
body = [body]
body = (x.encode('ascii') if isinstance(x, unicode) else x for x in body)
body = (x.encode(self.charset) if isinstance(x, unicode) else x for x in body)
return CloseWrapper(self.request, body)

def __repr__(self):
Expand Down
1 change: 1 addition & 0 deletions aspen/resources/static_resource.py
Expand Up @@ -24,6 +24,7 @@ def respond(self, request, response=None):
"""
response = response or Response()
# XXX Perform HTTP caching here.
assert type(self.raw) is str # sanity check
response.body = self.raw
response.headers['Content-Type'] = self.media_type
if self.media_type.startswith('text/'):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_unicode.py
Expand Up @@ -35,8 +35,7 @@ def test_response_as_wsgi_does_something_sane(harness):
[------------------]
text = u'א'
[------------------]
%(text)s
""")
%(text)s""")

actual = b''.join(list(wsgi({}, lambda a,b: None)))
assert actual == expected
Expand Down

0 comments on commit 9c170f0

Please sign in to comment.