Skip to content

Commit

Permalink
Merge pull request #334 from gittip/response-chokes-on-non-ascii
Browse files Browse the repository at this point in the history
Response chokes on non ascii
  • Loading branch information
pjz committed May 1, 2014
2 parents 707fa10 + 9c170f0 commit 17254ce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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
12 changes: 12 additions & 0 deletions tests/test_unicode.py
Expand Up @@ -28,6 +28,18 @@ def test_non_ascii_bytes_work_with_encoding(harness):
""").body.strip()
assert actual == expected

def test_response_as_wsgi_does_something_sane(harness):
expected = u'א'.encode('utf8')
wsgi = harness.simple(b"""
# encoding=utf8
[------------------]
text = u'א'
[------------------]
%(text)s""")

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


# decode_raw

Expand Down

0 comments on commit 17254ce

Please sign in to comment.