Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HH-230768 fix cookies #739

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion frontik/frontik_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ def __init__(
body: bytes = b'',
):
self.headers = HTTPHeaders(get_default_headers()) # type: ignore
if headers is not None:

if isinstance(headers, HTTPHeaders):
for k, v in headers.get_all():
self.headers.add(k, v)
elif headers is not None:
self.headers.update(headers)

self.status_code = status_code
self.body = body
self.data_written = False
Expand Down
40 changes: 40 additions & 0 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from fastapi import Response

from frontik.app import FrontikApplication
from frontik.handler import PageHandler, get_current_handler
from frontik.routing import plain_router
from frontik.testing import FrontikTestBase


@plain_router.get('/cookies', cls=PageHandler)
async def cookies_page(handler: PageHandler = get_current_handler()) -> None:
handler.set_cookie('key1', 'val1')
handler.set_cookie('key2', 'val2')


@plain_router.get('/asgi_cookies')
async def asgi_cookies_page(response: Response) -> None:
response.set_cookie('key1', 'val1')
response.set_cookie('key2', 'val2')


class TestFrontikTesting(FrontikTestBase):
@pytest.fixture(scope='class')
def frontik_app(self) -> FrontikApplication:
return FrontikApplication()

async def test_cookies(self):
response = await self.fetch('/cookies')

assert response.status_code == 200
assert response.headers.getall('Set-Cookie') == ['key1=val1; Path=/', 'key2=val2; Path=/']

async def test_asgi_cookies(self):
response = await self.fetch('/asgi_cookies')

assert response.status_code == 200
assert response.headers.getall('Set-Cookie') == [
'key1=val1; Path=/; SameSite=lax',
'key2=val2; Path=/; SameSite=lax',
]
Loading