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

CookieJar.cookies assumes Set-Cookie header to be present #3027

Open
1 task done
xrmx opened this issue Jan 9, 2025 · 2 comments
Open
1 task done

CookieJar.cookies assumes Set-Cookie header to be present #3027

xrmx opened this issue Jan 9, 2025 · 2 comments
Labels

Comments

@xrmx
Copy link

xrmx commented Jan 9, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm updating some code to work after the removal of CookieJar.items() and while before it was safe to call it if there was no cookie header set now it's raising a KeyError. Maybe it's worthwhile to handle that and return an empty list instead.

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

Sanic CLI

Operating System

Linux

Sanic Version

24.12.0

Additional context

No response

@xrmx xrmx added the bug label Jan 9, 2025
@ahopkins
Copy link
Member

ahopkins commented Jan 9, 2025

Do you have a snippet of what you are trying to do?

@xrmx
Copy link
Author

xrmx commented Jan 10, 2025

Do you have a snippet of what you are trying to do?

This is APM code reading data out of the response

Before 24.12:

    return {k: {"value": v.value, "path": v["path"]} for k, v in cookies.items()}

After:

    try:
        return {cookie.key: {"value": cookie.value, "path": cookie.path} for cookie in cookies.cookies}
    except KeyError:
        # cookies.cookies assumes Set-Cookie header will be there
        return {}

When testing this if I don't add a call to add_cookie in the response the KeyError is raised. This is an example test handler:

        @app.get("/add-cookies")
        async def add_cookies(request):
            response = json({"data": "message"}, headers={"sessionid": 1234555})
            # without the following line I get the KeyError
            response.add_cookie("some", "cookie")
            return response

Called with the test client:

    _, resp = sanic_app.test_client.get(
        "/add-cookies",
    )

So would be nice that cookies.cookies would not raise and just return an empty dict instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants