Skip to content

Commit

Permalink
Fix #1204 Deprecation Warnings in Slack Bolt Integration with Sanic
Browse files Browse the repository at this point in the history
Co-authored-by: Kazuhiro Sera <[email protected]>
  • Loading branch information
HTSagara and seratch committed Dec 11, 2024
1 parent 4eb3855 commit 35a4b09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 17 additions & 11 deletions slack_bolt/adapter/sanic/async_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,25 @@ def to_sanic_response(bolt_resp: BoltResponse) -> HTTPResponse:
body=bolt_resp.body,
headers=bolt_resp.first_headers_without_set_cookie(),
)

for cookie in bolt_resp.cookies():
for name, c in cookie.items():
resp.cookies[name] = c.value
for key, c in cookie.items():
expire_value = c.get("expires")
if expire_value is not None and expire_value != "":
expire = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z")
resp.cookies[name]["expires"] = expire
resp.cookies[name]["path"] = c.get("path")
resp.cookies[name]["domain"] = c.get("domain")
if c.get("max-age") is not None and len(c.get("max-age")) > 0: # type: ignore[arg-type]
resp.cookies[name]["max-age"] = int(c.get("max-age")) # type: ignore[arg-type]
resp.cookies[name]["secure"] = True
resp.cookies[name]["httponly"] = True
expires = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z") if expire_value else None
max_age = int(c["max-age"]) if c.get("max-age") else None
path = str(c.get("path")) if c.get("path") else "/"
domain = str(c.get("domain")) if c.get("domain") else None
resp.add_cookie(
key=key,
value=c.value,
expires=expires,
path=path,
domain=domain,
max_age=max_age,
secure=True,
httponly=True,
)

return resp


Expand Down
2 changes: 2 additions & 0 deletions tests/adapter_tests_async/test_async_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ async def endpoint(req: Request):
_, response = await api.asgi_client.get(url="/slack/install")
assert response.status_code == 200
assert response.headers.get("content-type") == "text/html; charset=utf-8"
assert response.headers.get("set-cookie") is not None
assert response.headers.get("set-cookie").endswith("; Path=/; Max-Age=600; SameSite=Lax; Secure; HttpOnly") is True

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

0 comments on commit 35a4b09

Please sign in to comment.