miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.31k stars 888 forks source link

Test cookie handling API changed in Werkzeug 2.3 #1982

Closed woutdenolf closed 1 year ago

woutdenolf commented 1 year ago

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: https://github.com/pallets/flask/pull/5053

Fjf commented 1 year ago

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 :)