Skip to content

Commit 66c2c00

Browse files
author
Vitalie Maldur
committed
Fix for (#976): refactor tests for websocket send_json and receive_json
1 parent 9b04034 commit 66c2c00

3 files changed

+9
-31
lines changed

tests/test_web_websocket.py

-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import unittest
3-
import json
43
from unittest import mock
54
from aiohttp import CIMultiDict, helpers
65
from aiohttp.web import (
@@ -144,25 +143,6 @@ def receive():
144143

145144
self.loop.run_until_complete(go())
146145

147-
def test_receive_json_nonjson(self):
148-
149-
@asyncio.coroutine
150-
def go():
151-
req = self.make_request('GET', '/')
152-
ws = WebSocketResponse()
153-
yield from ws.prepare(req)
154-
155-
@asyncio.coroutine
156-
def receive():
157-
return websocket.Message(websocket.MSG_TEXT, 'data', b'')
158-
159-
ws.receive = receive
160-
161-
with self.assertRaises(json.decoder.JSONDecodeError):
162-
yield from ws.receive_json()
163-
164-
self.loop.run_until_complete(go())
165-
166146
def test_send_str_nonstring(self):
167147
req = self.make_request('GET', '/')
168148
ws = WebSocketResponse()

tests/test_web_websocket_functional.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ def test_websocket_json_invalid_message(create_app_and_client):
3838
def handler(request):
3939
ws = web.WebSocketResponse()
4040
yield from ws.prepare(request)
41-
msg = yield from ws.receive()
42-
4341
try:
44-
msg.json()
42+
yield from ws.receive_json()
4543
except ValueError:
46-
ws.send_str("ValueError raised: '%s'" % msg.data)
44+
ws.send_str('ValueError was raised')
4745
else:
48-
raise Exception("No ValueError was raised")
46+
raise Exception('No Exception')
4947
finally:
5048
yield from ws.close()
5149
return ws
@@ -57,8 +55,8 @@ def handler(request):
5755
payload = 'NOT A VALID JSON STRING'
5856
ws.send_str(payload)
5957

60-
resp = yield from ws.receive()
61-
assert payload in resp.data
58+
data = yield from ws.receive_str()
59+
assert 'ValueError was raised' in data
6260

6361

6462
@pytest.mark.run_loop

tests/test_websocket_client_functional.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def handler(request):
2222
resp = yield from client.ws_connect('/')
2323
resp.send_str('ask')
2424

25-
msg = yield from resp.receive()
26-
assert msg.data == 'ask/answer'
25+
data = yield from resp.receive_str()
26+
assert data == 'ask/answer'
2727
yield from resp.close()
2828

2929

@@ -46,8 +46,8 @@ def handler(request):
4646

4747
resp.send_bytes(b'ask')
4848

49-
msg = yield from resp.receive()
50-
assert msg.data == b'ask/answer'
49+
data = yield from resp.receive_bytes()
50+
assert data == b'ask/answer'
5151

5252
yield from resp.close()
5353

0 commit comments

Comments
 (0)