From 810adbcf063a29fe9292e049a668b4bf9259b910538d54c3f110af8b3e748857 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Mon, 21 Feb 2022 12:09:43 +0000 Subject: [PATCH] - add 170.patch to improve python 3.10 compatibility - reenable tests OBS-URL: https://build.opensuse.org/package/show/devel:languages:python/python-wsproto?expand=0&rev=22 --- 170.patch | 208 +++++++++++++++++++++++++++++++++++++++++ python-wsproto.changes | 6 ++ python-wsproto.spec | 7 +- 3 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 170.patch diff --git a/170.patch b/170.patch new file mode 100644 index 0000000..67ffcac --- /dev/null +++ b/170.patch @@ -0,0 +1,208 @@ +From 3e7888e59f239f66766904c6d8303ad04d8ff1a6 Mon Sep 17 00:00:00 2001 +From: pgjones +Date: Fri, 18 Feb 2022 13:32:42 +0000 +Subject: [PATCH 1/5] Fix incompatibility in tests with the latest h11 + +The latest h11 events are frozen dataclasses and hence the headers +can't be overwritten. +--- + test/test_server.py | 32 +++++++++++++------------------- + 1 file changed, 13 insertions(+), 19 deletions(-) + +Index: wsproto-1.0.0/test/test_server.py +=================================================================== +--- wsproto-1.0.0.orig/test/test_server.py ++++ wsproto-1.0.0/test/test_server.py +@@ -5,13 +5,7 @@ import pytest + + from wsproto import WSConnection + from wsproto.connection import SERVER +-from wsproto.events import ( +- AcceptConnection, +- Event, +- RejectConnection, +- RejectData, +- Request, +-) ++from wsproto.events import AcceptConnection, RejectConnection, RejectData, Request + from wsproto.extensions import Extension + from wsproto.typing import Headers + from wsproto.utilities import ( +@@ -199,36 +193,30 @@ def _make_handshake( + ) + ) + event = client.next_event() +- return event, nonce ++ return cast(h11.InformationalResponse, event), nonce + + + def test_handshake() -> None: + response, nonce = _make_handshake([]) + +- response.headers = sorted(response.headers) # For test determinism +- assert response == h11.InformationalResponse( +- status_code=101, +- headers=[ +- (b"connection", b"Upgrade"), +- (b"sec-websocket-accept", generate_accept_token(nonce)), +- (b"upgrade", b"WebSocket"), +- ], +- ) ++ assert response.status_code == 101 ++ assert sorted(response.headers) == [ ++ (b"connection", b"Upgrade"), ++ (b"sec-websocket-accept", generate_accept_token(nonce)), ++ (b"upgrade", b"WebSocket"), ++ ] + + + def test_handshake_extra_headers() -> None: + response, nonce = _make_handshake([], accept_headers=[(b"X-Foo", b"bar")]) + +- response.headers = sorted(response.headers) # For test determinism +- assert response == h11.InformationalResponse( +- status_code=101, +- headers=[ +- (b"connection", b"Upgrade"), +- (b"sec-websocket-accept", generate_accept_token(nonce)), +- (b"upgrade", b"WebSocket"), +- (b"x-foo", b"bar"), +- ], +- ) ++ assert response.status_code == 101 ++ assert sorted(response.headers) == [ ++ (b"connection", b"Upgrade"), ++ (b"sec-websocket-accept", generate_accept_token(nonce)), ++ (b"upgrade", b"WebSocket"), ++ (b"x-foo", b"bar"), ++ ] + + + @pytest.mark.parametrize("accept_subprotocol", ["one", "two"]) +@@ -298,7 +286,7 @@ def test_protocol_error() -> None: + + def _make_handshake_rejection( + status_code: int, body: Optional[bytes] = None +-) -> List[Event]: ++) -> List[h11.Event]: + client = h11.Connection(h11.CLIENT) + server = WSConnection(SERVER) + nonce = generate_nonce() +@@ -333,7 +321,7 @@ def _make_handshake_rejection( + events = [] + while True: + event = client.next_event() +- events.append(event) ++ events.append(cast(h11.Event, event)) + if isinstance(event, h11.EndOfMessage): + return events + +Index: wsproto-1.0.0/src/wsproto/handshake.py +=================================================================== +--- wsproto-1.0.0.orig/src/wsproto/handshake.py ++++ wsproto-1.0.0/src/wsproto/handshake.py +@@ -119,7 +119,7 @@ class H11Handshake: + + :param bytes data: Data received from the WebSocket peer. + """ +- self._h11_connection.receive_data(data) ++ self._h11_connection.receive_data(data or b"") + while True: + try: + event = self._h11_connection.next_event() +@@ -141,7 +141,7 @@ class H11Handshake: + else: + self._events.append( + RejectConnection( +- headers=event.headers, ++ headers=list(event.headers), + status_code=event.status_code, + has_body=False, + ) +@@ -151,7 +151,7 @@ class H11Handshake: + self._state = ConnectionState.REJECTING + self._events.append( + RejectConnection( +- headers=event.headers, ++ headers=list(event.headers), + status_code=event.status_code, + has_body=True, + ) +@@ -387,7 +387,7 @@ class H11Handshake: + accept = value + continue # Skip appending to headers + elif name == b"sec-websocket-protocol": +- subprotocol = value ++ subprotocol = value.decode("ascii") + continue # Skip appending to headers + elif name == b"upgrade": + upgrade = value +@@ -408,7 +408,6 @@ class H11Handshake: + if accept != accept_token: + raise RemoteProtocolError("Bad accept token", event_hint=RejectConnection()) + if subprotocol is not None: +- subprotocol = subprotocol.decode("ascii") + if subprotocol not in self._initiating_request.subprotocols: + raise RemoteProtocolError( + f"unrecognized subprotocol {subprotocol}", +Index: wsproto-1.0.0/src/wsproto/utilities.py +=================================================================== +--- wsproto-1.0.0.orig/src/wsproto/utilities.py ++++ wsproto-1.0.0/src/wsproto/utilities.py +@@ -7,7 +7,9 @@ Utility functions that do not belong in + import base64 + import hashlib + import os +-from typing import Dict, List, Optional ++from typing import Dict, List, Optional, Union ++ ++from h11._headers import Headers as H11Headers + + from .events import Event + from .typing import Headers +@@ -51,7 +53,7 @@ class RemoteProtocolError(ProtocolError) + + + # Some convenience utilities for working with HTTP headers +-def normed_header_dict(h11_headers: Headers) -> Dict[bytes, bytes]: ++def normed_header_dict(h11_headers: Union[Headers, H11Headers]) -> Dict[bytes, bytes]: + # This mangles Set-Cookie headers. But it happens that we don't care about + # any of those, so it's OK. For every other HTTP header, if there are + # multiple instances then you're allowed to join them together with +Index: wsproto-1.0.0/test/test_client.py +=================================================================== +--- wsproto-1.0.0.orig/test/test_client.py ++++ wsproto-1.0.0/test/test_client.py +@@ -1,4 +1,4 @@ +-from typing import List, Optional ++from typing import cast, List, Optional + + import h11 + import pytest +@@ -27,7 +27,7 @@ def _make_connection_request(request: Re + client = WSConnection(CLIENT) + server = h11.Connection(h11.SERVER) + server.receive_data(client.send(request)) +- return server.next_event() ++ return cast(h11.Request, server.next_event()) + + + def test_connection_request() -> None: +@@ -114,7 +114,7 @@ def test_connection_send_state() -> None + ) + ) + ) +- headers = normed_header_dict(server.next_event().headers) ++ headers = normed_header_dict(cast(h11.Request, server.next_event()).headers) + response = h11.InformationalResponse( + status_code=101, + headers=[ +@@ -158,7 +158,7 @@ def _make_handshake( + ) + ) + ) +- request = server.next_event() ++ request = cast(h11.Request, server.next_event()) + if auto_accept_key: + full_request_headers = normed_header_dict(request.headers) + response_headers.append( diff --git a/python-wsproto.changes b/python-wsproto.changes index c3299c1..0005ca2 100644 --- a/python-wsproto.changes +++ b/python-wsproto.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Mon Feb 21 12:08:49 UTC 2022 - Dirk Müller + +- add 170.patch to improve python 3.10 compatibility +- reenable tests + ------------------------------------------------------------------- Thu Feb 17 21:00:48 UTC 2022 - Dirk Müller diff --git a/python-wsproto.spec b/python-wsproto.spec index a44dad6..140594e 100644 --- a/python-wsproto.spec +++ b/python-wsproto.spec @@ -26,6 +26,8 @@ License: MIT Group: Development/Languages/Python URL: https://pypi.python.org/pypi/wsproto Source: https://files.pythonhosted.org/packages/source/w/wsproto/wsproto-%{version}.tar.gz +# subset of https://github.com/python-hyper/wsproto/pull/170 +Patch1: 170.patch BuildRequires: %{python_module setuptools} BuildRequires: fdupes BuildRequires: python-rpm-macros @@ -59,7 +61,7 @@ RFC6455 and Compression Extensions for WebSocket via RFC7692 are fully supported. %prep -%setup -q -n wsproto-%{version} +%autosetup -p1 -n wsproto-%{version} %build %python_build @@ -69,8 +71,7 @@ RFC6455 and Compression Extensions for WebSocket via RFC7692 %python_expand %fdupes %{buildroot}%{$python_sitelib} %check -# https://github.com/python-hyper/wsproto/issues/169 -%pytest -k 'not (test_handshake or test_handshake_extra_headers)' +%pytest %files %{python_files} %doc README.rst