From 56c23d7c3028eaea12256a000ef34168f8de721f Mon Sep 17 00:00:00 2001 From: Eran Kampf Date: Mon, 19 Jun 2017 13:25:32 -0700 Subject: [PATCH] Catch the proper exception when waiting for a json response on a non-json handler I'm trying to test a simple handler that returns `text('OK')`. The `await response.json()` statement will throw a `ClientResponseError` since obviously I dont have 'application/json' in my content-type response header (see client_reqrep.py line #717). That error isn't caught which causes test to fail... --- sanic/testing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sanic/testing.py b/sanic/testing.py index 09554e21..2d74d7f1 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -1,5 +1,6 @@ import traceback from json import JSONDecodeError +from aiohttp import ClientResponseError from sanic.log import log @@ -32,7 +33,7 @@ class SanicTestClient: try: response.json = await response.json() - except (JSONDecodeError, UnicodeDecodeError): + except (ClientResponseError, JSONDecodeError, UnicodeDecodeError): response.json = None response.body = await response.read()