Skip to content

Commit 2b34966

Browse files
authored
Merge pull request #316 from samrussell/handle_unicode_body
Fix bug in handling unicode body
2 parents 8f8bc6d + 854b3df commit 2b34966

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

responses.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,10 @@ def __init__(
345345
content_type = "application/json"
346346

347347
if content_type is UNSET:
348-
content_type = "text/plain"
349-
350-
# body must be bytes
351-
if isinstance(body, six.text_type):
352-
body = body.encode("utf-8")
348+
if isinstance(body, six.text_type) and _has_unicode(body):
349+
content_type = "text/plain; charset=utf-8"
350+
else:
351+
content_type = "text/plain"
353352

354353
self.body = body
355354
self.status = status

test_responses.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,21 @@ def run():
924924
assert_reset()
925925

926926

927+
def test_handles_unicode_body():
928+
url = "http://example.com/test"
929+
930+
@responses.activate
931+
def run():
932+
responses.add(responses.GET, url, body="михољско лето")
933+
934+
resp = requests.get(url)
935+
936+
assert_response(resp, "михољско лето", content_type="text/plain; charset=utf-8")
937+
938+
run()
939+
assert_reset()
940+
941+
927942
def test_handles_buffered_reader_body():
928943
url = "http://example.com/test"
929944

0 commit comments

Comments
 (0)