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,89 +31,89 @@ 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" ,
@@ -140,32 +136,32 @@ def test_connect_and_close(self, sock):
140136 f .parser .send (args [0 ][0 ])
141137 f .parser .close ()
142138 self .assertIn (b'boom' , f .unmask (f .body ))
143-
139+
144140 @patch ('ws4py.client.socket' )
145141 def test_empty_response (self , sock ):
146-
142+
147143 s = MagicMock ()
148144 sock .socket .return_value = s
149145 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
150146 ("127.0.0.1" , 80 , 0 , 0 ))]
151147
152148 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
153-
149+
154150 s .recv .return_value = b""
155151 self .assertRaises (HandshakeError , c .connect )
156152 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
157153 s .close .assert_called_once_with ()
158-
154+
159155 @patch ('ws4py.client.socket' )
160- def test_invdalid_response_code (self , sock ):
161-
156+ def test_invalid_response_code (self , sock ):
157+
162158 s = MagicMock ()
163159 sock .socket .return_value = s
164160 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
165161 ("127.0.0.1" , 80 , 0 , 0 ))]
166162
167163 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
168-
164+
169165 s .recv .return_value = b"\r \n " .join ([
170166 b"HTTP/1.1 200 Switching Protocols" ,
171167 b"Connection: Upgrade" ,
@@ -181,18 +177,18 @@ def test_invdalid_response_code(self, sock):
181177 self .assertRaises (HandshakeError , c .connect )
182178 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
183179 s .close .assert_called_once_with ()
184-
180+
185181 @patch ('ws4py.client.socket' )
186182 def test_invalid_response_headers (self , sock ):
187-
183+
188184 for key_header , invalid_value in ((b'upgrade' , b'boom' ),
189185 (b'connection' , b'bim' )):
190186 s = MagicMock ()
191187 sock .socket .return_value = s
192188 sock .getaddrinfo .return_value = [(socket .AF_INET , socket .SOCK_STREAM , 0 , "" ,
193189 ("127.0.0.1" , 80 , 0 , 0 ))]
194190 c = WebSocketBaseClient (url = "ws://127.0.0.1/?token=value" )
195-
191+
196192 status_line = b"HTTP/1.1 101 Switching Protocols"
197193 headers = {
198194 b"connection" : b"Upgrade" ,
@@ -205,15 +201,15 @@ def test_invalid_response_headers(self, sock):
205201 }
206202
207203 headers [key_header ] = invalid_value
208-
204+
209205 request = [status_line ] + [k + b" : " + v for (k , v ) in headers .items ()] + [b'\r \n ' ]
210206 s .recv .return_value = b"\r \n " .join (request )
211207
212208 self .assertRaises (HandshakeError , c .connect )
213209 s .shutdown .assert_called_once_with (socket .SHUT_RDWR )
214210 s .close .assert_called_once_with ()
215211 sock .reset_mock ()
216-
212+
217213class ThreadedClientTest (unittest .TestCase ):
218214
219215 @patch ('ws4py.client.socket' )
0 commit comments