diff --git a/sanic/testing.py b/sanic/testing.py index 6be79cd0..09554e21 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -1,4 +1,5 @@ import traceback +from json import JSONDecodeError from sanic.log import log @@ -26,9 +27,14 @@ class SanicTestClient: session, method.lower())(url, *args, **kwargs) as response: try: response.text = await response.text() - response.json = await response.json() except UnicodeDecodeError as e: response.text = None + + try: + response.json = await response.json() + except (JSONDecodeError, UnicodeDecodeError): + response.json = None + response.body = await response.read() return response