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

Test cookie handling API changed in Werkzeug 2.3 #1982

Closed
woutdenolf opened this issue Apr 29, 2023 · 1 comment
Closed

Test cookie handling API changed in Werkzeug 2.3 #1982

woutdenolf opened this issue Apr 29, 2023 · 1 comment

Comments

@woutdenolf
Copy link

API change in Werkzeug 2.3: https://werkzeug.palletsprojects.com/en/2.3.x/changes/#version-2-3-0

Refactor the test client cookie implementation. [#1060](https://github.com/pallets/werkzeug/issues/1060), [#1680](https://github.com/pallets/werkzeug/issues/1680)

    - The cookie_jar attribute is deprecated. http.cookiejar is no longer used for storage.
    - Domain and path matching is used when sending cookies in requests. The domain and path parameters default to localhost and /.
    - Added a get_cookie method to inspect cookies.
    - Cookies have decoded_key and decoded_value attributes to match what the app sees rather than the encoded values a client would see.
    - The first positional server_name parameter to set_cookie and delete_cookie is deprecated. Use the domain parameter instead.
    - Other parameters to delete_cookie besides domain, path, and value are deprecated.

Affected code in flask-socketio:

sclient = socketio.test_client(app, flask_test_client=client)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venvs\ewoks\py3.8\lib\site-packages\flask_socketio\__init__.py:729: in test_client
    return SocketIOTestClient(app, self, namespace=namespace,
venvs\ewoks\py3.8\lib\site-packages\flask_socketio\test_client.py:79: in __init__
    self.connect(namespace=namespace, query_string=query_string,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <flask_socketio.test_client.SocketIOTestClient object at 0x0000000009AE0A90>, namespace = '/', query_string = None
headers = None, auth = None

    def connect(self, namespace=None, query_string=None, headers=None,
                auth=None):
        """Connect the client.

        :param namespace: The namespace for the client. If not provided, the
                          client connects to the server on the global
                          namespace.
        :param query_string: A string with custom query string arguments.
        :param headers: A dictionary with custom HTTP headers.
        :param auth: Optional authentication data, given as a dictionary.

        Note that it is usually not necessary to explicitly call this method,
        since a connection is automatically established when an instance of
        this class is created. An example where it this method would be useful
        is when the application accepts multiple namespace connections.
        """
        url = '/socket.io'
        namespace = namespace or '/'
        if query_string:
            if query_string[0] != '?':
                query_string = '?' + query_string
            url += query_string
        environ = EnvironBuilder(url, headers=headers).get_environ()
        environ['flask.app'] = self.app
        if self.flask_test_client:
            # inject cookies from Flask
>           self.flask_test_client.cookie_jar.inject_wsgi(environ)
E           AttributeError: 'dict_values' object has no attribute 'inject_wsgi'

venvs\ewoks\py3.8\lib\site-packages\flask_socketio\test_client.py:116: AttributeError

Flask handled the changes as follows: pallets/flask#5053

@Fjf
Copy link

Fjf commented May 1, 2023

For anyone else who has the same issue, you can make a quick fix by replacing

self.flask_test_client.cookie_jar.inject_wsgi(environ)
to
environ["HTTP_COOKIE"] = "; ".join(cookie._to_request_header() for cookie in self.flask_test_client.cookie_jar)

It does the same as the original werkzeug inject_wsgi:
https://github.com/pallets/werkzeug/blob/6e2c1d15637bef6635545cbc8eaf0d161fad974a/src/werkzeug/test.py#L207-L215

This also uses hidden cookie functions, so it's not recommended :)

CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue Feb 7, 2024
 * Upgraded Werkzeug to >3.0.0
 * Werkzeug's `Response.charset` got removed.
   See pallets/werkzeug#2768
 * Upgraded Flask to >3.0.0
 * FlaskClient.cookie_jar was affected due to changes in Werkzeug
   Flask just removed CookieJar alltogether, while also checking the
   path of cookies in the tests more strictly.
   See: miguelgrinberg/Flask-SocketIO#1982
   See: pallets/flask#5053
 * Cookie values now only accept strings and not bytes.

Change-Id: Id1a8014f826254235866e86b936bb2a5c720219a
CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue Feb 7, 2024
 * Upgraded Werkzeug to >3.0.0
 * Werkzeug's `Response.charset` got removed.
   See pallets/werkzeug#2768
 * Upgraded Flask to >3.0.0
 * FlaskClient.cookie_jar was affected due to changes in Werkzeug
   Flask just removed CookieJar alltogether, while also checking the
   path of cookies in the tests more strictly.
   See: miguelgrinberg/Flask-SocketIO#1982
   See: pallets/flask#5053
 * Cookie values now only accept strings and not bytes.

Change-Id: Id1a8014f826254235866e86b936bb2a5c720219a
CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue Feb 7, 2024
 * Upgraded Werkzeug to >3.0.0
 * Werkzeug's `Response.charset` got removed.
   See pallets/werkzeug#2768
 * Upgraded Flask to >3.0.0
 * FlaskClient.cookie_jar was affected due to changes in Werkzeug
   Flask just removed CookieJar alltogether, while also checking the
   path of cookies in the tests more strictly.
   See: miguelgrinberg/Flask-SocketIO#1982
   See: pallets/flask#5053
 * Cookie values now only accept strings and not bytes.

Change-Id: Id1a8014f826254235866e86b936bb2a5c720219a
MooseyAnon added a commit to MooseyAnon/corna that referenced this issue Sep 8, 2024
As part of our python3.12 upgrade, we have also upgraded all our packages.

It seems that the werkzeug have deprecated the `cookie_jar` in the testing
client[1].

This commit fixes any tests broken but changed behaviour of cookies due to the
flask and werkzeug upgrades. Most of these changes use the simplified cookie
interface `get_cookie` (this may have always been there but was missed during
initial development).

[1] miguelgrinberg/Flask-SocketIO#1982
MooseyAnon added a commit to MooseyAnon/corna that referenced this issue Sep 14, 2024
As part of our python3.12 upgrade, we have also upgraded all our packages.

It seems that the werkzeug have deprecated the `cookie_jar` in the testing
client[1].

This commit fixes any tests broken but changed behaviour of cookies due to the
flask and werkzeug upgrades. Most of these changes use the simplified cookie
interface `get_cookie` (this may have always been there but was missed during
initial development).

[1] miguelgrinberg/Flask-SocketIO#1982
MooseyAnon added a commit to MooseyAnon/corna that referenced this issue Sep 14, 2024
As part of our python3.12 upgrade, we have also upgraded all our packages.

It seems that the werkzeug have deprecated the `cookie_jar` in the testing
client[1].

This commit fixes any tests broken but changed behaviour of cookies due to the
flask and werkzeug upgrades. Most of these changes use the simplified cookie
interface `get_cookie` (this may have always been there but was missed during
initial development).

[1] miguelgrinberg/Flask-SocketIO#1982
MooseyAnon added a commit to MooseyAnon/corna that referenced this issue Sep 14, 2024
As part of our python3.12 upgrade, we have also upgraded all our packages.

It seems that the werkzeug have deprecated the `cookie_jar` in the testing
client[1].

This commit fixes any tests broken but changed behaviour of cookies due to the
flask and werkzeug upgrades. Most of these changes use the simplified cookie
interface `get_cookie` (this may have always been there but was missed during
initial development).

[1] miguelgrinberg/Flask-SocketIO#1982
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants