Skip to content

Commit

Permalink
Python 3: Encode text content in Response.iter_content().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Oct 3, 2018
1 parent dc97ad5 commit a49e763
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 2 additions & 4 deletions tools/wptserve/tests/functional/test_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,18 @@ def test_headers(self):
self.assertEqual(resp.info()["Expires"], "0")

class TestPipesWithVariousHandlers(TestUsingServer):
@pytest.mark.xfail(sys.version_info >= (3,), reason="wptserve only works on Py2")
def test_with_python_file_handler(self):
resp = self.request("/test_string.py", query="pipe=slice(null,2)")
self.assertEqual(resp.read(), "PA")
self.assertEqual(resp.read(), b"PA")

@pytest.mark.xfail(sys.version_info >= (3,), reason="wptserve only works on Py2")
def test_with_python_func_handler(self):
@wptserve.handlers.handler
def handler(request, response):
return "PASS"
route = ("GET", "/test/test_pipes_1/", handler)
self.server.router.register(*route)
resp = self.request(route[1], query="pipe=slice(null,2)")
self.assertEqual(resp.read(), "PA")
self.assertEqual(resp.read(), b"PA")

def test_with_python_func_handler_using_response_writer(self):
@wptserve.handlers.handler
Expand Down
4 changes: 3 additions & 1 deletion tools/wptserve/wptserve/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ def iter_content(self, read_file=False):
True, the entire content of the file will be returned as a string facilitating
non-streaming operations like template substitution.
"""
if isinstance(self.content, (binary_type, text_type)):
if isinstance(self.content, binary_type):
yield self.content
elif isinstance(self.content, text_type):
yield self.content.encode(self.encoding)
elif hasattr(self.content, "read"):
if read_file:
yield self.content.read()
Expand Down

0 comments on commit a49e763

Please sign in to comment.