Skip to content

Commit 35a4b09

Browse files
HTSagaraseratch
andcommitted
Fix #1204 Deprecation Warnings in Slack Bolt Integration with Sanic
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 4eb3855 commit 35a4b09

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

slack_bolt/adapter/sanic/async_handler.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@ def to_sanic_response(bolt_resp: BoltResponse) -> HTTPResponse:
2929
body=bolt_resp.body,
3030
headers=bolt_resp.first_headers_without_set_cookie(),
3131
)
32+
3233
for cookie in bolt_resp.cookies():
33-
for name, c in cookie.items():
34-
resp.cookies[name] = c.value
34+
for key, c in cookie.items():
3535
expire_value = c.get("expires")
36-
if expire_value is not None and expire_value != "":
37-
expire = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z")
38-
resp.cookies[name]["expires"] = expire
39-
resp.cookies[name]["path"] = c.get("path")
40-
resp.cookies[name]["domain"] = c.get("domain")
41-
if c.get("max-age") is not None and len(c.get("max-age")) > 0: # type: ignore[arg-type]
42-
resp.cookies[name]["max-age"] = int(c.get("max-age")) # type: ignore[arg-type]
43-
resp.cookies[name]["secure"] = True
44-
resp.cookies[name]["httponly"] = True
36+
expires = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z") if expire_value else None
37+
max_age = int(c["max-age"]) if c.get("max-age") else None
38+
path = str(c.get("path")) if c.get("path") else "/"
39+
domain = str(c.get("domain")) if c.get("domain") else None
40+
resp.add_cookie(
41+
key=key,
42+
value=c.value,
43+
expires=expires,
44+
path=path,
45+
domain=domain,
46+
max_age=max_age,
47+
secure=True,
48+
httponly=True,
49+
)
50+
4551
return resp
4652

4753

tests/adapter_tests_async/test_async_sanic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ async def endpoint(req: Request):
218218
_, response = await api.asgi_client.get(url="/slack/install")
219219
assert response.status_code == 200
220220
assert response.headers.get("content-type") == "text/html; charset=utf-8"
221+
assert response.headers.get("set-cookie") is not None
222+
assert response.headers.get("set-cookie").endswith("; Path=/; Max-Age=600; SameSite=Lax; Secure; HttpOnly") is True
221223

222224
# NOTE: Although sanic-testing 0.6 does not have this value,
223225
# Sanic apps properly generate the content-length header

0 commit comments

Comments
 (0)