Skip to content

Commit 0bc032c

Browse files
committed
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.
1 parent 80d167f commit 0bc032c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

elasticapm/contrib/sanic/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from sanic import Sanic
3535
from sanic import __version__ as version
36-
from sanic.cookies import CookieJar
36+
from sanic.cookies import Cookie, CookieJar
3737
from sanic.request import Request
3838
from sanic.response import HTTPResponse
3939

@@ -120,7 +120,14 @@ async def get_response_info(config: Config, response: HTTPResponse, event_type:
120120
result["status_code"] = response.status
121121

122122
if config.capture_headers:
123-
result["headers"] = dict(response.headers)
123+
124+
def normalize(v):
125+
# we are getting entries for Set-Cookie headers as Cookie instances
126+
if isinstance(v, Cookie):
127+
return str(v)
128+
return v
129+
130+
result["headers"] = {k: normalize(v) for k, v in response.headers.items()}
124131

125132
if config.capture_body in ("all", event_type) and "octet-stream" not in response.content_type:
126133
result["body"] = response.body.decode("utf-8")

tests/contrib/sanic/sanic_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def test_cookies_normalization(sanic_elastic_app, elasticapm_client):
199199
_, resp = sanic_app.test_client.get(
200200
"/add-cookies",
201201
)
202+
assert resp.status_code == 200
202203
assert len(apm._client.events[constants.TRANSACTION]) == 1
203204
transaction = apm._client.events[constants.TRANSACTION][0]
204205
assert transaction["context"]["response"]["cookies"] == {"some": {"value": "cookie", "path": "/"}}

0 commit comments

Comments
 (0)