Skip to content

Commit bf89ecc

Browse files
authored
Fix sanic cookies headers serialization (#2194)
* 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. * tests/sanic: use a different way of setting cookies on older versions * Skip sanic newest on python 3.8 Because it fails with: /lib/python3.8/site-packages/sanic/helpers.py:21: in <module> STATUS_CODES: dict[int, bytes] = { E TypeError: 'type' object is not subscriptabl
1 parent 80d167f commit bf89ecc

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

.ci/.matrix_exclude.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ exclude:
217217
FRAMEWORK: sanic-20.12
218218
- VERSION: python-3.6
219219
FRAMEWORK: sanic-newest
220+
- VERSION: python-3.8
221+
FRAMEWORK: sanic-newest
220222
- VERSION: pypy-3
221223
# aioredis
222224
FRAMEWORK: aioredis-newest

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/fixtures.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ async def custom_headers(request):
158158
@app.get("/add-cookies")
159159
async def add_cookies(request):
160160
response = json({"data": "message"}, headers={"sessionid": 1234555})
161-
response.add_cookie("some", "cookie")
161+
if hasattr(response, "add_cookie"):
162+
response.add_cookie("some", "cookie")
163+
else:
164+
response.cookies["some"] = "cookie"
162165
return response
163166

164167
try:

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)