Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 58 additions & 35 deletions tests/asyncio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,11 @@ def remove_accept_header(self, request, response):
with self.assertRaises(InvalidHandshake) as raised:
async with connect(get_uri(server) + "/no-op", close_timeout=MS):
self.fail("did not raise")
self.assertEqual(
str(raised.exception),
"missing Sec-WebSocket-Accept header",
)

self.assertEqual(
str(raised.exception),
"missing Sec-WebSocket-Accept header",
)

async def test_timeout_during_handshake(self):
"""Client times out before receiving handshake response from server."""
Expand All @@ -461,10 +462,11 @@ async def test_timeout_during_handshake(self):
with self.assertRaises(TimeoutError) as raised:
async with connect(f"ws://{host}:{port}", open_timeout=MS):
self.fail("did not raise")
self.assertEqual(
str(raised.exception),
"timed out during opening handshake",
)

self.assertEqual(
str(raised.exception),
"timed out during opening handshake",
)

async def test_connection_closed_during_handshake(self):
"""Client reads EOF before receiving handshake response from server."""
Expand All @@ -476,15 +478,16 @@ def close_connection(self, request):
with self.assertRaises(InvalidMessage) as raised:
async with connect(get_uri(server)):
self.fail("did not raise")
self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response",
)
self.assertIsInstance(raised.exception.__cause__, EOFError)
self.assertEqual(
str(raised.exception.__cause__),
"connection closed while reading HTTP status line",
)

self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response",
)
self.assertIsInstance(raised.exception.__cause__, EOFError)
self.assertEqual(
str(raised.exception.__cause__),
"connection closed while reading HTTP status line",
)

async def test_http_response(self):
"""Client reads HTTP response."""
Expand Down Expand Up @@ -562,16 +565,16 @@ async def junk(reader, writer):
with self.assertRaises(InvalidMessage) as raised:
async with connect(f"ws://{host}:{port}"):
self.fail("did not raise")
self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response",
)
self.assertIsInstance(raised.exception.__cause__, ValueError)
self.assertEqual(
str(raised.exception.__cause__),
"unsupported protocol; expected HTTP/1.1: "
"220 smtp.invalid ESMTP Postfix",
)

self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response",
)
self.assertIsInstance(raised.exception.__cause__, ValueError)
self.assertEqual(
str(raised.exception.__cause__),
"unsupported protocol; expected HTTP/1.1: 220 smtp.invalid ESMTP Postfix",
)


class SecureClientTests(unittest.IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -619,10 +622,11 @@ async def test_reject_invalid_server_certificate(self):
# The test certificate is self-signed.
async with connect(get_uri(server)):
self.fail("did not raise")
self.assertIn(
"self signed certificate",
str(raised.exception).replace("-", " "),
)

self.assertIn(
"self signed certificate",
str(raised.exception).replace("-", " "),
)

async def test_reject_invalid_server_hostname(self):
"""Client rejects certificate where server hostname doesn't match."""
Expand All @@ -633,10 +637,11 @@ async def test_reject_invalid_server_hostname(self):
get_uri(server), ssl=CLIENT_CONTEXT, server_hostname="invalid"
):
self.fail("did not raise")
self.assertIn(
"Hostname mismatch",
str(raised.exception),
)

self.assertIn(
"Hostname mismatch",
str(raised.exception),
)

async def test_cross_origin_redirect(self):
"""Client follows redirect to a secure URI on a different origin."""
Expand Down Expand Up @@ -717,6 +722,7 @@ async def test_authenticated_socks_proxy_error(self):
self.fail("did not raise")
finally:
self.proxy_options.update(proxyauth=None)

self.assertEqual(
str(raised.exception),
"failed to connect to SOCKS proxy",
Expand Down Expand Up @@ -745,6 +751,7 @@ async def test_socks_proxy_connection_timeout(self):
with self.assertRaises(TimeoutError) as raised:
async with connect("ws://example.com/", open_timeout=MS):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"timed out during opening handshake",
Expand Down Expand Up @@ -817,6 +824,7 @@ async def test_authenticated_http_proxy_error(self):
self.fail("did not raise")
finally:
self.proxy_options.update(proxyauth=None)

self.assertEqual(
str(raised.exception),
"proxy rejected connection: HTTP 407",
Expand Down Expand Up @@ -851,6 +859,7 @@ async def test_http_proxy_protocol_error(self):
self.fail("did not raise")
finally:
self.proxy_options.update(break_http_connect=False)

self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response from proxy",
Expand All @@ -867,6 +876,7 @@ async def test_http_proxy_connection_error(self):
self.fail("did not raise")
finally:
self.proxy_options.update(close_http_connect=False)

self.assertEqual(
str(raised.exception),
"did not receive a valid HTTP response from proxy",
Expand All @@ -891,6 +901,7 @@ async def test_http_proxy_connection_timeout(self):
with self.assertRaises(TimeoutError) as raised:
async with connect("ws://example.com/", open_timeout=MS):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"timed out during opening handshake",
Expand Down Expand Up @@ -948,6 +959,7 @@ async def test_https_proxy_invalid_proxy_certificate(self):
# The proxy certificate isn't trusted.
async with connect("wss://example.com/"):
self.fail("did not raise")

self.assertIn(
"unable to get local issuer certificate",
str(raised.exception),
Expand All @@ -961,6 +973,7 @@ async def test_https_proxy_invalid_server_certificate(self):
# The test certificate is self-signed.
async with connect(get_uri(server), proxy_ssl=self.proxy_context):
self.fail("did not raise")

self.assertIn(
"self signed certificate",
str(raised.exception).replace("-", " "),
Expand Down Expand Up @@ -1066,6 +1079,7 @@ async def test_ssl_without_secure_uri(self):
"""Client rejects ssl when URI isn't secure."""
with self.assertRaises(ValueError) as raised:
await connect("ws://localhost/", ssl=CLIENT_CONTEXT)

self.assertEqual(
str(raised.exception),
"ssl argument is incompatible with a ws:// URI",
Expand All @@ -1075,6 +1089,7 @@ async def test_secure_uri_without_ssl(self):
"""Client rejects ssl=None when URI is secure."""
with self.assertRaises(ValueError) as raised:
await connect("wss://localhost/", ssl=None)

self.assertEqual(
str(raised.exception),
"ssl=None is incompatible with a wss:// URI",
Expand All @@ -1088,6 +1103,7 @@ async def test_proxy_ssl_without_https_proxy(self):
proxy="http://localhost:8080",
proxy_ssl=True,
)

self.assertEqual(
str(raised.exception),
"proxy_ssl argument is incompatible with an http:// proxy",
Expand All @@ -1101,6 +1117,7 @@ async def test_https_proxy_without_proxy_ssl(self):
proxy="https://localhost:8080",
proxy_ssl=None,
)

self.assertEqual(
str(raised.exception),
"proxy_ssl=None is incompatible with an https:// proxy",
Expand All @@ -1111,6 +1128,7 @@ async def test_unsupported_proxy(self):
with self.assertRaises(InvalidProxy) as raised:
async with connect("ws://example.com/", proxy="other://localhost:51080"):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"other://localhost:51080 isn't a valid proxy: scheme other isn't supported",
Expand All @@ -1120,6 +1138,7 @@ async def test_unix_without_path_or_sock(self):
"""Unix client requires path when sock isn't provided."""
with self.assertRaises(ValueError) as raised:
await unix_connect()

self.assertEqual(
str(raised.exception),
"no path and sock were specified",
Expand All @@ -1131,6 +1150,7 @@ async def test_unix_with_path_and_sock(self):
self.addCleanup(sock.close)
with self.assertRaises(ValueError) as raised:
await unix_connect(path="/", sock=sock)

self.assertEqual(
str(raised.exception),
"path and sock can not be specified at the same time",
Expand All @@ -1140,6 +1160,7 @@ async def test_invalid_subprotocol(self):
"""Client rejects single value of subprotocols."""
with self.assertRaises(TypeError) as raised:
await connect("ws://localhost/", subprotocols="chat")

self.assertEqual(
str(raised.exception),
"subprotocols must be a list, not a str",
Expand All @@ -1149,6 +1170,7 @@ async def test_unsupported_compression(self):
"""Client rejects incorrect value of compression."""
with self.assertRaises(ValueError) as raised:
await connect("ws://localhost/", compression=False)

self.assertEqual(
str(raised.exception),
"unsupported compression: False",
Expand All @@ -1162,6 +1184,7 @@ async def test_reentrancy(self):
with self.assertRaises(RuntimeError) as raised:
async with connecter:
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"connect() isn't reentrant",
Expand Down
5 changes: 5 additions & 0 deletions tests/asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ async def test_recv_during_recv(self):
await self.connection.recv()
finally:
recv_task.cancel()

self.assertEqual(
str(raised.exception),
"cannot call recv while another coroutine "
Expand All @@ -245,6 +246,7 @@ async def test_recv_during_recv_streaming(self):
await self.connection.recv()
finally:
recv_streaming_task.cancel()

self.assertEqual(
str(raised.exception),
"cannot call recv while another coroutine "
Expand Down Expand Up @@ -367,6 +369,7 @@ async def test_recv_streaming_during_recv(self):
self.fail("did not raise")
finally:
recv_task.cancel()

self.assertEqual(
str(raised.exception),
"cannot call recv_streaming while another coroutine "
Expand All @@ -385,6 +388,7 @@ async def test_recv_streaming_during_recv_streaming(self):
self.fail("did not raise")
finally:
recv_streaming_task.cancel()

self.assertEqual(
str(raised.exception),
"cannot call recv_streaming while another coroutine "
Expand Down Expand Up @@ -1006,6 +1010,7 @@ async def test_ping_duplicate_payload(self):

with self.assertRaises(ConcurrencyError) as raised:
await self.connection.ping("idem")

self.assertEqual(
str(raised.exception),
"already waiting for a pong with the same data",
Expand Down
3 changes: 3 additions & 0 deletions tests/asyncio/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def test_not_found(self):
with self.assertRaises(InvalidStatus) as raised:
async with connect(get_uri(server) + "/n"):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"server rejected WebSocket connection: HTTP 404",
Expand Down Expand Up @@ -146,6 +147,7 @@ def process_request(ws, request):
with self.assertRaises(InvalidStatus) as raised:
async with connect(get_uri(server) + "/"):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"server rejected WebSocket connection: HTTP 403",
Expand All @@ -163,6 +165,7 @@ async def process_request(ws, request):
with self.assertRaises(InvalidStatus) as raised:
async with connect(get_uri(server) + "/"):
self.fail("did not raise")

self.assertEqual(
str(raised.exception),
"server rejected WebSocket connection: HTTP 403",
Expand Down
Loading
Loading