From 0bc032c60218f8ea524d42a6de3732b34416bff4 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 13 Jan 2025 16:37:57 +0100 Subject: [PATCH 1/3] contrib/sanic: fix the format of set-cookie header stored in context We are getting a Cookie instance from Sanic that by default get translate to a dict that is not a proper type for an header. Instead convert it to its string represantation. --- elasticapm/contrib/sanic/utils.py | 11 +++++++++-- tests/contrib/sanic/sanic_tests.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) 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/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": "/"}} From 70d314546df2151313beb56e99042831b701a075 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 13 Jan 2025 16:15:52 +0100 Subject: [PATCH 2/3] tests/sanic: use a different way of setting cookies on older versions --- tests/contrib/sanic/fixtures.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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: From 593cad7d44a922ca4cca1c5d6c314cc031ebe3d4 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 14 Jan 2025 09:53:43 +0100 Subject: [PATCH 3/3] Skip sanic newest on python 3.8 Because it fails with: /lib/python3.8/site-packages/sanic/helpers.py:21: in STATUS_CODES: dict[int, bytes] = { E TypeError: 'type' object is not subscriptabl --- .ci/.matrix_exclude.yml | 2 ++ 1 file changed, 2 insertions(+) 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