@@ -42,12 +42,18 @@ async def send_str(self, data):
4242 def test_receive (self , data ):
4343 self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .TEXT , data , '' ))
4444
45- def test_binary (self ):
46- self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .BINARY , 0 , '' ))
45+ def test_binary (self , data = bytes () ):
46+ self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .BINARY , data , '' ))
4747
4848 def test_error (self ):
4949 self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .ERROR , 0 , '' ))
5050
51+ def test_close (self ):
52+ self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .CLOSED , 0 , '' ))
53+
54+ def test_ping (self ):
55+ self .receive_queue .put_nowait (aiohttp .WSMessage (aiohttp .WSMsgType .PING , 0 , '' ))
56+
5157 async def receive (self ):
5258 value = await self .receive_queue .get ()
5359 if self .receive_side_effect :
@@ -78,7 +84,9 @@ def setUp(self):
7884 self .ws_loop_future = self .loop .run_until_complete (self .server .ws_connect ())
7985
8086 def tearDown (self ):
81- self .loop .run_until_complete (self .server .close ())
87+ if self .server .connected :
88+ self .client .test_server .test_close ()
89+ self .loop .run_until_complete (self .ws_loop_future )
8290 teardown_test_loop (self .loop )
8391
8492 @property
@@ -92,6 +100,9 @@ def handler(self, value):
92100 def receive (self , data ):
93101 self .client .test_server .test_receive (data )
94102
103+ def receive_binary (self , data ):
104+ self .client .test_server .test_binary (data )
105+
95106 def test_pep8_conformance (self ):
96107 """Test that we conform to PEP8."""
97108
@@ -157,6 +168,30 @@ async def test_message_not_json(self):
157168 await self .ws_loop_future
158169 self .assertIsInstance (transport_error .exception .args [1 ], ValueError )
159170
171+ @unittest_run_loop
172+ async def test_message_binary_not_utf8 (self ):
173+ # If we get a binary message, we should try to decode it as JSON, but
174+ # if it's not valid we should just ignore it, and an exception should
175+ # not be thrown
176+ self .receive_binary (bytes ((0xE0 , 0x80 , 0x80 )))
177+ self .client .test_server .test_close ()
178+ await self .ws_loop_future
179+
180+ @unittest_run_loop
181+ async def test_message_binary_not_json (self ):
182+ # If we get a binary message, we should try to decode it as JSON, but
183+ # if it's not valid we should just ignore it, and an exception should
184+ # not be thrown
185+ self .receive_binary ('not json' .encode ())
186+ self .client .test_server .test_close ()
187+ await self .ws_loop_future
188+
189+ @unittest_run_loop
190+ async def test_message_ping_ignored (self ):
191+ self .client .test_server .test_ping ()
192+ self .client .test_server .test_close ()
193+ await self .ws_loop_future
194+
160195 @unittest_run_loop
161196 async def test_connection_timeout (self ):
162197 def bad_connect ():
@@ -180,6 +215,22 @@ def handler(server, data):
180215
181216 self .receive ('{"jsonrpc": "2.0", "method": "test_method", "id": 1}' )
182217
218+ @unittest_run_loop
219+ async def test_server_request_binary (self ):
220+ # Test that if the server sends a binary websocket message, that's a
221+ # UTF-8 encoded JSON request we process it
222+ def test_method ():
223+ return 1
224+ self .server .test_method = test_method
225+
226+ def handler (server , data ):
227+ response = json .loads (data )
228+ self .assertEqual (response ["result" ], 1 )
229+
230+ self .handler = handler
231+
232+ self .receive_binary ('{"jsonrpc": "2.0", "method": "test_method", "id": 1}' .encode ())
233+
183234 @unittest_run_loop
184235 async def test_server_notification (self ):
185236 def test_method ():
0 commit comments