diff --git a/.ci/.matrix_exclude.yml b/.ci/.matrix_exclude.yml index 8df06914d..0349959a1 100644 --- a/.ci/.matrix_exclude.yml +++ b/.ci/.matrix_exclude.yml @@ -217,6 +217,8 @@ exclude: FRAMEWORK: sanic-20.12 - VERSION: python-3.6 FRAMEWORK: sanic-newest + - VERSION: python-3.8 + FRAMEWORK: sanic-newest - VERSION: pypy-3 # aioredis FRAMEWORK: aioredis-newest diff --git a/elasticapm/contrib/sanic/utils.py b/elasticapm/contrib/sanic/utils.py index 91cb986e3..9744bf89b 100644 --- a/elasticapm/contrib/sanic/utils.py +++ b/elasticapm/contrib/sanic/utils.py @@ -33,7 +33,7 @@ from sanic import Sanic from sanic import __version__ as version -from sanic.cookies import CookieJar +from sanic.cookies import Cookie, CookieJar from sanic.request import Request from sanic.response import HTTPResponse @@ -120,7 +120,14 @@ async def get_response_info(config: Config, response: HTTPResponse, event_type: result["status_code"] = response.status if config.capture_headers: - result["headers"] = dict(response.headers) + + def normalize(v): + # we are getting entries for Set-Cookie headers as Cookie instances + if isinstance(v, Cookie): + return str(v) + return v + + result["headers"] = {k: normalize(v) for k, v in response.headers.items()} if config.capture_body in ("all", event_type) and "octet-stream" not in response.content_type: result["body"] = response.body.decode("utf-8") diff --git a/tests/contrib/sanic/fixtures.py b/tests/contrib/sanic/fixtures.py index 0f231243c..e21bb00b9 100644 --- a/tests/contrib/sanic/fixtures.py +++ b/tests/contrib/sanic/fixtures.py @@ -158,7 +158,10 @@ async def custom_headers(request): @app.get("/add-cookies") async def add_cookies(request): response = json({"data": "message"}, headers={"sessionid": 1234555}) - response.add_cookie("some", "cookie") + if hasattr(response, "add_cookie"): + response.add_cookie("some", "cookie") + else: + response.cookies["some"] = "cookie" return response try: diff --git a/tests/contrib/sanic/sanic_tests.py b/tests/contrib/sanic/sanic_tests.py index 291ceae7c..a59d508a1 100644 --- a/tests/contrib/sanic/sanic_tests.py +++ b/tests/contrib/sanic/sanic_tests.py @@ -199,6 +199,7 @@ def test_cookies_normalization(sanic_elastic_app, elasticapm_client): _, resp = sanic_app.test_client.get( "/add-cookies", ) + assert resp.status_code == 200 assert len(apm._client.events[constants.TRANSACTION]) == 1 transaction = apm._client.events[constants.TRANSACTION][0] assert transaction["context"]["response"]["cookies"] == {"some": {"value": "cookie", "path": "/"}}