diff --git a/docs/howto/upgrade.rst b/docs/howto/upgrade.rst index db6bf11f..8cfd7b4b 100644 --- a/docs/howto/upgrade.rst +++ b/docs/howto/upgrade.rst @@ -259,9 +259,12 @@ Arguments of :func:`~asyncio.client.connect` ``extra_headers`` → ``additional_headers`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -If you're adding headers to the handshake request sent by -:func:`~legacy.client.connect` with the ``extra_headers`` argument, you must -rename it to ``additional_headers``. +If you're setting the ``User-Agent`` header with the ``extra_headers`` argument, +you should set it with ``user_agent_header`` instead. + +If you're adding other headers to the handshake request sent by +:func:`~legacy.client.connect` with ``extra_headers``, you must rename it to +``additional_headers``. Arguments of :func:`~asyncio.server.serve` .......................................... @@ -310,7 +313,10 @@ replace it with a ``process_request`` function or coroutine. ``extra_headers`` → ``process_response`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -If you're adding headers to the handshake response sent by +If you're setting the ``Server`` header with ``extra_headers``, you should set +it with the ``server_header`` argument instead. + +If you're adding other headers to the handshake response sent by :func:`~legacy.server.serve` with the ``extra_headers`` argument, you must write a ``process_response`` callable instead. diff --git a/src/websockets/asyncio/client.py b/src/websockets/asyncio/client.py index 9582a4bb..1e560fe0 100644 --- a/src/websockets/asyncio/client.py +++ b/src/websockets/asyncio/client.py @@ -89,7 +89,7 @@ async def handshake( if additional_headers is not None: self.request.headers.update(additional_headers) if user_agent_header: - self.request.headers["User-Agent"] = user_agent_header + self.request.headers.setdefault("User-Agent", user_agent_header) self.protocol.send_request(self.request) await asyncio.wait( diff --git a/src/websockets/sync/client.py b/src/websockets/sync/client.py index e2a28764..b7ab8366 100644 --- a/src/websockets/sync/client.py +++ b/src/websockets/sync/client.py @@ -86,7 +86,7 @@ def handshake( if additional_headers is not None: self.request.headers.update(additional_headers) if user_agent_header is not None: - self.request.headers["User-Agent"] = user_agent_header + self.request.headers.setdefault("User-Agent", user_agent_header) self.protocol.send_request(self.request) if not self.response_rcvd.wait(timeout): diff --git a/tests/asyncio/test_client.py b/tests/asyncio/test_client.py index bdd519fb..9c7ee46a 100644 --- a/tests/asyncio/test_client.py +++ b/tests/asyncio/test_client.py @@ -121,6 +121,14 @@ async def test_remove_user_agent(self): async with connect(get_uri(server), user_agent_header=None) as client: self.assertNotIn("User-Agent", client.request.headers) + async def test_legacy_user_agent(self): + """Client can override User-Agent header with additional_headers.""" + async with serve(*args) as server: + async with connect( + get_uri(server), additional_headers={"User-Agent": "Smith"} + ) as client: + self.assertEqual(client.request.headers["User-Agent"], "Smith") + async def test_keepalive_is_enabled(self): """Client enables keepalive and measures latency by default.""" async with serve(*args) as server: diff --git a/tests/sync/test_client.py b/tests/sync/test_client.py index dbecadca..4844d3b5 100644 --- a/tests/sync/test_client.py +++ b/tests/sync/test_client.py @@ -82,6 +82,14 @@ def test_remove_user_agent(self): with connect(get_uri(server), user_agent_header=None) as client: self.assertNotIn("User-Agent", client.request.headers) + def test_legacy_user_agent(self): + """Client can override User-Agent header with additional_headers.""" + with run_server() as server: + with connect( + get_uri(server), additional_headers={"User-Agent": "Smith"} + ) as client: + self.assertEqual(client.request.headers["User-Agent"], "Smith") + def test_keepalive_is_enabled(self): """Client enables keepalive and measures latency by default.""" with run_server() as server: