Add a limit to WebSocket message size (Fixes #193)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import asyncio
|
||||
import sys
|
||||
import unittest
|
||||
from microdot import Microdot
|
||||
from microdot.websocket import with_websocket, WebSocket
|
||||
from microdot import Microdot, Request
|
||||
from microdot.websocket import with_websocket, WebSocket, WebSocketError
|
||||
from microdot.test_client import TestClient
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ class TestWebSocket(unittest.TestCase):
|
||||
return self.loop.run_until_complete(coro)
|
||||
|
||||
def test_websocket_echo(self):
|
||||
WebSocket.max_message_length = 65537
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/echo')
|
||||
@@ -26,34 +27,10 @@ class TestWebSocket(unittest.TestCase):
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
results = []
|
||||
|
||||
def ws():
|
||||
data = yield 'hello'
|
||||
results.append(data)
|
||||
data = yield b'bye'
|
||||
results.append(data)
|
||||
data = yield b'*' * 300
|
||||
results.append(data)
|
||||
data = yield b'+' * 65537
|
||||
results.append(data)
|
||||
|
||||
client = TestClient(app)
|
||||
res = self._run(client.websocket('/echo', ws))
|
||||
self.assertIsNone(res)
|
||||
self.assertEqual(results, ['hello', b'bye', b'*' * 300, b'+' * 65537])
|
||||
|
||||
@unittest.skipIf(sys.implementation.name == 'micropython',
|
||||
'no support for async generators in MicroPython')
|
||||
def test_websocket_echo_async_client(self):
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/echo')
|
||||
@app.route('/divzero')
|
||||
@with_websocket
|
||||
async def index(req, ws):
|
||||
while True:
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
async def divzero(req, ws):
|
||||
1 / 0
|
||||
|
||||
results = []
|
||||
|
||||
@@ -72,6 +49,35 @@ class TestWebSocket(unittest.TestCase):
|
||||
self.assertIsNone(res)
|
||||
self.assertEqual(results, ['hello', b'bye', b'*' * 300, b'+' * 65537])
|
||||
|
||||
res = self._run(client.websocket('/divzero', ws))
|
||||
self.assertIsNone(res)
|
||||
WebSocket.max_message_length = -1
|
||||
|
||||
@unittest.skipIf(sys.implementation.name == 'micropython',
|
||||
'no support for async generators in MicroPython')
|
||||
def test_websocket_large_message(self):
|
||||
saved_max_body_length = Request.max_body_length
|
||||
Request.max_body_length = 10
|
||||
app = Microdot()
|
||||
|
||||
@app.route('/echo')
|
||||
@with_websocket
|
||||
async def index(req, ws):
|
||||
data = await ws.receive()
|
||||
await ws.send(data)
|
||||
|
||||
results = []
|
||||
|
||||
async def ws():
|
||||
data = yield '0123456789abcdef'
|
||||
results.append(data)
|
||||
|
||||
client = TestClient(app)
|
||||
res = self._run(client.websocket('/echo', ws))
|
||||
self.assertIsNone(res)
|
||||
self.assertEqual(results, [])
|
||||
Request.max_body_length = saved_max_body_length
|
||||
|
||||
def test_bad_websocket_request(self):
|
||||
app = Microdot()
|
||||
|
||||
@@ -106,7 +112,7 @@ class TestWebSocket(unittest.TestCase):
|
||||
(None, 'foo'))
|
||||
self.assertEqual(ws._process_websocket_frame(WebSocket.BINARY, b'foo'),
|
||||
(None, b'foo'))
|
||||
self.assertRaises(OSError, ws._process_websocket_frame,
|
||||
self.assertRaises(WebSocketError, ws._process_websocket_frame,
|
||||
WebSocket.CLOSE, b'')
|
||||
self.assertEqual(ws._process_websocket_frame(WebSocket.PING, b'foo'),
|
||||
(WebSocket.PONG, b'foo'))
|
||||
|
||||
Reference in New Issue
Block a user