11# -*- coding: utf-8 -*-
22from base64 import b64encode
33from hashlib import sha1
4- import os
54import socket
65import time
76import unittest
87
9- from mock import MagicMock , call , patch
8+ from mock import MagicMock , patch
109
11- from ws4py .manager import WebSocketManager
12- from ws4py .websocket import WebSocket
1310from ws4py import WS_KEY
1411from ws4py .exc import HandshakeError
1512from ws4py .framing import Frame , OPCODE_TEXT , OPCODE_CLOSE
16- from ws4py .messaging import CloseControlMessage
1713from ws4py .client import WebSocketBaseClient
1814from ws4py .client .threadedclient import WebSocketClient
1915
2016class BasicClientTest (unittest .TestCase ):
2117 def test_invalid_hostname_in_url (self ):
2218 self .assertRaises (ValueError , WebSocketBaseClient , url = "qsdfqsd65qsd354" )
23-
19+
2420 def test_invalid_scheme_in_url (self ):
2521 self .assertRaises (ValueError , WebSocketBaseClient , url = "ftp://localhost" )
26-
22+
2723 def test_invalid_hostname_in_url (self ):
2824 self .assertRaises (ValueError , WebSocketBaseClient , url = "ftp://?/" )
29-
25+
3026 def test_parse_unix_schemes (self ):
3127 c = WebSocketBaseClient (url = "ws+unix:///my.socket" )
3228 self .assertEqual (c .scheme , "ws+unix" )
@@ -35,95 +31,98 @@ def test_parse_unix_schemes(self):
3531 self .assertEqual (c .unix_socket_path , "/my.socket" )
3632 self .assertEqual (c .resource , "/" )
3733 self .assertEqual (c .bind_addr , "/my.socket" )
38-
34+
3935 c = WebSocketBaseClient (url = "wss+unix:///my.socket" )
4036 self .assertEqual (c .scheme , "wss+unix" )
4137 self .assertEqual (c .host , "localhost" )
4238 self .assertIsNone (c .port )
4339 self .assertEqual (c .unix_socket_path , "/my.socket" )
4440 self .assertEqual (c .resource , "/" )
4541 self .assertEqual (c .bind_addr , "/my.socket" )
46-
42+
4743 def test_parse_ws_scheme (self ):
4844 c = WebSocketBaseClient (url = "ws://127.0.0.1/" )
4945 self .assertEqual (c .scheme , "ws" )
5046 self .assertEqual (c .host , "127.0.0.1" )
5147 self .assertEqual (c .port , 80 )
5248 self .assertEqual (c .resource , "/" )
5349 self .assertEqual (c .bind_addr , ("127.0.0.1" , 80 ))
54-
50+
5551 def test_parse_ws_scheme_when_missing_resource (self ):
5652 c = WebSocketBaseClient (url = "ws://127.0.0.1" )
5753 self .assertEqual (c .scheme , "ws" )
5854 self .assertEqual (c .host , "127.0.0.1" )
5955 self .assertEqual (c .port , 80 )
6056 self .assertEqual (c .resource , "/" )
6157 self .assertEqual (c .bind_addr , ("127.0.0.1" , 80 ))
62-
58+
6359 def test_parse_ws_scheme_with_port (self ):
6460 c = WebSocketBaseClient (url = "ws://127.0.0.1:9090" )
6561 self .assertEqual (c .scheme , "ws" )
6662 self .assertEqual (c .host , "127.0.0.1" )
6763 self .assertEqual (c .port , 9090 )
6864 self .assertEqual (c .resource , "/" )
6965 self .assertEqual (c .bind_addr , ("127.0.0.1" , 9090 ))
70-
66+
7167 def test_parse_ws_scheme_with_query_string (self ):
7268 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
7369 self .assertEqual (c .scheme , "ws" )
7470 self .assertEqual (c .host , "127.0.0.1" )
7571 self .assertEqual (c .port , 80 )
7672 self .assertEqual (c .resource , "/?token=value" )
7773 self .assertEqual (c .bind_addr , ("127.0.0.1" , 80 ))
78-
74+
7975 def test_parse_wss_scheme (self ):
8076 c = WebSocketBaseClient (url = "wss://127.0.0.1/" )
8177 self .assertEqual (c .scheme , "wss" )
8278 self .assertEqual (c .host , "127.0.0.1" )
8379 self .assertEqual (c .port , 443 )
8480 self .assertEqual (c .resource , "/" )
8581 self .assertEqual (c .bind_addr , ("127.0.0.1" , 443 ))
86-
82+
8783 def test_parse_wss_scheme_when_missing_resource (self ):
8884 c = WebSocketBaseClient (url = "wss://127.0.0.1" )
8985 self .assertEqual (c .scheme , "wss" )
9086 self .assertEqual (c .host , "127.0.0.1" )
9187 self .assertEqual (c .port , 443 )
9288 self .assertEqual (c .resource , "/" )
9389 self .assertEqual (c .bind_addr , ("127.0.0.1" , 443 ))
94-
90+
9591 def test_parse_wss_scheme_with_port (self ):
9692 c = WebSocketBaseClient (url = "wss://127.0.0.1:9090" )
9793 self .assertEqual (c .scheme , "wss" )
9894 self .assertEqual (c .host , "127.0.0.1" )
9995 self .assertEqual (c .port , 9090 )
10096 self .assertEqual (c .resource , "/" )
10197 self .assertEqual (c .bind_addr , ("127.0.0.1" , 9090 ))
102-
98+
10399 def test_parse_wss_scheme_with_query_string (self ):
104100 c = WebSocketBaseClient (url = "wss://127.0.0.1/?token=value" )
105101 self .assertEqual (c .scheme , "wss" )
106102 self .assertEqual (c .host , "127.0.0.1" )
107103 self .assertEqual (c .port , 443 )
108104 self .assertEqual (c .resource , "/?token=value" )
109105 self .assertEqual (c .bind_addr , ("127.0.0.1" , 443 ))
110-
106+
111107 @patch ('ws4py.client.socket' )
112108 def test_connect_and_close (self , sock ):
113-
109+
114110 s = MagicMock ()
115111 sock .socket .return_value = s
116112 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
117113 ("127.0.0.1" , 80 , 0 , 0 ))]
118114
119115 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
120-
116+
121117 s .recv .return_value = b"\r \n " .join ([
122118 b"HTTP/1.1 101 Switching Protocols" ,
123119 b"Connection: Upgrade" ,
124120 b"Sec-Websocket-Version: 13" ,
125121 b"Content-Type: text/plain;charset=utf-8" ,
126122 b"Sec-Websocket-Accept: " + b64encode (sha1 (c .key + WS_KEY ).digest ()),
123+ b"Sec-WebSocket-Protocol: proto1, proto2" ,
124+ b"Sec-WebSocket-Extensions: ext1, ext2" ,
125+ b"Sec-WebSocket-Extensions: ext3" ,
127126 b"Upgrade: websocket" ,
128127 b"Date: Sun, 26 Jul 2015 12:32:55 GMT" ,
129128 b"Server: ws4py/test" ,
@@ -132,6 +131,8 @@ def test_connect_and_close(self, sock):
132131
133132 c .connect ()
134133 s .connect .assert_called_once_with (("127.0.0.1" , 80 ))
134+ self .assertEqual (c .protocols , [b'proto1' , b'proto2' ])
135+ self .assertEqual (c .extensions , [b'ext1' , b'ext2' , b'ext3' ])
135136
136137 s .reset_mock ()
137138 c .close (code = 1006 , reason = "boom" )
@@ -140,32 +141,32 @@ def test_connect_and_close(self, sock):
140141 f .parser .send (args [0 ][0 ])
141142 f .parser .close ()
142143 self .assertIn (b'boom' , f .unmask (f .body ))
143-
144+
144145 @patch ('ws4py.client.socket' )
145146 def test_empty_response (self , sock ):
146-
147+
147148 s = MagicMock ()
148149 sock .socket .return_value = s
149150 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
150151 ("127.0.0.1" , 80 , 0 , 0 ))]
151152
152153 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
153-
154+
154155 s .recv .return_value = b""
155156 self .assertRaises (HandshakeError , c .connect )
156157 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
157158 s .close .assert_called_once_with ()
158-
159+
159160 @patch ('ws4py.client.socket' )
160- def test_invdalid_response_code (self , sock ):
161-
161+ def test_invalid_response_code (self , sock ):
162+
162163 s = MagicMock ()
163164 sock .socket .return_value = s
164165 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
165166 ("127.0.0.1" , 80 , 0 , 0 ))]
166167
167168 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
168-
169+
169170 s .recv .return_value = b"\r \n " .join ([
170171 b"HTTP/1.1 200 Switching Protocols" ,
171172 b"Connection: Upgrade" ,
@@ -181,18 +182,18 @@ def test_invdalid_response_code(self, sock):
181182 self .assertRaises (HandshakeError , c .connect )
182183 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
183184 s .close .assert_called_once_with ()
184-
185+
185186 @patch ('ws4py.client.socket' )
186187 def test_invalid_response_headers (self , sock ):
187-
188+
188189 for key_header , invalid_value in ((b'upgrade' , b'boom' ),
189190 (b'connection' , b'bim' )):
190191 s = MagicMock ()
191192 sock .socket .return_value = s
192193 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
193194 ("127.0.0.1" , 80 , 0 , 0 ))]
194195 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
195-
196+
196197 status_line = b"HTTP/1.1 101 Switching Protocols"
197198 headers = {
198199 b"connection" : b"Upgrade" ,
@@ -205,15 +206,15 @@ def test_invalid_response_headers(self, sock):
205206 }
206207
207208 headers [key_header ] = invalid_value
208-
209+
209210 request = [status_line ] + [k + b" : " + v for (k , v ) in headers .items ()] + [b'\r \n ' ]
210211 s .recv .return_value = b"\r \n " .join (request )
211212
212213 self .assertRaises (HandshakeError , c .connect )
213214 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
214215 s .close .assert_called_once_with ()
215216 sock .reset_mock ()
216-
217+
217218class ThreadedClientTest (unittest .TestCase ):
218219
219220 @patch ('ws4py.client.socket' )
@@ -277,7 +278,6 @@ def test_thread_is_started_once_connected_secure(self):
277278 self .assertFalse (self .client ._th .is_alive ())
278279
279280
280-
281281if __name__ == '__main__' :
282282 suite = unittest .TestSuite ()
283283 loader = unittest .TestLoader ()
0 commit comments