3
3
import threading
4
4
import unittest
5
5
6
- from websockets .exceptions import InvalidHandshake
6
+ from websockets .exceptions import InvalidHandshake , InvalidURI
7
7
from websockets .extensions .permessage_deflate import PerMessageDeflate
8
8
from websockets .sync .client import *
9
9
@@ -25,29 +25,6 @@ def test_connection(self):
25
25
with run_client (server ) as client :
26
26
self .assertEqual (client .protocol .state .name , "OPEN" )
27
27
28
- def test_connection_fails (self ):
29
- """Client connects to server but the handshake fails."""
30
-
31
- def remove_accept_header (self , request , response ):
32
- del response .headers ["Sec-WebSocket-Accept" ]
33
-
34
- # The connection will be open for the server but failed for the client.
35
- # Use a connection handler that exits immediately to avoid an exception.
36
- with run_server (do_nothing , process_response = remove_accept_header ) as server :
37
- with self .assertRaises (InvalidHandshake ) as raised :
38
- with run_client (server , close_timeout = MS ):
39
- self .fail ("did not raise" )
40
- self .assertEqual (
41
- str (raised .exception ),
42
- "missing Sec-WebSocket-Accept header" ,
43
- )
44
-
45
- def test_tcp_connection_fails (self ):
46
- """Client fails to connect to server."""
47
- with self .assertRaises (OSError ):
48
- with run_client ("ws://localhost:54321" ): # invalid port
49
- self .fail ("did not raise" )
50
-
51
28
def test_existing_socket (self ):
52
29
"""Client connects using a pre-existing socket."""
53
30
with run_server () as server :
@@ -103,6 +80,35 @@ def create_connection(*args, **kwargs):
103
80
with run_client (server , create_connection = create_connection ) as client :
104
81
self .assertTrue (client .create_connection_ran )
105
82
83
+ def test_invalid_uri (self ):
84
+ """Client receives an invalid URI."""
85
+ with self .assertRaises (InvalidURI ):
86
+ with run_client ("http://localhost" ): # invalid scheme
87
+ self .fail ("did not raise" )
88
+
89
+ def test_tcp_connection_fails (self ):
90
+ """Client fails to connect to server."""
91
+ with self .assertRaises (OSError ):
92
+ with run_client ("ws://localhost:54321" ): # invalid port
93
+ self .fail ("did not raise" )
94
+
95
+ def test_handshake_fails (self ):
96
+ """Client connects to server but the handshake fails."""
97
+
98
+ def remove_accept_header (self , request , response ):
99
+ del response .headers ["Sec-WebSocket-Accept" ]
100
+
101
+ # The connection will be open for the server but failed for the client.
102
+ # Use a connection handler that exits immediately to avoid an exception.
103
+ with run_server (do_nothing , process_response = remove_accept_header ) as server :
104
+ with self .assertRaises (InvalidHandshake ) as raised :
105
+ with run_client (server , close_timeout = MS ):
106
+ self .fail ("did not raise" )
107
+ self .assertEqual (
108
+ str (raised .exception ),
109
+ "missing Sec-WebSocket-Accept header" ,
110
+ )
111
+
106
112
def test_timeout_during_handshake (self ):
107
113
"""Client times out before receiving handshake response from server."""
108
114
gate = threading .Event ()
@@ -115,10 +121,7 @@ def stall_connection(self, request):
115
121
with run_server (do_nothing , process_request = stall_connection ) as server :
116
122
try :
117
123
with self .assertRaises (TimeoutError ) as raised :
118
- # While it shouldn't take 50ms to open a connection, this
119
- # test becomes flaky in CI when setting a smaller timeout,
120
- # even after increasing WEBSOCKETS_TESTS_TIMEOUT_FACTOR.
121
- with run_client (server , open_timeout = 5 * MS ):
124
+ with run_client (server , open_timeout = 2 * MS ):
122
125
self .fail ("did not raise" )
123
126
self .assertEqual (
124
127
str (raised .exception ),
0 commit comments